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

2024/9/19 8:57:06

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]: FalseIn [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]: -infIn [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?

Answer

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 casearises when at least one operand is NaN. Every NaN shall compare unordered with everything, includingitself

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".

https://en.xdnf.cn/q/72223.html

Related Q&A

recursion within a class

I am trying to place a recursive formula inside a class statementclass SomeNode:def __init__(self, a):leng = len(a)half= leng/2self.firstnode=a[0][0]self.child1=SomeNode([a[i]for k in range(leng)])self…

There is an example of Spyne client?

Im trying to use spyne (http://spyne.io) in my server with ZeroMQ and MsgPack. Ive followed the examples to program the server side, but i cant find any example that helps me to know how to program the…

Safely bind method from one class to another class in Python [duplicate]

This question already has answers here:What is the difference between a function, an unbound method and a bound method?(6 answers)Closed 5 years ago.I know I can attach a function to a class and make …

Basic Python OpenCV cropping and resizing

can someone help me with a little cropping algorithm? its openCV.. im trying to figure this out. I know the method is crop = image[y:y1, x:x1]. If I have an image with new_dimensionXxnew_dimensionY pi…

Why does Keras loss drop dramatically after the first epoch?

Im training a U-Net CNN in Keras/Tensorflow and find that loss massively decreases between the last batch of the first epoch, and the first batch of the second epoch: Epoch 00001: loss improved from in…

extract strings from a binary file in python

I have a project where I am given a file and i need to extract the strings from the file. Basically think of the "strings" command in linux but im doing this in python. The next condition is …

Installing numpy on Mac to work on AWS Lambda

Is there a way to install numpy on a Mac so that it will work when uploaded to AWS Lambda? I have tried a variety of different ways, including using different pip versions, using easy_install, and fol…

python- how to get the output of the function used in Timer

I want to run a function for 10s then do other stuff. This is my code using Timerfrom threading import Timer import timedef timeout():b=truereturn ba=false t = Timer(10,timeout) t.start()while(a==f…

Create automated tests for interactive shell based on Pythons cmd module

I am building an interactive shell using Python 3 and the cmd module. I have already written simple unit tests using py.test to test the individual functions, such as the do_* functions. Id like to c…

Matplotlib with multiprocessing freeze computer

I have an issue with matplotlib and multiprocessing. I launch a first process, where I display an image and select an area, and close the figure. Then I launch another process, where I call a graph fun…