numpy.amin() | Find minimum value in Numpy Array and it’s index

In this article we will discuss how to find the minimum or smallest value in a Numpy array and it’s indices using numpy.amin().

numpy.amin()

Python’s numpy module provides a function to get the minimum value from a Numpy array i.e.

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

Arguments :

  • a : numpy array from which it needs to find the minimum value.
  • axis : It’s optional and if not provided then it will flattened the passed numpy array and returns the min value in it.
    • If it’s provided then it will return for array of min values along the axis i.e.
    • If axis=0 then it returns an array containing min value for each columns.
    • If axis=1 then it returns an array containing min value for each row.

Let’s look in detail,

Find minimum value & it’s index in a 1D Numpy Array:

Let’s create a 1D numpy array from a list i.e.

arr = numpy.array([11, 12, 13, 14, 15, 16, 17, 15, 11, 12, 14, 15, 16, 17])

Find minimum value:

Advertisements

Now let’s use numpy.amin() to find the minimum value from this numpy array by passing just array as argument i.e.

# Get the minimum element from a Numpy array
minElement = numpy.amin(arr)

print('Minimum element from Numpy Array : ', minElement)

Output:
Minimum element from Numpy Array :  11

It returns the minimum value from the passed numpy array i.e. 11

Find index of minimum value :

Get the array of indices of minimum value in numpy array using numpy.where() i.e.

# Get the indices of minimum element in numpy array
result = numpy.where(arr == numpy.amin(arr))

print('Returned tuple of arrays :', result)
print('List of Indices of minimum element :', result[0])

Output:
Returned result  : (array([0, 8], dtype=int32),)
List of Indices of minimum element : [0 8]

In numpy.where() when we pass the condition expression only then it returns a tuple of arrays (one for each axis) containing the indices of element that satisfies the given condition. As our numpy array has one axis only therefore returned tuple contained one array of indices.

Find minimum value & it’s index in a 2D Numpy Array

Let’s create a 2D numpy array i.e.

# Create a 2D Numpy array from list of lists
arr2D = numpy.array([[11, 12, 13],
                     [14, 15, 16],
                     [17, 15, 11],
                     [12, 14, 15]])

Contents of the 2D numpy array are,
[[11 12 13]
 [14 15 16]
 [17 15 11]
 [12 14 15]]

Find min value in complete 2D numpy array

To find minimum value from complete 2D numpy array we will not pass axis in numpy.amin() i.e.

# Get the minimum value from complete 2D numpy array
minValue = numpy.amin(arr2D)

It will return the minimum value from complete 2D numpy arrays i.e. in all rows and columns.
11

Find min values along the axis in 2D numpy array | min in rows or columns:

If we pass axis=0 in numpy.amin() then it returns an array containing min value for each column i.e.

# Get the minimum values of each column i.e. along axis 0
minInColumns = numpy.amin(arr2D, axis=0)

print('min value of every column: ', minInColumns)

Output:
min value of every column:  [11 12 11]

If we pass axis = 1 in numpy.amin() then it returns an array containing min value for each row i.e.
# Get the minimum values of each row i.e. along axis 1
minInRows = numpy.amin(arr2D, axis=1)

print('min value of every Row: ', minInRows)

Output:
min value of every Row:  [11 14 11 12]

Find index of minimum value from 2D numpy array:

Contents of the 2D numpy array arr2D are,

[[11 12 13]
 [14 15 16]
 [17 15 11]
 [12 14 15]]

Let’s get the array of indices of minimum value in 2D numpy array i.e.
# Find index of minimum value from 2D numpy array
result = numpy.where(arr2D == numpy.amin(arr2D))

print('Tuple of arrays returned : ', result)

print('List of coordinates of minimum value in Numpy array : ')
# zip the 2 arrays to get the exact coordinates
listOfCordinates = list(zip(result[0], result[1]))
# travese over the list of cordinates
for cord in listOfCordinates:
    print(cord)

Output:
Tuple of arrays returned :  (array([0, 2], dtype=int32), array([0, 2], dtype=int32))
List of coordinates of minimum value in Numpy array : 
(0, 0)
(2, 2)

numpy.amin() & NaN

numpy.amin() propagates the NaN values i.e. if there is a NaN in the given numpy array then numpy.amin() will return NaN as minimum value. For example,

arr = numpy.array([11, 12, 13, 14, 15], dtype=float)
arr[3] = numpy.NaN

print('min element from Numpy Array : ', numpy.amin(arr))

Output:
min element from Numpy Array :  nan

If you want to ignore the NaNs while finding the min values from numpy then use numpy.nanmin() instead.

Complete example is as follows,

import numpy


def main():
    # Create a Numpy array from a list
    arr = numpy.array([11, 12, 13, 14, 15, 16, 17, 15, 11, 12, 14, 15, 16, 17])

    print('Contents of Numpy array : ', arr, sep='\n')

    print("*** Get minimum element from a 1D numpy array***")

    # Get the minimum element from a Numpy array
    minElement = numpy.amin(arr)
    print('min element from Numpy Array : ', minElement)

    print("*** Get the indices of minimum element from a 1D numpy array***")

    # Get the indices of minimum element in numpy array
    result = numpy.where(arr == numpy.amin(arr))
    print('Returned result  :', result)
    print('List of Indices of minimum element :', result[0])

    print("*** Get minimum element from a 2D numpy array***")

    # Create a 2D Numpy array from list of lists
    arr2D = numpy.array([[11, 12, 13],
                         [14, 15, 16],
                         [17, 15, 11],
                         [12, 14, 15]])

    print('Contents of 2D Numpy Array', arr2D, sep='\n')

    # Get the minimum value from complete 2D numpy array
    minValue = numpy.amin(arr2D)

    print('min value from complete 2D array : ', minValue)

    # Get the minimum values of each column i.e. along axis 0
    minInColumns = numpy.amin(arr2D, axis=0)

    print('min value of every column: ', minInColumns)

    # Get the minimum values of each row i.e. along axis 1
    minInRows = numpy.amin(arr2D, axis=1)

    print('min value of every Row: ', minInRows)

    print('*** Get the index of minimum value in 2D numpy array ***')

    # Find index of minimum value from 2D numpy array
    result = numpy.where(arr2D == numpy.amin(arr2D))

    print('Tuple of arrays returned : ', result)

    print('List of coordinates of minimum value in Numpy array : ')
    # zip the 2 arrays to get the exact coordinates
    listOfCordinates = list(zip(result[0], result[1]))
    # travese over the list of cordinates
    for cord in listOfCordinates:
        print(cord)

    print('*** numpy.amin() & NaN ***')
    arr = numpy.array([11, 12, 13, 14, 15], dtype=float)
    arr[3] = numpy.NaN

    print('min element from Numpy Array : ', numpy.amin(arr))


if __name__ == '__main__':
    main()


Output
Contents of Numpy array : 
[11 12 13 14 15 16 17 15 11 12 14 15 16 17]
*** Get minimum element from a 1D numpy array***
min element from Numpy Array :  11
*** Get the indices of minimum element from a 1D numpy array***
Returned result  : (array([0, 8], dtype=int32),)
List of Indices of minimum element : [0 8]
*** Get minimum element from a 2D numpy array***
Contents of 2D Numpy Array
[[11 12 13]
 [14 15 16]
 [17 15 11]
 [12 14 15]]
min value from complete 2D array :  11
min value of every column:  [11 12 11]
min value of every Row:  [11 14 11 12]
*** Get the index of minimum value in 2D numpy array ***
Tuple of arrays returned :  (array([0, 2], dtype=int32), array([0, 2], dtype=int32))
List of coordinates of minimum value in Numpy array : 
(0, 0)
(2, 2)
*** numpy.amin() & NaN ***
min element from Numpy Array :  nan

 

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top