Finding max value and it’s index in Numpy Array
Get index of max value in numpy array python: 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:
Numpy.amax: Let’s create a 1D numpy array from a list given below and find the maximum values and its index
Find maximum value:
Numpy find max index: 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
- np.delete(): Remove items/rows/columns from Numpy Array | How to Delete Rows/Columns in a Numpy Array?
- Count occurrences of a value in NumPy array in Python | numpy.count() in Python
- Python Programming – NumPy
Find index of maximum value :
np amax: To get the index of the max value in the array, we will 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
Numpy max value in 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 :
Numpy get index of max: 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
Numpy max 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