Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy argmax - random tie breaking

Tags:

python

numpy

In numpy.argmax function, tie breaking between multiple max elements is so that the first element is returned. Is there a functionality for randomizing tie breaking so that all maximum numbers have equal chance of being selected?

Below is an example directly from numpy.argmax documentation.

>>> b = np.arange(6) >>> b[1] = 5 >>> b array([0, 5, 2, 3, 4, 5]) >>> np.argmax(b) # Only the first occurrence is returned. 1 

I am looking for ways so that 1st and 5th elements in the list are returned with equal probability.

Thank you!

like image 747
Jenna Kwon Avatar asked Feb 06 '17 15:02

Jenna Kwon


People also ask

What does argmax () do in Python?

The argmax function returns the argument or arguments (arg) for the target function that returns the maximum (max) value from the target function.

What does argmax in Numpy do?

Returns the indices of the maximum values along an axis. Input array. By default, the index is into the flattened array, otherwise along the specified axis.

What is the difference between NP Max and NP argmax?

Essentially, the argmax function returns the index of the maximum value of a Numpy array. What is this? It's somewhat similar to the Numpy maximum function, but instead of returning the maximum value, it returns the index of the maximum value.

Can argmax return multiple values?

argmax() function returns the indices of the maximum values along an axis. In case of multiple occurrences of the maximum values, the indices corresponding to the first occurrence will be returned.


2 Answers

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]) 
like image 103
Divakar Avatar answered Sep 22 '22 03:09

Divakar


In the case of a multi-dimensional array, choice won't work.

An alternative is

def randargmax(b,**kw):   """ a random tie-breaking argmax"""   return np.argmax(np.random.random(b.shape) * (b==b.max()), **kw) 

If for some reason generating random floats is slower than some other method, random.random can be replaced with that other method.

like image 44
Manux Avatar answered Sep 21 '22 03:09

Manux