Python numpy.argmax() Function with Examples

Python provides us with a powerful module that allows us to work on difficult mathematical operations.

The NumPy module, which is also noted for its high level of complexity, provides us with a variety of data structures to work with. We frequently deal with data in terms of Arrays and Matrices. Analytics and preprocessing appear to be inadequate without NumPy as the mathematical model solution, whether it is a computation problem or a real-life problem.

numpy.argmax() Function:

The numpy.argmax() function returns the indices of the array’s maximum element in a particular axis.

Syntax:

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

Parameters

array: It is the array given as input

axis: It is an integer. This is optional. Along a specific axis, such as 0 represents along column and 1 along the row.

out: This is optional. It is an array. It provides the ability to insert output into the out array, which must be of acceptable shape and datatype.

numpy.argmax() Function with Examples in Python

1)Using argmax To Fetch Maximum Element from a Matrix

In a nutshell, the argmax() function allows us to obtain the maximum element from the entire array. However, if no axis is specified, i.e. by row or column, it just gives the number of elements at output.

Similarly, if we want to get the greatest element from the entire array without needing to provide the axis, we must use the unravel_index() function.

Approach:

  • Import numpy module using the import keyword.
  • Pass some random size(rowsize*colsize) to the arange() function and apply the reshape() function by passing arguments rowsize and column size.
  • Print the given array.
  • Apply argmax() function to the given array and pass it as an argument along with the shape of the given array to the unravel_index() function to get the index of the maximum element in the above-given matrix.
  • Store it in a variable
  • Print the above index value.
  • Print the maximum element in the given array.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random size(rowsize*colsize) to the arange() function and apply the reshape() 
# function by passing arguments rowsize and column size
gvn_arr = np.arange(16).reshape(4,4) 
# Print the given array
print("The given array\n", gvn_arr)
# Apply argmax() function to the given array and pass it as an argument along 
# with the shape of the given array to the unravel_index() function 
# to get the index of the maximum element in the above given matrix.
# Store it in a variable
print("The index of the maximum element in the above given matrix is:")
indx = np.unravel_index(np.argmax(gvn_arr), gvn_arr.shape)
# Print the above index value.
print(indx)
# Print the maximum element in the given array
print("The maximum element in the given array = ", gvn_arr[indx])

Output:

The given array
[[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]
 [12 13 14 15]]
The index of the maximum element in the above given matrix is:
(3, 3)
The maximum element in the given array = 15

2) Python code to fetch maximum element for a particular row

Now, we must add the argument axis=1 to the argmax() function.
As an output, this method returns the index of the maximum element across each row.

Approach:

  • Import numpy module using the import keyword.
  • Pass some random size(rowsize*colsize) to the arange() function and apply the reshape() function by passing arguments rowsize and column size.
  • Print the given array.
  • Pass the given array and axis=1 as arguments to the argmax() function to get the index of the maximum element for each row in the above-given matrix and print it.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random size(rowsize*colsize) to the arange() function and apply the reshape() 
# function by passing arguments rowsize and column size
gvn_arr = np.arange(16).reshape(4,4) 
# Print the given array
print("The given array\n", gvn_arr)
# Pass the given array and axis=1 as arguments to the argmax() function 
# to get the index of the maximum element for each row in the above given matrix
# and print it.
print("The index of the maximum element for each row in the above given matrix:")
print(np.argmax(gvn_arr, axis=1))

Output:

The given array
[[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]
 [12 13 14 15]]
The index of the maximum element for each row in the above given matrix:
[3 3 3 3]

3) Python code to fetch maximum element for a particular column

Now, we must add the argument axis=0 to the argmax() function.
As an output, this method returns the index of the maximum element across each column.

# Import numpy module using the import keyword
import numpy as np
# Pass some random size(rowsize*colsize) to the arange() function and apply the reshape() 
# function by passing arguments rowsize and column size
gvn_arr = np.arange(16).reshape(4,4) 
# Print the given array
print("The given array\n", gvn_arr)
# Pass the given array and axis=0 as arguments to the argmax() function 
# to get the index of the maximum element for each column in the above given matrix
# and print it.
print("The index of the maximum element for each column in the above given matrix:")
print(np.argmax(gvn_arr, axis=0))

Output:

The given array
[[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]
 [12 13 14 15]]
The index of the maximum element for each column in the above given matrix:
[3 3 3 3]