简体   繁体   English

为什么numpy中的“ NaN”比“ -np.inf”“小”?

[英]Why is `NaN` considered “smaller” than `-np.inf` in numpy?

What is the reason that NaN 's are considered less than -np.inf in any comparisons involving np.min or np.argmin ? 在涉及np.minnp.argmin任何比较中, NaN小于-np.inf的原因是什么?

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. 我想这可能与NaN值返回False的任何比较的副作用,但是当您“碰巧”有时以数组中的NaN值结尾时,这种恕我直言会带来一些相当烦人的效果。 The usage of np.nanmin or np.nanargmin some feels like a quickfix that was somehow stapled on top of the existing behaviour. np.nanminnp.nanargmin的使用有些感觉像是在现有行为基础上以某种方式添加的np.nanmin np.nanargmin

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? 除了文档中的注释外:“ 传播NaN值,也就是说,如果至少一项是NaN,则相应的最小值也将是NaN。要忽略NaN值(MATLAB行为),请使用nanmin。找不到解释这种行为背后原因的任何东西。这是想要的还是NaN值的特定内部表示的副作用?为什么?

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. 正如@Dunno在评论中提到的,将NaN与数字进行比较并没有太多意义,因此这种行为可能是可以的。 The IEEE 754 standard says this about comparing NaNs with numbers: IEEE 754标准说明了将NaN与数字进行比较的方法:

Four mutually exclusive relations are possible: less than, equal, greater than, and unordered. 可能存在四种互斥关系:小于,相等,大于和无序。 The last case arises when at least one operand is NaN. 当至少一个操作数为NaN时,出现最后一种情况。 Every NaN shall compare unordered with everything, including itself 每个NaN都应将无序与所有事物(包括自身)进行比较

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". 将导致“无序”结果,因此它属于“小于”关系是不正确的。

So, as you might be already knowing this:- 因此,您可能已经知道:

" inf " is infinity - a value that is greater than any other value. “ inf”是无穷大-一个大于任何其他值的值。 " -inf " is, therefore, smaller than any other value remember this value is a number. 因此,“-inf”小于任何其他值。请记住,该值是一个数字。

" nan " stands for Not A Number. “ nan”表示不是数字。

So, if according to your array " m,n " as you declared above and as soon as you perform " np.min() " on any of these actually what happens is as soon as " nan " is encountered the other elements are not checked or compared and the following statement is executed and the value is returned: 因此,如果根据您在上面声明的数组“ m,n”以及一旦对其中任何一个执行“ np.min()”,实际上发生的就是在遇到“ nan”时其他元素就不会检查或比较并执行以下语句并返回值:

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

and thus a " nan " is returned as the answer to the function! 因此返回“ nan”作为函数的答案!

Check this code it returns as soon as it encounter's first " nan " and its position is returned in the corresponding function 检查此代码,遇到第一个“ 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

and the operation's such as " np.nan < -np.inf " and " np.nan > -np.inf " returns " False " because " nan " cannot be compared by any number " -inf " here and the " False " in each case of above operation is not the answer of comparison but is because of a kind of exception or the execution of the code mentioned above which is due to logical error because though infinity but it cannot be compared to a thing " nothing " with respect to a number! 并且“ np.nan <-np.inf”和“ np.nan> -np.inf”之类的操作返回“ False”,因为“ nan”在此处不能与任何数字“ -inf”和“ False”进行比较在上述每种情况下,操作都不是比较的答案,而是由于一种异常或上述代码的执行,这是由于逻辑错误而引起的,因为尽管无穷大,但相对于“无”而言到一个数字!

and therefore if you remove all " nan " in the array and then calculate min by " np.nanmin() " you get the output as expected " -inf " here no problem arises! 因此,如果删除数组中的所有“ nan”,然后通过“ np.nanmin()”计算min,则输出将如预期的那样-inf,这不会出现问题!

So," Nan "is not smaller or greater than " inf " or " -inf " because actually it is not comparable with any of these or any number it will return " False " in comparison with any number !! 因此,“ Nan”不小于或大于“ inf”或“ -inf”,因为实际上它与任何这些数字或任何数字都不可比,与任何数字相比,它将返回“ False”!

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

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

and so on ............... 等等 ...............

Hope it helps !! 希望能帮助到你 !!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM