[Numpy-discussion] tie breaking for max, min, argmax, argmin

Johannes Kulick johannes.kulick at ipvs.uni-stuttgart.de
Thu Mar 12 09:31:51 EDT 2015


Hello,

I wonder if it would be worth to enhance max, min, argmax and argmin (more?)
with a tie breaking parameter: If multiple entries have the same value the first
value is returned by now. It would be useful to have a parameter to alter this
behavior to an arbitrary tie-breaking. I would propose, that the tie-breaking
function gets a list with all indices of the max/mins. 

Example:
>>> a = np.array([ 1, 2, 5, 5, 2, 1])
>>> np.argmax(a, tie_breaking=random.choice)
3

>>> np.argmax(a, tie_breaking=random.choice)
2

>>> np.argmax(a, tie_breaking=random.choice)
2

>>> np.argmax(a, tie_breaking=random.choice)
2

>>> np.argmax(a, tie_breaking=random.choice)
3

Especially for some randomized experiments it is necessary that not always the
first maximum is returned, but a random optimum. Thus I end up writing these
things over and over again.

I understand, that max and min are crucial functions, which shouldn't be slowed
down by the proposed changes. Adding new functions instead of altering the
existing ones would be a good option.

Are there any concerns against me implementing these things and sending a pull
request? Should such a function better be included in scipy for example?

Best,
Johannes



More information about the NumPy-Discussion mailing list