Find min value Index in NumPy array

Numpy

In this post, we are going to learn how to Find the min value Index in the NumPy array by using the numpy.amin() or numpy.argmin() function with Python programs examples.

NumPy.amin() function


The numpy. amin() function help us to get the minimum value from the numpy array along an axis.

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

Parameters

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

1. Find minimum value in 1D array


In this Python program example, we have used numpy.amin() function to get minimum value by passing numpy array as an argument. We can use the np. where() function with amin() function to get the indices of min values that returns tuples of the array that contain indices(one for each axis), wherever min 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])

#min values in numpy array
minval = np.amin(nparr)


print('min value in array:',minval)

indice = np.where(nparr == np.amin(nparr))

print('min value index tuple of array :',indice)


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


Output

min value in array: 3
min value index tuple of array : (array([0], dtype=int32),)
min value index: [0]

2. Find min value index in 2D NumPy array


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

  • In which the first array tuples contain row-wise indices for min values
  • The second array tuple for column-wise indices for min 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]])


minvalInCols = np.amin(nparr, axis=0)
print('min values in columns:',minvalInCols)

minvalInRows = np.amin(nparr, axis=1)
print('min values in Rows:',minvalInRows)

#rows-columns min values indices
index = np.where(nparr == np.amin(nparr))
print('indices array :',index)


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

Output

min values in columns: [3 6 3]
min values in Rows: [3 9 3 6]
indices array : (array([0, 2], dtype=int32), array([0, 2], dtype=int32))

 indices of min vaues: (0, 0)

 indices of min vaues: (2, 2)

3. numpy argmin() 2D to Find min value index column-wise


In this Python example, we have used numpy.amin() function with axis=0 to get all the min values column-wise.To get the indices of all min values column-wise, We have used the np.argmin() 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 min values
min_val_colwise = np.amin(nparr, axis=0)

print('min val column-wise:',min_val_colwise )
 
#column wise min values indexes
minVal_Index_colwise = np.argmin(nparr, axis=0)

print('min val index colwise:',minVal_Index_colwise)

Output

min val column-wise: [3 6 3]
min val index colwise: [0 0 2]

4. numpy argmin 2D to Find Min value index row-wise


In this Python program example, we have used amin() function with axis=1 to get all the min values row-wise.To get the indices of all min values row-wise, We have used the np.argmin() function that accepts a two-argument numpy array along with the axis.

import numpy as np
 
 
nparr = np.array([[3, 6, 9],
                [12, 9, 18],
                [21,9, 3],
                [6,9 , 12]])
 
 
 
 
 
#row wise min values
min_val_rowwise = np.amin(nparr, axis=1)

print('min val row-wise:',min_val_rowwise )
 
#row wise min values indexes
minVal_Index_rowwise = np.argmin(nparr, axis=1)

print('min val index rowwise:',minVal_Index_rowwise)
 

Output

min val row-wise: [3 9 3 6]
min val index rowwise: [0 1 2 0]

5. NumPy min() with NAN value


The NumPy amin() function returns NAN values as the minimum 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)

minVal_Index = np.argmin(nparr)
print('min value in array: ', np.amin(nparr))
print('min value index: ', minVal_Index)

Output

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