我有两个带有浮点值的 numpy 数组,我试图找到数字大致相等的索引(浮点比较)。
所以像这样:
x = np.random.rand(3)
y = np.random.rand(3)
x[2] = y[2]
# Do the comparison and it should return 2 as the index
我尝试过类似的东西
np.where(np.allclose(x, y))
但是,这会返回一个空数组。如果我做:
np.where(x == y) # This is fine.
我尝试结合使用numpy.where
and numpy.allclose
但无法使其发挥作用。当然,我可以用循环来完成,但这看起来很乏味且不符合Python标准。
你要找的是np.isclose:
np.where(np.isclose(x, y))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)