Here is what I am trying to do with Numpy in Python 2.7. Suppose I have an array a
defined by the following:
a = np.array([[1,3,3],[4,5,6],[7,8,1]])
I can do a.argmax(0)
or a.argmax(1)
to get the row/column wise argmax:
a.argmax(0)
Out[329]: array([2, 2, 1], dtype=int64)
a.argmax(1)
Out[330]: array([1, 2, 1], dtype=int64)
However, when there is a tie like in a
's first row, I would like to get the argmax decided randomly between the ties (by default, Numpy returns the first element whenever a tie occurs in argmax or argmin).
Last year, someone put a question on solving Numpy argmax/argmin ties randomly: Select One Element in Each Row of a Numpy Array by Column Indices
However, the question aimed at uni-dimensional arrays. There, the most voted answer works well for that. There is a second answer that attempts to solve the problem also for multidimensional arrays but doesn't work - i.e. it does not return, for each row/column the index of the maximum value with ties solved randomly.
What would be the most performent way to do that, since I am working with big arrays?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…