Numpy argmax() function returns indices of the max element of the array in a particular axis.

**np.argmax**

The np.argmax() is a built-in Numpy function that is used to get the indices of the maximum element from an array (single-dimensional array) or any row or column (multidimensional array) of any given array.

**Syntax**

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

**Parameters**

The np.argmax() function takes two arguments as a parameter:

**arr**: The array from which we want the indices of the max element.**axis**: By default, it is None. But for the multidimensional array, if we’re going to find an index of any maximum of element row-wise or column-wise, then we have to give axis=1 or axis=0, respectively.**out**: This is an optional parameter. This provides a feature to insert output to the array with an appropriate shape and dtype.

**Return Value**

Python NumPy argmax() function returns an array of the same shape of the given array containing the indices of the maximum elements.

**Finding the index of the maximum element from a 1D array**

See the following code.

#Importing numpy import numpy as np #We will create an 1D array arr = np.array([4, 24, 3, 12, 4, 4]) #Printing the array print("The array is: ", arr) #Shape of the array print("Shape of the array is : ", np.shape(arr)) #Now we will print index of max value of this array print("Index of value of the given array is: ", np.argmax(arr))

**Output**

The array is: [ 4 24 3 12 4 4] Shape of the array is : (6,) Index of value of the given array is: 1

**Explanation**

In this program, we have first declared an array with some random numbers given by the user.

Then we have printed the shape (size) of the array.

Then we have called argmax() to get the index of the maximum element from the array. We can see that the maximum element of this array is 14, which is at position 1, so the output is 1.

**Finding indices of maximum elements from Multidimensional Array**

#Importing numpy import numpy as np #We will create a 2D array #Of shape 4x3 arr = np.array([(14, 29, 34), (42, 5, 46), (1, 38, 44), (5, 16, 52)]) #Printing the array print("The array is: ") print(arr) print("Shape of the array is: ", np.shape(arr)) #Now we will find the indices of maximum value for some cases #Indices of maximum value of each row a = np.argmax(arr, axis=1) print("Index of maximum value of each row of the array is: ", a) #Indices of ,aximum value of each column b = np.argmax(arr, axis=0) print("Index of maximum value of each column of the array is: ", b)

**Output**

The array is: [[14 29 34] [42 5 46] [ 1 38 44] [ 5 16 52]] Shape of the array is: (4, 3) Index of maximum value of each row of the array is: [2 2 2 2] Index of maximum value of each column of the array is: [1 2 3]

**Explanation**

In the above program, we have first declared the matrix of size 4×3, and you can see the shape of the matrix also, which is (4,3).

Then we have called argmax() to get the output of different cases.

In the first case, we have passed *arr and ***axis=1,** which returns an array of size 4 containing indices of all the maximum elements from each row. In the second case, we have passed *arr and axis=0*, which returns an array of size 3 contain.

**Use np.arange() function to create an array and then use np argmax() function**

Let’s use the numpy arange() function to create a two-dimensional array and find the index of the maximum value of the array.

# app.py import numpy as np data = np.arange(8).reshape(2, 4) print(data) maxValIndex = np.argmax(data) print('The index of maxium array value is: ') print(maxValIndex)

**Output**

python3 app.py [[0 1 2 3] [4 5 6 7]] The index of maxium array value is: 7

**Multiple occurrences of the maximum values**

In the case of multiple occurrences of the maximum values, the indices corresponding to the first occurrence are returned.

See the following code.

# app.py import numpy as np data = [[21, 11, 10], [18, 21, 19]] print(data) maxValIndex = np.argmax(data) print('The index of maxium array value is: ') print(maxValIndex)

**Output**

python3 app.py [[21, 11, 10], [18, 21, 19]] The index of maxium array value is: 0

In the above example, the maximum value is** 21,** but it is found two times in the array.

So, it will return the index of the first occurrence. In our case, the index is 0.

Let’s find the maximum value along a given axis.

See the following code.

# app.py import numpy as np data = [[21, 11, 10], [18, 21, 19]] print(data) maxValIndex = np.argmax(data, axis=0) print('The index of maxium array value is: ') print(maxValIndex)

**Output**

python3 app.py [[21, 11, 10], [18, 21, 19]] The index of maxium array value is: [0 1 1]

In the above code, we are checking the maximum element along with the x-axis.

Our output is [0, 1, 1] that means 21 > 18, so it returns 0 because index of 21 is 0.

Then 11 < 21 means the index of 21 had returned, which is 1.

Then 10 < 19, which means the index of 19 had returned, which is 1.

That’s it for this tutorial.