HolyMonk HolyMonk - 5 months ago 37
Python Question

The logic behind `np.nanargmin([np.nan, np.inf]) = 0`

I understand that

finds the smallest number in a list that is not a NaN. However if called upon the array
[np.nan, np.inf]
it yields
which is a NaN. I find this behavior rather odd and am just wondering what the logic in defining
this way.

Answer Source

If you look at the documentation for np.nanargmin it says:

Warning: the results cannot be trusted if a slice contains only NaNs and Infs.

If you view the source code it has the following line:

a, mask = _replace_nan(a, np.inf)

So it is replacing all nan occurrences with inf, and so then it is finding the min (still a bit questionable), which will be argmin([inf, inf]).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download