Why does numpy.argmax for a list of all False bools yield zero?
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Over a Mysterious Island Looping
--
Chapters
00:00 Why Does Numpy.Argmax For A List Of All False Bools Yield Zero?
00:36 Accepted Answer Score 8
01:00 Answer 2 Score 3
01:36 Answer 3 Score 1
01:49 Thank you
--
Full question
https://stackoverflow.com/questions/4576...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #numpy
#avk47
ACCEPTED ANSWER
Score 9
From the source code:
In case of multiple occurrences of the maximum values, the indices
corresponding to the first occurrence are returned.
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.
ANSWER 2
Score 3
So at the end of the day it was a misinterpretation of argmax (which is a straightforward function), forgetting that False and True are values that have an order. I was blindsided to these realities in using argmax as a tool in service of to finding a specific element (an index to any True element) and expecting it to behave like a common find function with the common conventions of returning an empty list [], -1 for an index, or even None under the condition the element does not exit.
I wound up coding my ultimate solution as follows
s = pandas.Series( listOfBools )
idx = s.argmax()
if idx == s.index[0] and not s[idx] :
return -1
return idx
ANSWER 3
Score 1
If you are using pandas, can you mask the boolean series with itself and then take the min or max of that series? This gives nan if there are no True values.
>>> s = pd.Series([False, False, True, False, True, False],
index=[0, 1, 2, 3, 4, 5])
>>> s[s].index.max()
4
>>> s[s].index.min()
2
>>> s = pd.Series([False, False, False], index=[0,1,2])
>>> s[s].index.max()
nan