我有一个numpy数组ssh_sum
:
>>> ssh_sum
array([[ 0., 2., 1., 0., 0., 0.],
[ 0., 0., 1., 2., 0., 0.],
[ 0., 0., 0., 1., 0., 2.]])
我希望在这个数组中计算逐元素的倒数值。当我多次调用np.reciprocal时,Numpy会返回不同的值:
>>> ssh_sum
array([[ 0., 2., 1., 0., 0., 0.],
[ 0., 0., 1., 2., 0., 0.],
[ 0., 0., 0., 1., 0., 2.]])
>>> np.reciprocal(ssh_sum, where=(ssh_sum > 0.))
array([[ 6.90326535e-310, 5.00000000e-001, 1.00000000e+000,
0.00000000e+000, 1.07034283e-296, 1.33666925e+241],
[ 4.74783847e-309, 1.45260789e-296, 1.00000000e+000,
5.00000000e-001, 2.13436228e-287, -3.13188338e-294],
[ 4.85105226e-309, 1.08690709e+171, 4.09521901e+149,
1.00000000e+000, 2.82730247e-311, 5.00000000e-001]])
>>> np.reciprocal(ssh_sum, where=(ssh_sum > 0.))
array([[ inf, 0.5, 1. , inf, inf, inf],
[ inf, inf, 1. , 0.5, inf, inf],
[ inf, inf, inf, 1. , inf, 0.5]])
>>> np.reciprocal(ssh_sum, where=(ssh_sum > 0.))
array([[ 6.90326535e-310, 5.00000000e-001, 1.00000000e+000,
0.00000000e+000, 1.07034283e-296, 1.33666925e+241],
[ 4.74783847e-309, 1.45260789e-296, 1.00000000e+000,
5.00000000e-001, 2.13436228e-287, -3.13188338e-294],
[ 4.85105226e-309, 1.08690709e+171, 4.09521901e+149,
1.00000000e+000, 2.82730247e-311, 5.00000000e-001]])
>>> np.reciprocal(ssh_sum, where=(ssh_sum > 0.))
array([[ inf, 0.5, 1. , inf, inf, inf],
[ inf, inf, 1. , 0.5, inf, inf],
[ inf, inf, inf, 1. , inf, 0.5]])
这里发生了什么事情?我正在使用Python 3.4.5和numpy 1.13.3。
m = ssh_sum > 0 ; ssh_sum[m] = 1 / ssh_sum[m]
- cs95np.isclose
已经足够接近了。 - abarnert