Finding max value and it’s index in Numpy Array
In this article we will discuss about how we can find max value and the index of that max value in Numpy array using numpy.amx()
.
numpy.amax( ) :
Syntax-numpy.amax(arr, axis=None, out=None, keepdims=<no value>, initial=<no value>)
Parameters :
- arr: Numpy array
- axis: This is an optional parameter unless provided flattens the array.
(Default: Returns the array of max values)
- Axis = 0: Returns array containing max values of each columns
- Axis = 1: Returns array containing max values of each rows
Let’s see one by one how to find it in 1D and 2D Numpy array.
- Maximum value & its index in a 1D Numpy Array
- Find maximum value & its index in a 2D Numpy Array
- numpy.amax() & NaN
Maximum value & its index in a 1D Numpy Array:
Let’s create a 1D numpy array from a list given below and find the maximum values and its index
Find maximum value:
To find the maximum value in the array, we can use numpy.amax( )
function and pass the array as function to it.
[10,5,19,56,87,96,74,15,50,12,98]
import numpy as np # Finding the maximum value inside an array using amax( ) arr = np.array([10, 5, 19, 56, 87, 96, 74, 15, 50, 12, 98]) maxElem = np.amax(arr) print("Max element : ", maxElem)
Output : Max element :Â 98
Find index of maximum value :
To get the index of the max value in the array, we will we have to use the where( )
function from the numpy library.
CODE:
import numpy as np # Index of the maximum element arr = np.array([10, 5, 19, 56, 87, 96, 74, 15, 50, 12, 98]) maxElem = np.amax(arr) print("Max element : ", maxElem) res = np.where(arr == np.amax(arr)) print("Returned result :", res) print("List of Indices of maximum element :", res[0])
Output : Max element : 98 Returned result : (array([10], dtype=int32),) List of Indices of maximum element : [10]
Here, when we called the where( ) function, it returned us with a tuple of arrays containing the indices of all the values that follow our conditions.
Find maximum value & its index in a 2D Numpy Array
We will use the following 2D array to demonstrate.
{50,59,54}
{45,46,78}
{98,20,24}
Find max value in complete 2D numpy array :
When we are going to find the max value in a 2D numpy array, we can either do it by finding a single value or we can find column wise or row wise.
CODE:
import numpy as np arr = np.array([[50, 59, 54], [45, 46, 78], [98, 20, 24]]) # Get the maximum value from the 2D array maxValue = np.amax(arr) print("The maximum value inside the array is", maxValue)
Output : The maximum value inside the array is 98
Column or Row wise value
To find the max value per each row, we can pass axis =1 and for columns we can use axis =0.
CODE:
import numpy as np arr = np.array([[50, 59, 54], [45, 46, 78], [98, 20, 24]]) # Get the maximum valuevin rows maxRows = np.amax(arr, axis=1) # Get the maximum valuevin columns maxColumns = np.amax(arr, axis=0) print( "The maximum values in rows are : ", maxRows, " and the maximum value in columns are : ", maxColumns, )
Output : The maximum values in rows are :Â [59 78 98]Â and the maximum value in columns are :Â [98 59 78]
Find index of maximum value from 2D numpy array:
CODE:
import numpy as np arr = np.array([[50, 59, 54], [45, 46, 78], [98, 20, 24]]) # Get the index of max value inside the 2D array res = np.where(arr == np.amax(arr)) print("Tuple :", res) print("Now Coordinates of max value in 2D array :") # zipping both the arrays to find the coordinates Coordinates = list(zip(res[0], res[1])) for elem in Coordinates: print(elem)
Output : Tuple : (array([2], dtype=int32), array([0], dtype=int32)) Now Coordinates Of max value in 2D array : (2, 0)
numpy.amax() & NaN :
amax( )
also propagates the NaN values , which means if there is a NaN value present in the numpy array, then the max value returned by the function will be NaN.
import numpy as np arr = np.array([[50, 59, np.NaN], [45, 46, 78], [98, 20, 24]]) # amax( ) propagating the NaN values print("The max element in the numpy array is :", np.amax(arr))
Output : The max element in the numpy array is : nan