Find max value index in NumPy array

Numpy

In this post, we are going to learn how to find the max value index in the NumPy array.The numpy.amax() and np.argmax() function row-wise and column-wise by the help of Python program.

numpy.amax( )


The NumPy module amax() function returns the maximum value of the numpy array or maximum value along an axis.

Syntax

numpy.amax(a, axis=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)

Parameters

  • array: The numpy array in which maximum value need to find.
  • axis : The optional parameter,if not provided it flatten the array and returns the max value.
    • axis =0 returns an array that contain max value for each columns.
    • axis=1 returns an array that contain max value for each rows.

1. Find max value Index in 1D NumPy array


In this Python program example, we have used numpy.amax() function to get maximum value by passing a numpy array as an argument. The np. where() function To get the indices of max values that returns tuples of the array that contain indices(one for each axis), wherever max value exists. We can access indices by using indices[0].

import numpy as np


nparr = np.array([3,6,9,12,15,18,21,24,27,30,9,9,9])

indice = np.where(nparr == np.amax(nparr))
print('max value index:',indice)


print('max value index:',indice[0])

Output

indices: (array([9], dtype=int32),)
max value index: [9]

2. Find max value index in 2D NumPy array


In this Python program example,we are finding max value in 2D NumPy array.numpy.amax() return the max value in 2D array.The numpy.where(condition) will return a tuple of two arrays indexes of max values.

  • In which the first array tuples contain row-wise indices for max values.
  • The second array tuple for column-wise indices for max values.
  • The zip() function to zip both arrays to get all indices row-wise and column-wise.
  • The for loop to iterate over the zip arrays.
import numpy as np


nparr = np.array([[3, 6, 9],
                [12, 9, 18],
                [21,9, 3],
                [6,9 , 12]])


maxvalInCols = np.amax(nparr, axis=0)
print('max values colwise:',maxvalInCols)

maxvalInRows = np.amax(nparr, axis=1)
print('max values Rowswise:',maxvalInRows)

#rows-columns max values indices
index = np.where(nparr == np.amax(nparr))
print(index)


listofIndices = list(zip(index[0], index[1]))
for indexes in listofIndices:
    print('indices of max vaues:',indexes)

Output

max values colwise : [21  9 18]
max values Rowswise: [ 9 18 21 12]
(array([2], dtype=int32), array([0], dtype=int32))
indices of max vaues: (2, 0)

3. numpy argmax() 2D to Find max value index column-wise


In this Python example, we have used numpy.amax() function with axis=0 to get all the max values column-wise.To get the indices of all max values column-wise, We have used np. argmax() function that accepts two arguments numpy array along with axis.

import numpy as np
 
 
nparr = np.array([[3, 6, 9],
                [12, 9, 18],
                [21,9, 3],
                [6,9 , 12]])
 
 
 
 
 
#column wise max values and indexes
max_val_colwise = np.amax(nparr, axis=0)

print('max val column-wise:',max_val_colwise)
 
#column wise max values indexes
maxVal_Index_colwise = np.argmax(nparr, axis=0)

print('max val index colwise:',maxVal_Index_colwise)


Output

max val column-wise: [21  9 18]
max val index colwise: [2 1 1]

4. numpy argmax 2D to Find Max value index row-wise


In this Python example, we have used amax() function with axis=1 to get all the max values row-wise.To get the indices of all max values row-wise, We have used the np.argmax() function that accepts a two-dimensional numpy array along with an axis.

import numpy as np
 
 
nparr = np.array([[3, 6, 9],
                [12, 9, 18],
                [21,9, 3],
                [6,9 , 12]])

#row wise max values and indexes
maxVal_ind_rowise = np.argmax(nparr, axis=1)
 
print('max val rowise:',max_val_Rowwise)


#row wise max values indexes
max_val_Rowwise = np.amax(nparr, axis=1)
 
print('max val index rowise:',maxVal_ind_rowise)

 

Output

max val index rowise: [ 9 18 21 12]
max val index rowise: [2 2 0 2]

5. NumPy max() with NAN value


The NumPy amax() function returns NAN values as the maximum value in the case of a numpy array containing NAN values. Let us understand with an example.

import numpy as np



nparr = np.array([3, 6, 9, 12, 15,np.nan], dtype=float)

print(nparr)

maxVal_Index = np.argmax(nparr)
print('max value in array: ', np.amax(nparr))
print('max value index: ', maxVal_Index)

Output

[ 3.  6.  9. 12. 15. nan]
max value in array:  nan
max value index:  5

Summary

In this post we have learned how to how to Find max value index in NumPy array by using numpy.amax() and np.argmax() function