Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check array for values equal or very close to zero

I have a one dimensional numpy array for which I need to find out if any value is zero or very close to it. With this line I can check for zeros rapidly:

if 0. in my_array:
    # do something

but I also have very small elements like 1.e-22 which I would also like to treat as zeros (otherwise I get a Divide by zero warning further down the road)

Say my threshold is 1.e-6 and I want to efficiently check if any value in my array is smaller than that. How can I do this?

like image 850
Gabriel Avatar asked Oct 11 '13 23:10

Gabriel


People also ask

How do you check if all numbers in an array are equal?

To check if all values in an array are equal:Use the Array. every() method to iterate over the array. Check if each array element is equal to the first one. The every method only returns true if the condition is met for all array elements.

How do you check if an array is a condition?

To check if any value in JavaScript array satisfies the condition you can use Array. prototype. some() method. It returns true if any item in array satisfies the condition else returns false .

How do you check if a value is zero in Python?

To check if a value is zero or not None in Python: Use the double equals == operator to check if the value is equal to 0 . Use the is not operator to check if the value is not None .

How to find the closest value to zero from an array?

How to find the closest value to zero from an array with positive and negative numbers in JavaScript 1 If tsis empty, return 0 (zero). 2 If two numbers are as close to zero, consider the positive number as the closest to zero (eg. if tscontains -5 and 5,... More ...

How do you know if a number is close to zero?

If two numbers are as close to zero, consider the positive number as the closest to zero (eg. if ts contains -5 and 5, return 5). Temperatures are always expressed with floating-point numbers ranging from -273 to 5526. ts is always a valid array and is never null.

How to find the indices of array elements that are non-zero?

This f unction is used to find the indices of array elements that are non-zero, grouped by element. Method 3: Finding the indices of null elements using numpy.nonzero ()

How to find the temperature of two numbers closest to zero?

If two numbers are as close to zero, consider the positive number as the closest to zero (eg. if ts contains -5 and 5, return 5). Temperatures are always expressed with floating-point numbers ranging from -273 to 5526. ts is always a valid array and is never null. Based on the exposed data, the following implementation solves the problem:


2 Answers

There's no reason to loop in Python; just broadcast the abs and the < and use np.any:

np.any(np.absolute(my_array) < eps)
like image 86
abarnert Avatar answered Sep 18 '22 16:09

abarnert


If you are using this for testing you can use numpy.testing.assert_almost_equal

As the document says it uses a method similar to @phihag suggests:

any(abs(x) < 0.5 * 10**(-decimal))
like image 40
fabrizioM Avatar answered Sep 20 '22 16:09

fabrizioM