我有两个包含浮点数值的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
和numpy.allclose
的组合,但无法使其工作。当然,我可以用循环来做,但那似乎很繁琐而且不符合Python风格。
np.isclose
代替np.allclose
? - Divakar