Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find float in ndarray

I tried to find a float number in ndarray. Due to the software package I am using (Abaqus), the precision it outputs is a little bit low. For example, 10 is something like 10.00003. Therefore, I was wondering whether there is a "correct" way to do it, that is neater than my code.

Example code:

import numpy as np

array = np.arange(10)
number = 5.00001

if I do this:

idx = np.where(number==array)[0][0]

Then the result is empty because 5.00001 does not equal to 5.

Now I am doing:

atol = 1e-3 # Absolute tolerance
idx = np.where(abs(number-array) < atol)[0][0]

which works, and is not too messy... Yet I was wondering there would be a neater way to do it. Thanks!

PS: numpy.allclose() is another way to do it, but I need to use number * np.ones([array.shape[0], array.shape[1]]) and it still seems verbose to me...


Edit: Thank you all so much for the fantastic answers! np.isclose() is the exact function that I am looking for, and I missed it since it is not in the doc... I wouldn't have realized this until they update the doc, if it weren't you guys. Thank you again!

like image 410
Yuxiang Wang Avatar asked Oct 28 '25 17:10

Yuxiang Wang


1 Answers

PS: numpy.allclose() is another way to do it, but I need to use number * np.ones([array.shape[0], array.shape[1]]) and it still seems verbose to me...

You almost never need to do anything like number * np.ones([array.shape[0], array.shape[1]]). Just as you can multiply that scalar number by that ones array to multiply all of its 1 values by number, you can pass that scalar number to allclose to compare all of the original array's values to number. For example:

>>> a = np.array([[2.000000000001, 2.0000000002], [2.000000000001, 1.999999999]])
>>> np.allclose(a, 2)
True

As a side note, if you really do need an array of all 2s, there's an easier way to do it than multiplying 2 by ones:

>>> np.tile(2, array.shape)
array([[2, 2], [2, 2]])

For that matter, I don't know why you need to do [array.shape[0], array.shape[1]]. If the array is 2D, that's exactly the same thing as array.shape. If the array might be larger, it's exactly the same as array.shape[:2].


I'm not sure this solves your actual problem, because it seems like you want to know which ones are close and not close, rather than just whether or not they all are. But the fact that you said you could use allclose if not for the fact that it's too verbose to create the array to compare with.

So, if you need whereclose rather than allclose… well, there's no such function. But it's pretty easy to build yourself, and you can always wrap it up if you're doing it repeatedly.

If you had an isclose method—like allclose, but returning a bool array instead of a single bool—you could just write:

idx = np.where(isclose(a, b, 0, atol))[0][0]

… or, if you're doing it over and over:

def whereclose(a, b, rtol=1e-05, atol=1e-08):
    return np.where(isclose(a, b, rtol, atol))

idx = whereclose(a, b, 0, atol)[0][0]

As it turns out, version 1.7 of numpy does have exactly that function (see also here), but it doesn't appear to be in the docs. If you don't want to rely on a possibly-undocumented function, or need to work with numpy 1.6, you can write it yourself trivially:

def isclose(a, b, rtol=1e-05, atol=1e-08):
    return np.abs(a-b) <= (atol + rtol * np.abs(b))
like image 190
abarnert Avatar answered Oct 31 '25 08:10

abarnert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!