Why is "NaN" considered "less" than "-np.inf" in numpy?

What is the reason NaNless -np.inffor any comparisons to np.minor np.argmin?

import numpy as np
In [73]: m = np.array([np.nan, 1., 0., -np.inf])
In [74]: n = np.array([-np.inf, 1., 0., np.nan])

# Huh??
In [75]: np.min(m)
Out[75]: nan
In [76]: np.min(n)
Out[76]: nan

# Same for np.argmin
In [77]: np.argmin(m)
Out[77]: 0
In [78]: np.argmin(n)
Out[78]: 3

# Its all false!
In [79]: np.nan < -np.inf
Out[79]: False

In [80]: np.nan > -np.inf
Out[80]: False

# OK, that seems to fix it, but its not necessarily elegant
In [81]: np.nanmin(m)
Out[81]: -inf

In [82]: np.nanargmin(m)
Out[82]: 3

I would suggest that this is probably a side effect of any comparisons with NaNreturning values , Falsehowever this imho leads to some rather annoying effects when you "accidentally" sometimes end up with a value NaNin your array. Using elements np.nanminor np.nanargminseems like a quick fix that was somehow stitched over existing behavior.

docs:" NaN , NaN, min NaN. NaN ( MATLAB), nanmin. , . NaN ? ?

+4
2

@Dunno, NaN , , , . IEEE 754 , NaN :

: , , . , NaN. NaN ,

:

# Its all false!
In [79]: np.nan < -np.inf
Out[79]: False

"" , , "".

+4

, : -

"inf" - - , . "-inf", , , , .

"nan" " ".

, "m, n", , "np.min()" , , , "nan" , :

 if (@isnan@(mp)) { /* nan encountered; it maximal */ return 0; } 

, , "" !

, , "nan",

    In [1]: import numpy as np

    In [2]: m = np.array([1., 0., -np.inf, np.nan])

    In [3]: n = np.array([np.nan, 1., np.nan, 0.])

    In [4]: np.argmin(m)
    Out[4]: 3

    In [5]: np.argmin(n)
    Out[5]: 0

, "np.nan < -np.inf" "np.nan > -np.inf", "False" , "nan" "-inf" "False" , - , , , "" !

, "" min "np.nanmin()", "-inf" !

, "" , "inf" "-inf", , - , "False" !!

    In [1]: np.nan < 1
    Out[1]: False

    In [2]: np.nan > 1
    Out[2]: False

.................

, !

0

Source: https://habr.com/ru/post/1665880/


All Articles