Numpy argmax – random tie breaking

Use np.random.choice

np.random.choice(np.flatnonzero(b == b.max()))

Let’s verify for an array with three max candidates –

In [298]: b
Out[298]: array([0, 5, 2, 5, 4, 5])

In [299]: c=[np.random.choice(np.flatnonzero(b == b.max())) for i in range(100000)]

In [300]: np.bincount(c)
Out[300]: array([    0, 33180,     0, 33611,     0, 33209])

Leave a Comment