Numpy

Numpy Argmax : How to get argmax element in numpy array stepwise ?

Numpy argmax function returns the indices of the maximum element of NumPy array axis wise. If the axis is None, It gives indices of max in the array. Well, This article will introduce the NumPy argmax with syntax and Implementation.

NumPy argmax : How to use it?

In this section firstly, we will implement the argmax() function. But before this, We have a prerequisite of numpy array.

Prerequisites :

Let’s create a NumPy array with some elements. Here is the piece of the code.

import numpy as np
arr = np.array([[1, 12, 9], [41, 15, 23],[43, 55, 98]])

1. argmax with axis=None :

If we provide the axis =None or ignore the axis parameter. We will get the indices of the max element in NumPy array. Let’s invoke this function.

import numpy as np
arr = np.array([[1, 12, 9], [41, 15, 23],[43, 55, 98]])
np.argmax(arr)

We can also use add axis=None like below. It will not impact anywhere.

np.argmax(arr,axis=None)
argmax with axis=None

Here we have the max element at the 8th indices of the NumPy array. Which is the output for the function.

2. Argmax with axis=0 :

Here axis =0 means, It will show the max element column-wise. Let’s implement it.

import numpy as np
arr = np.array([[1, 12, 9], [41, 15, 23],[43, 55, 98]])
np.argmax(arr,axis=0)
argmax with axis=0

Here in each column, the max element is at indices 2. That’s why it returns [2,2,2]. Because the element 41, 55,98 are the max element in each column.

3.Argmax with axis=1 :

Here everything else will be the same only it will return the max element indices row-wise.

import numpy as np
arr = np.array([[1, 12, 9], [41, 15, 23],[43, 55, 98]])
np.argmax(arr,axis=1)
argmax with axis=1

 

As in the above example, the row-wise (12,41,98) elements are maximum respectively. These elements are found are at [1,0,2] indices. Hence the above function returns the same output for the above code.

Conclusion –

Numpy is really essential in the data processing. NumPy argmax is an implicit function of NumPy python module. As we have seen this function argmax returns the max element axis wise. I hope this article is enough for knowing argmax function. Still, if you think, anything should be the part of this article. Please let us know.

 

Thanks 

Data Science Learner Team