Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
NumPy argmax()
In this tutorial, you will learn about the numpy.argmax() method with the help of examples.
The argmax() method returns the index of the largest element of an array.
Example
import numpy as np
array1 = np.array([10, 12, 14, 11, 5])
# return index of largest element (14)
maxIndex= np.argmax(array1)
print(maxIndex)
# Output: 2
argmax() Syntax
The syntax of argmax() is:
numpy.argmax(array, axis = None, out = None, keepdims = <no value>)
argmax() Arguments
The argmax() method takes four arguments:
array- input arrayaxis(optional) - axis along which index is returned (int)out(optional) - array to store the outputkeepdims(optional) - whether to preserve the input array's dimension (bool)
argmax() Return Value
The argmax() method returns the index of the largest element.
Example 1: argmax() With String
The argmax() method with an array of string or char returns the index of the largest element based on ASCII value.
import numpy as np
array = np.array(['A', 'B', 'G', 'D', 'C'])
# return index of max element 'G'
maxIndex = np.argmax(array)
print(maxIndex)
Output
2
Example 2: argmax() With 2D Array
The axis argument defines how we can handle the index of the largest 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 largest element in each column is returned. - If
axis= 1, the index of the largest element in each row is returned.
import numpy as np
array = np.array([[10, 17, 25], [15, 11, 22]])
# return the index of the largest element of the flattened array
maxIndex = np.argmax(array)
print('Index of the largest element in the flattened array: ', maxIndex)
# return the index of the largest element in each column
maxIndex = np.argmax(array, axis = 0)
print('Index of the largest element in each row (axis 0): ', maxIndex)
# return the index of the largest element in each row
maxIndex = np.argmax(array, axis = 1)
print('Index of the largest element in each row (axis 1): ', maxIndex)
Output
Index of the largest element in the flattened array: 2 Index of the largest element in each row (axis 0): [1 0 0] Index of the largest element in each row (axis 1): [2 2]
Example 3: argmax() With 'out' Array
In our previous examples, the argmax() 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.argmax(array1, axis = 0, out = array2)
print(array2)
Output
[1 2 0]
Example 4: argmax() 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)
maxIndex = np.argmax(array1, axis = 1)
print('\n Without keepdims: \n', maxIndex)
print('Shape of array: ', maxIndex.shape)
# set keepdims to True to retain the dimension of the input array
maxIndex = np.argmax(array1, axis = 1, keepdims = True)
print('\n With keepdims: \n', maxIndex)
print('Shape of array: ', maxIndex.shape)
Output
Shape of original array: (2, 3) Without keepdims: [2 2] Shape of array: (2,) With keepdims: [[2] [2]] 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 largest element in that column.