How to return all the minimum indices in numpy

That documentation makes more sense when you think about multidimensional arrays.

>>> x = numpy.array([[0, 1],
...                  [3, 2]])
>>> x.argmin(axis=0)
array([0, 0])
>>> x.argmin(axis=1)
array([0, 1])

With an axis specified, argmin takes one-dimensional subarrays along the given axis and returns the first index of each subarray’s minimum value. It doesn’t return all indices of a single minimum value.

To get all indices of the minimum value, you could do

numpy.where(x == x.min())

Leave a Comment