What might be the cause of 'invalid value encountered in less_equal' in numpy
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<=BRuntimeWarning: invalid value encountered in less_equalOut[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] <= BRuntimeWarning: invalid value encountered in less_equalOut[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.
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.)
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).