In this article we will discuss how to get the maximum / largest value in a Numpy array and its indices using numpy.amax().
numpy.amax()
Python’s numpy module provides a function to get the maximum value from a Numpy array i.e.
numpy.amax(a, axis=None, out=None, keepdims=<no value>, initial=<no value>)
Arguments :
- a : numpy array from which it needs to find the maximum value.
- axis : It’s optional and if not provided then it will flattened the passed numpy array and returns the max value in it.
- If it’s provided then it will return for array of max values along the axis i.e.
- If axis=0 then it returns an array containing max value for each columns.
- If axis=1 then it returns an array containing max value for each row.
Let’s look in detail,
Find maximum value & its 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 maximum value:
Frequently Asked:
- Check if any value in Numpy Array is negative in Python
- Check if all elements in NumPy Array are False
- What is a Structured Numpy Array and how to create and sort it in Python?
- Check if 2D NumPy Array or Matrix is Symmetric
Now let’s use numpy.amax() to find the maximum value from this numpy array by passing just array as argument i.e.
# Get the maximum element from a Numpy array maxElement = numpy.amax(arr) print('Max element from Numpy Array : ', maxElement)
Output:
Max element from Numpy Array : 17
It returns the maximum value from the passed numpy array i.e. 17
Find index of maximum value :
Get the array of indices of maximum value in numpy array using numpy.where() i.e.
# Get the indices of maximum element in numpy array result = numpy.where(arr == numpy.amax(arr)) print('Returned tuple of arrays :', result) print('List of Indices of maximum element :', result[0])
Output:
Returned tuple of arrays : (array([ 6, 13], dtype=int32),) List of Indices of maximum element : [ 6 13]
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 maximum value & its 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 max value in complete 2D numpy array
To find maximum value from complete 2D numpy array we will not pass axis in numpy.amax() i.e.
# Get the maximum value from complete 2D numpy array maxValue = numpy.amax(arr2D)
It will return the maximum value from complete 2D numpy arrays i.e. in all rows and columns.
17
Find max values along the axis in 2D numpy array | max in rows or columns:
If we pass axis=0 in numpy.amax() then it returns an array containing max value for each column i.e.
# Get the maximum values of each column i.e. along axis 0 maxInColumns = numpy.amax(arr2D, axis=0) print('Max value of every column: ', maxInColumns)
Output:
Max value of every column: [17 15 16]
If we pass axis = 1 in numpy.amax() then it returns an array containing max value for each row i.e.
# Get the maximum values of each row i.e. along axis 1 maxInRows = numpy.amax(arr2D, axis=1) print('Max value of every Row: ', maxInRows)
Output:
Max value of every Row: [13 16 17 15]
Find index of maximum 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 maximum value in 2D numpy array i.e.
# Find index of maximum value from 2D numpy array result = numpy.where(arr2D == numpy.amax(arr2D)) print('Tuple of arrays returned : ', result) print('List of coordinates of maximum 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([2], dtype=int32), array([0], dtype=int32)) List of coordinates of maximum value in Numpy array : (2, 0)
numpy.amax() & NaN
numpy.amax() propagates the NaN values i.e. if there is a NaN in the given numpy array then numpy.amax() will return NaN as maximum value. For example,
arr = numpy.array([11, 12, 13, 14, 15], dtype=float) arr[3] = numpy.NaN print('Max element from Numpy Array : ', numpy.amax(arr))
Output:
Max element from Numpy Array : nan
If you want to ignore the NaNs while finding the max values from numpy then use numpy.nanmax() 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 Maximum element from a 1D numpy array***") # Get the maximum element from a Numpy array maxElement = numpy.amax(arr) print('Max element from Numpy Array : ', maxElement) print("*** Get the indices of maximum element from a 1D numpy array***") # Get the indices of maximum element in numpy array result = numpy.where(arr == numpy.amax(arr)) print('Returned result :', result) print('List of Indices of maximum element :', result[0]) print("*** Get Maximum 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 maximum value from complete 2D numpy array maxValue = numpy.amax(arr2D) print('Max value from complete 2D array : ', maxValue) # Get the maximum values of each column i.e. along axis 0 maxInColumns = numpy.amax(arr2D, axis=0) print('Max value of every column: ', maxInColumns) # Get the maximum values of each row i.e. along axis 1 maxInRows = numpy.amax(arr2D, axis=1) print('Max value of every Row: ', maxInRows) print('*** Get the index of maximum value in 2D numpy array ***') # Find index of maximum value from 2D numpy array result = numpy.where(arr2D == numpy.amax(arr2D)) print('Tuple of arrays returned : ', result) print('List of coordinates of maximum 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.amax() & NaN ***') arr = numpy.array([11, 12, 13, 14, 15], dtype=float) arr[3] = numpy.NaN print('Max element from Numpy Array : ', numpy.amax(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 Maximum element from a 1D numpy array*** Max element from Numpy Array : 17 *** Get the indices of maximum element from a 1D numpy array*** Returned result : (array([ 6, 13], dtype=int32),) List of Indices of maximum element : [ 6 13] *** Get Maximum element from a 2D numpy array*** Contents of 2D Numpy Array [[11 12 13] [14 15 16] [17 15 11] [12 14 15]] Max value from complete 2D array : 17 Max value of every column: [17 15 16] Max value of every Row: [13 16 17 15] *** Get the index of maximum value in 2D numpy array *** Tuple of arrays returned : (array([2], dtype=int32), array([0], dtype=int32)) List of coordinates of maximum value in Numpy array : (2, 0) *** numpy.amax() & NaN *** Max element from Numpy Array : nan
Great, I love this explanation. Thanks so much