scipy.stats.mode
works great, but I need to break modal ties at random.
import numpy as np
import scipy.stats as stats
a = np.array([[3, 3, 4],
[3, 1, 0],
[4, 5, 0],
[4, 3, 0]])
stats.mode(a, axis=0)
Out[37]: ModeResult(mode=array([[3, 3, 0]]), count=array([[2, 2, 3]]))
For the first result (column), scipy.stats.mode
chooses 3 among the tied candidates 3 and 4, as follows:
If there is more than one such value, only the smallest is returned.
So among 3 and 4, it picks 3 because it's the smallest. I'd like to randomly choose among 3 and 4, but scipy.stats.mode
doesn't bring back enough information to allow me to do that. Is there a good way to do this using numpy
or a decent alternative?