Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What might be the cause of 'invalid value encountered in less_equal' in numpy

I experienced a RuntimeWarning

 RuntimeWarning: invalid value encountered in less_equal

Generated by this line of code of mine:

center_dists[j] <= center_dists[i]

Both center_dists[j] and center_dists[i] are numpy arrays

What might be the cause of this warning ?

like image 855
Alex Gao Avatar asked Jan 22 '16 20:01

Alex Gao


3 Answers

That's most likely happening because of a np.nan somewhere in the inputs involved. An example of it is shown below -

In [1]: A = np.array([4, 2, 1])

In [2]: B = np.array([2, 2, np.nan])

In [3]: A<=B
RuntimeWarning: invalid value encountered in less_equal
Out[3]: array([False,  True, False], dtype=bool)

For all those comparisons involving np.nan, it would output False. Let's confirm it for a broadcasted comparison. Here's a sample -

In [1]: A = np.array([4, 2, 1])

In [2]: B = np.array([2, 2, np.nan])

In [3]: A[:,None] <= B
RuntimeWarning: invalid value encountered in less_equal
Out[3]: 
array([[False, False, False],
       [ True,  True, False],
       [ True,  True, False]], dtype=bool)

Please notice the third column in the output which corresponds to the comparison involving third element np.nan in B and that results in all False values.

like image 57
Divakar Avatar answered Oct 10 '22 10:10

Divakar


As a follow-up to Divakar's answer and his comment on how to suppress the RuntimeWarning, a safer way is suppressing them only locally using with np.errstate() (docs): it is good to generally be alerted when comparisons to np.nan yield False, and ignore the warning only when this is really what is intended. Here for the OP's example:

with np.errstate(invalid='ignore'):
  center_dists[j] <= center_dists[i]

Upon exiting the with block, error handling is reset to what it was before.

Instead of invalid value encountered, one can also ignore all errors by passing all='ignore'. Interestingly, this is missing from the kwargs in the docs for np.errstate(), but not in the ones for np.seterr(). (Seems like a small bug in the np.errstate() docs.)

like image 21
Ulrich Stern Avatar answered Oct 10 '22 10:10

Ulrich Stern


Adding to the above answers another way to suppress this warning is to use numpy.less explicitly, supplying the where and out parameters:

np.less([1, 2], [2, np.nan])  

outputs: array([ True, False]) causing the runtime warning,

np.less([1, 2], [2, np.nan], where=np.isnan([2, np.nan])==False)

does not calculate result for the 2nd array element according to the docs leaving the value undefined (I got True output for both elements), while

np.less([1, 2], [2, np.nan], where=np.isnan([2, np.nan])==False, out=np.full((1, 2), False)

writes the result into an array pre-initilized to False (and so always gives False in the 2nd element).

like image 4
Yuri Feldman Avatar answered Oct 10 '22 09:10

Yuri Feldman