Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

nan

numpy

What is the reason that NaN's are considered less than -np.inf in any comparisons involving np.min or 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 guess that its probably a side effect of any comparisons with NaN values returning False, however this imho leads to some rather annoying effects when you "happen" to sometimes end up with a NaN value in your array. The usage of np.nanmin or np.nanargmin some feels like a quickfix that was somehow stapled on top of the existing behaviour.

Apart from that note in the docs: "NaN values are propagated, that is if at least one item is NaN, the corresponding min value will be NaN as well. To ignore NaN values (MATLAB behavior), please use nanmin., I haven't found anything that explains the rationale behind that behaviour. Is this wanted or a side effect of a particular internal representation of NaN values? And why?

like image 327
tttthomasssss Avatar asked Jan 05 '17 13:01

tttthomasssss


People also ask

What is NaN and INF in Python?

inf is infinity - a value that is greater than any other value. -inf is therefore smaller than any other value. nan stands for Not A Number, and this is not equal to 0 .

What is NaN value in numpy?

Introduction to NumPy NaN. In Python, NumPy NAN stands for not a number and is defined as a substitute for declaring value which are numerical values that are missing values in an array as NumPy is used to deal with arrays in Python and this can be initialized using numpy.

What does NaN stand for in Python?

NaN stands for Not A Number and is one of the common ways to represent the missing value in the data. It is a special floating-point value and cannot be converted to any other type than float.

What does INFS mean in Python?

Definition and Usageinf constant returns a floating-point positive infinity. For negative infinity, use -math. inf .


1 Answers

As @Dunno mentioned in a comment, it does not give much meaning to compare a NaN with a number, so this behaviour is probably ok. The IEEE 754 standard says this about comparing NaNs with numbers:

Four mutually exclusive relations are possible: less than, equal, greater than, and unordered. The last case arises when at least one operand is NaN. Every NaN shall compare unordered with everything, including itself

According to the standard this:

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

would result in an "unordered" result, so it is not true that it is belongs to the relation "less than".

like image 170
J. P. Petersen Avatar answered Sep 29 '22 05:09

J. P. Petersen