I have a numpy array:
foo = array([3, 1, 4, 0, 1, 0])
I want the top 3 items. Calling
foo.argsort()[::-1][:3]
returns
array([2, 0, 4])
Notice values foo[1]
and foo[4]
are equal, so numpy.argsort()
handles the tie by returning the index of the item which appears last in the array; i.e. index 4.
For my application I can't have the tie breaking always bias the end of the array, so how can I implement a random tie break? That is, half the time I would get array([2, 0, 4])
, and the other half I would get array([2, 0, 1])
.