NumPy argmin()

The argmin() method returns the index of the smallest element of an array.

Example

import numpy as np
array1 = np.array([10, 12, 14, 11, 5])

# return index of smallest element (5) minIndex = np.argmin(array1)
print(minIndex) # Output: 4

argmin() Syntax

The syntax of argmin() is:

numpy.argmin(array, axis = None, out = None, keepdims = <no value>)

argmin() Arguments

The argmin() method takes four arguments:

  • array - input array
  • axis (optional) - axis along which index is returned (int)
  • out (optional) - array to store the output
  • keepdims (optional) - whether to preserve the input array's dimension (bool)

argmin() Return Value

The argmin() method returns the index of the smallest element.


Example 1: argmin() With String

The argmin() method with an array of string or char returns the index of the smallest element based on ASCII value.

import numpy as np
array = np.array(['A', 'B', 'G', 'D', 'C'])

# return index of min element 'A' minArgument = np.argmin(array)
print(minArgument)

Output

0

Example 2: argmin() With 2D Array

The axis argument defines how to handle the index of the smallest element in a 2D array.

  • If axis = None, the array is flattened and the index of the flattened array is returned.
  • If axis = 0, the index of the smallest element in each column is returned.
  • If axis = 1, the index of the smallest element in each row is returned.
import numpy as np
array = np.array([[10, 17, 25], 
  [15, 11, 22]])
                  
# return the index of the smallest element of the flattened array minIndex = np.argmin(array)
print('Index of the smallest element in the flattened array: ', minIndex)
# return the index of the smallest element in each column minIndex = np.argmin(array, axis = 0)
print('Index of the smallest element in each column (axis 0): ', minIndex)
# return the index of the smallest element in each row minIndex = np.argmin(array, axis = 1)
print('Index of the smallest element in each row (axis 1): ', minIndex)

Output

Index of the smallest element in the flattened array:  0
Index of the smallest element in each column (axis 0):  [0 1 1]
Index of the smallest element in each row (axis 1):  [0 1]

Example 3: argmin() With 'out' Array

In our previous examples, the argmin() function generated a new array as output.

However, we can use an existing array to store the output using the out argument.

import numpy as np

array1 = np.array([[10, 17, 25], 
    [15, 11, 22], 
    [11, 19, 20]])

# create an empty array
array2= np.array([0, 0, 0])

# pass the 'out' argument to store the result in array2 np.argmin(array1, axis = 0, out = array2)
print(array2)

Output

[0 1 2]

Example 4: argmin() With keepdims

When keepdims = True, the dimensions of the resulting array matches the dimension of an input array.

import numpy as np

array1 = np.array([[10, 17, 25], 
            		    [15, 11, 22]])
print('Shape of original array: ', array1.shape)

minIndex = np.argmin(array1, axis = 1)
print('\n Without keepdims: \n', minIndex) print('Shape of array: ', minIndex.shape)
# set keepdims to True to retain the dimension of the input array minIndex = np.argmin(array1, axis = 1, keepdims = True)
print('\n With keepdims: \n', minIndex) print('Shape of array: ', minIndex.shape)

Output

Shape of original array:  (2, 3)

 Without keepdims: 
[0 1]
Shape of array:  (2,)

 With keepdims: 
 [[0]
 [1]]
Shape of array:  (2, 1)

Without keepdims, the result is simply a one-dimensional array of indices.

With keepdims, the resulting array has the same number of dimensions as the input array.

Similarly, with axis = 1 and keepdims = True, the resulting array has the same number of rows as the input array, but its columns have a single element i.e. the index of the smallest element in that column.