numpy.argmax() in Python


The Python numpy.argmax() function returns the indices of maximum elements along the specific axis inside the array.

Basic Syntax

Following is the basic syntax for numpy.argmax() function in Python:

numpy.argmax(arr, axis=None, out=None)

And the parameters are:

Parameter Description
arr The input array
axis [int, OPTIONAL] Along the axis like 1 or 0.
out [array, OPTIONAL] If provided then it will insert output to the out array with appropriate shape.

 

Return Value

This function returns array of indices into the array.

Example’s

Following are the examples for numpy.argmax() function:

Example 1

# Python Program illustration of numpy.argmax() function 

import numpy as np 

# Generating 2D array for input 
array = np.arange(15).reshape(3, 5) 
print("The input array: \n", array) 

# without axis
print("\nThe max element: ", np.argmax(array))
# with axis
print("\nThe indices of max element: ", np.argmax(array, axis=0)) 
print("\nThe indices of max element: ", np.argmax(array, axis=1))  

The output for the above program is as given below:

The input array:
[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]]
The max element: 14
The indices of max element: [2, 2, 2, 2, 2]
The indices of max element: [4, 4, 4]

Example 2

# Python Program illustration of numpy.argmax() function 

import numpy as np 

# Generating 2D array for input 
array = np.arange(20).reshape(4, 5) 
array[0][2] = 17

print("The input array: \n", array) 

# without axis
print("\nThe max element: ", np.argmax(array))
# with axis
print("\nThe indices of max element: ", np.argmax(array, axis=0)) 
print("\nThe indices of max element: ", np.argmax(array, axis=1)) 

The output for the above program is as given below:

The input array:
[[ 0, 1, 17, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]]
The max element: ‘, 19)
The indices of max element: ‘, array([3, 3, 0, 3, 3]
The indices of max element: ‘, array([2, 4, 4, 4]