NumPy Tutorial NumPy Statistics NumPy References

NumPy - argmin() function



The NumPy argmin() function returns the indices of the minimum values along an axis. It is calculated over the flattened array by default, otherwise over the specified axis.

Syntax

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

Parameters

a Required. Specify the input array (array_like).
axis Optional. Specify axis or axes along which the indices of the minimum values are computed. The default is to compute it over the flattened array.
out Optional. Specify output array for the result. The default is None. If provided, it must have the same shape as output.

Return Value

Returns an array containing indices of the minimum values when out=None, otherwise returns a reference to the output array.

Example:

In the example below, argmin() function is used to find out index of the minimum value in the whole array.

import numpy as np
Arr = np.arange(12).reshape(3,4)

print("Array is:")
print(Arr)

#index of minimum value
idx = np.argmin(Arr)
print("\nIndex of minimum value is:", idx)

The output of the above code will be:

Array is:
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

Index of minimum value is: 0

Example:

When axis parameter is provided, index of minimum value can be calculated over the specified axes. Consider the following example.

import numpy as np
Arr = np.arange(12).reshape(3,4)

print("Array is:")
print(Arr)

#Index of minimum value along axis=0
print("\nIndex of minimum value along axis=0")
print(np.argmin(Arr, axis=0))

#Index of minimum value along axis=1
print("\nIndex of minimum value along axis=1")
print(np.argmin(Arr, axis=1))

The output of the above code will be:

Array is:
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

Index of minimum value along axis=0
[0 0 0 0]

Index of minimum value along axis=1
[0 0 0]

Example:

When the array contains more than one minimum value, the function returns index of first occurrence of it.

import numpy as np
Arr = [20, 20, 10, 20, 30, 50, 10]

print("Array is:", Arr)

#Index of minimum value
print("Index of minimum value is:", np.argmin(Arr))

The output of the above code will be:

Array is: [20, 20, 10, 20, 30, 50, 10]
Index of minimum value is: 2

❮ NumPy - Functions