{"id":8650,"date":"2021-06-12T10:55:54","date_gmt":"2021-06-12T05:25:54","guid":{"rendered":"https:\/\/python-programs.com\/?p=8650"},"modified":"2021-11-22T18:53:29","modified_gmt":"2021-11-22T13:23:29","slug":"find-max-value-its-index-in-numpy-array-numpy-amax","status":"publish","type":"post","link":"https:\/\/python-programs.com\/find-max-value-its-index-in-numpy-array-numpy-amax\/","title":{"rendered":"Find max value & its index in Numpy Array | numpy.amax()"},"content":{"rendered":"
In this article we will discuss about how we can find max value and the index of that max value in Numpy array using Parameters :<\/strong><\/p>\n (Default: Returns the array of max values)<\/p>\n Let’s see one by one how to find it in 1D and 2D Numpy array.<\/p>\n Let\u2019s create a 1D numpy array from a list given below and find the maximum values and its index<\/p>\n To find the maximum value in the array, we can use To get the index of the max value in the array, we will we have to use the CODE:<\/p>\n Here, when we called the where( ) function, it returned us with a tuple of\u00a0 arrays containing the indices of all the\u00a0 values that follow our conditions.<\/p>\n We will use the following 2D array to demonstrate.<\/p>\n {50,59,54}<\/em><\/p>\n {45,46,78}<\/em><\/p>\n {98,20,24}<\/em><\/p>\n 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.<\/p>\n CODE:<\/p>\n Column or Row wise value<\/strong><\/p>\n To find the max value per each row, we can\u00a0 pass axis =1 and for columns we can use axis =0.<\/p>\n CODE:<\/p>\n CODE:<\/p>\n <\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" 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 …<\/p>\nnumpy.amx()<\/code>.<\/p>\n
numpy.amax( ) :<\/h3>\n
Syntax-numpy.amax(arr, axis=None, out=None, keepdims=<no value>, initial=<no value>)<\/pre>\n
\n
\n
\n
<\/a>Maximum value & its index in a 1D Numpy Array:<\/h3>\n
Find maximum value:<\/h4>\n
numpy.amax( )<\/code> function and pass the array as function to it.<\/p>\n
[10,5,19,56,87,96,74,15,50,12,98]<\/pre>\n
import numpy as np\r\n\r\n# Finding the maximum value inside an array using amax( )\r\narr = np.array([10, 5, 19, 56, 87, 96, 74, 15, 50, 12, 98])\r\nmaxElem = np.amax(arr)\r\nprint(\"Max element : \", maxElem)\r\n<\/pre>\n
Output :\r\nMax element :\u00a0 98<\/pre>\n
Find index of maximum value :<\/h4>\n
where( )<\/code> function from the numpy library.<\/p>\n
import numpy as np\r\n\r\n# Index of the maximum element\r\narr = np.array([10, 5, 19, 56, 87, 96, 74, 15, 50, 12, 98])\r\nmaxElem = np.amax(arr)\r\nprint(\"Max element : \", maxElem)\r\nres = np.where(arr == np.amax(arr))\r\nprint(\"Returned result :\", res)\r\nprint(\"List of Indices of maximum element :\", res[0])\r\n<\/pre>\n
Output :\r\nMax element :\u00a0 98\r\nReturned result\u00a0 : (array([10], dtype=int32),)\r\nList of Indices of maximum element : [10]<\/pre>\n
<\/a>Find maximum value & its index in a 2D Numpy Array<\/h3>\n
Find max value in complete 2D numpy array :<\/h4>\n
import numpy as np\r\n\r\narr = np.array([[50, 59, 54], [45, 46, 78], [98, 20, 24]])\r\n# Get the maximum value from the 2D array\r\n\r\nmaxValue = np.amax(arr)\r\nprint(\"The maximum value inside the array is\", maxValue)\r\n<\/pre>\n
Output :\r\nThe maximum value inside the array is 98<\/pre>\n
import numpy as np\r\n\r\narr = np.array([[50, 59, 54], [45, 46, 78], [98, 20, 24]])\r\n# Get the maximum valuevin rows\r\nmaxRows = np.amax(arr, axis=1)\r\n# Get the maximum valuevin columns\r\nmaxColumns = np.amax(arr, axis=0)\r\n\r\nprint(\r\n \"The maximum values in rows are : \",\r\n maxRows,\r\n \" and the maximum value in columns are : \",\r\n maxColumns,\r\n)\r\n<\/pre>\n
Output :\r\nThe maximum values in rows are :\u00a0 [59 78 98]\u00a0 and the maximum value in columns are :\u00a0 [98 59 78]<\/pre>\n
Find index of maximum value from 2D numpy array:<\/h4>\n
import numpy as np\r\n\r\narr = np.array([[50, 59, 54], [45, 46, 78], [98, 20, 24]])\r\n# Get the index of max value inside the 2D array\r\nres = np.where(arr == np.amax(arr))\r\nprint(\"Tuple :\", res)\r\nprint(\"Now Coordinates of max value in 2D array :\")\r\n# zipping both the arrays to find the coordinates\r\nCoordinates = list(zip(res[0], res[1]))\r\n\r\nfor elem in Coordinates:\r\n print(elem)\r\n\r\n<\/pre>\n
Output :\r\nTuple : (array([2], dtype=int32), array([0], dtype=int32))\r\nNow Coordinates Of max value in 2D array :\r\n(2, 0)<\/pre>\n
<\/a>numpy.amax() & NaN :<\/h3>\n
amax( )<\/code> 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.<\/p>\n
import numpy as np\r\n\r\narr = np.array([[50, 59, np.NaN], [45, 46, 78], [98, 20, 24]])\r\n# amax( ) propagating the NaN values\r\n\r\nprint(\"The max element in the numpy array is :\", np.amax(arr))\r\n<\/pre>\n
Output :\r\nThe max element in the numpy array is : nan<\/pre>\n