How to use numpy.argmax() in Python

In this tutorial, we will learn how to use numpy.argmax() in Python using a few simple examples.

Using numpy.argmax() in Python

In Python, numpy.argmax() returns the indices of the maximum element of any given array in a particular axis. Let us see how it works with a simple example.

First, we need to import the library numpy into python and declare an array on which we will perform the operations.

# import numpy
import numpy as np

# declare an  array
a = [[41,83,46],[8,90,56],[54,76,16]]

Once we have declared an array, we are ready to use the syntax. Here, the syntax numpy.argmax(a, axis) has two parameters array and axis. Here, array is already declared. As soon as we declare the axis parameter, the array gets divided into rows and columns. Then, numpy checks the rows and columns individually.

axis = 0 means that the operation is performed down the columns whereas, axis = 1  means that the operations is performed across the rows.

# Using np.argmax() syntax
b = np.argmax(a, axis=0)


[2 1 1]

So, here we see that by providing the argument axis = 0, np.argmax() returns the indices of maximum elements of every column in the array. Now, let’s check using the axis = 1 argument.

# Using np.argmax() syntax
b = np.argmax(a, axis=1)


[1 1 1]

As a result, we can see that this time, np.argmax() returns indices of the maximum element in every row of the given array.

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *