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).
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.
What you look for is np.isclose
:
np.where(np.isclose(x, y))
You can always use something relying on:
np.where( np.abs(x-y) < epsilon )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With