<分区>
我有一个 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。