为什么所有False bool列表的numpy.argmax都为零? [英] Why does numpy.argmax for a list of all False bools yield zero?
问题描述
我正在使用numpy.argmax
来计算第一个索引,在其中可以在布尔向量中找到True
.调用pandas.Series
会给我系列索引,而不是元素索引.
I'm using numpy.argmax
to calculate the first index where True
can be found in a vector of bools. Invoking on a pandas.Series
gives me the Series index rather than the element index.
我在代码中发现了一个细微的错误,当向量全为False时会弹出该错误;在这种情况下返回索引0似乎很危险,因为True很可能是True在第一个元素中的情况.此返回值的设计选择是什么?
I found a subtle bug in my code that popped up when the vector was all False; returning index 0 in this case seems dangerous since True could very well be the case where True was in the first element. What's the design choice for this return value?
>>> numpy.argmax([False,False,False])
0
>>> numpy.argmax([True, False, True])
0
>>> s = pandas.Series( [ False, False, False ] , index=[3,6,9] )
>>> numpy.argmax(s)
3
>>> s1 = pandas.Series( [ True, False, False ] , index=[3,6,9] )
>>> numpy.argmax(s1)
3
推荐答案
来自源代码:
In case of multiple occurrences of the maximum values, the indices
corresponding to the first occurrence are returned.
In case of multiple occurrences of the maximum values, the indices
corresponding to the first occurrence are returned.
在向量全为False的情况下,最大值为零,因此返回第一次出现的最大值的索引,即0.
In the case where the vector is all False, the max value is zero so the index of the first occurrence of the max value i.e. 0 is returned.
这篇关于为什么所有False bool列表的numpy.argmax都为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!