简体   繁体   中英

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

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

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]) .

If you look in the source you see:

a, mask = _replace_nan(a, np.inf)
res = np.argmin(a, axis=axis)

meaning that it's replacing np.nan with np.inf . Since np.argmin for repeated values returns the first instance of that value, np.argmin([np.inf, np.inf]) returns 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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