I have two numpy arrays with floating point values and I am trying to find the indices where the numbers are approximately equal (floating point comparisons).
我有两个具有浮点值的numpy数组,我试图找到数字大致相等的索引(浮点比较)。
So something like:
所以类似于:
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
I tried something like
我尝试过类似的东西
np.where(np.allclose(x, y))
However, this returns an empty array. If I do:
但是,这会返回一个空数组。如果我做:
np.where(x == y) # This is fine.
I tried using a combination of numpy.where
and numpy.allclose
but could not make it work. Of course, I can do it with a loop but that seems tedious and unpythonic.
我尝试使用numpy.where和numpy.allclose的组合,但无法使其工作。当然,我可以通过一个循环来做到这一点,但这看起来很单调乏味。
2 个解决方案
#2
2
You can always use something relying on:
你可以随时使用依赖的东西:
np.where( np.abs(x-y) < epsilon )
#1
#2
2
You can always use something relying on:
你可以随时使用依赖的东西:
np.where( np.abs(x-y) < epsilon )