简体   繁体   English

`np.nanargmin([np.nan, np.inf]) = 0` 背后的逻辑

[英]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.我知道np.nanargmin在列表中找到不是 NaN 的最小数字。 However if called upon the array [np.nan, np.inf] it yields 0 which is a NaN.但是,如果调用数组[np.nan, np.inf]它会产生0 ,这是一个 NaN。 I find this behavior rather odd and am just wondering what the logic in defining np.argmin this way.我发现这种行为很奇怪,我只是想知道以这种方式定义np.argmin的逻辑是什么。

If you look at the documentation for np.nanargmin it says:如果您查看np.nanargmin的文档,它会说:

Warning: the results cannot be trusted if a slice contains only NaNs and Infs.警告:如果切片仅包含 NaN 和 Inf,则结果不可信。

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]) .所以它用inf替换所有nan出现,然后它找到 min (仍然有点问题),这将是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 .这意味着它正在用np.nan替换np.inf Since np.argmin for repeated values returns the first instance of that value, np.argmin([np.inf, np.inf]) returns 0由于重复值的np.argmin返回该值的第一个实例,因此np.argmin([np.inf, np.inf])返回0

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

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