argmin

argmin(a, axis=None, out=None)

Returns the indices of the minimum values along an axis.

Parameters:
a : ndarray

Input array. Only support ndarrays of dtype float16, float32, and float64.

axis : int, optional

By default, the index is into the flattened array, otherwise along the specified axis.

out : ndarray or None, optional

If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype.

Returns:
index_array : ndarray of indices whose dtype is same as the input ndarray.

Array of indices into the array. It has the same shape as a.shape with the dimension along axis removed.

Notes

In case of multiple occurrences of the minimum values, the indices corresponding to the first occurrence are returned.

This function differs from the original numpy.argmin in the following aspects:

  • Input type does not support Python native iterables(list, tuple, …).
  • Output has dtype that is same as the input ndarray.
  • out param: cannot perform auto broadcasting. out ndarray’s shape must be the same as the expected output.
  • out param: cannot perform auto type cast. out ndarray’s dtype must be the same as the expected output.
  • out param does not support scalar input case.

Examples

>>> a = np.arange(6).reshape(2,3) + 10
>>> a
array([[10., 11., 12.],
       [13., 14., 15.]])
>>> np.argmin(a)
array(0.)
>>> np.argmin(a, axis=0)
array([0., 0., 0.])
>>> np.argmin(a, axis=1)
array([0., 0.])
>>> b = np.arange(6)
>>> b[2] = 0
>>> b
array([0., 1., 0., 3., 4., 5.])
>>> np.argmax(b)  # Only the first occurrence is returned.
array(0.)

Specify out ndarray:

>>> a = np.arange(6).reshape(2,3) + 10
>>> b = np.zeros((2,))
>>> np.argmin(a, axis=1, out=b)
array([0., 0.])
>>> b
array([0., 0.])