What if we only want to know the index of the min and max of certain dimensions? numpy.ndarray.max¶. ; axis: By default, it is None.But for the multidimensional array, if we're going to find an index of any maximum of element row-wise or column-wise, we have to give axis=1 or axis=0, respectively. NumPy argmax() is an inbuilt NumPy function that is used to get the indices of the maximum element from an array (single-dimensional array) or any row or column (multidimensional array) of any given array.. NumPy argmax() NumPy argmax() function returns indices of the max element of the array in a particular axis. To get the index of the max value in the array, we will we have to use the where ( ) function from the numpy library. When we use Numpy argmax, the function identifies the maximum value in the array. numpy array get a value from a 2D array Code Example For our case, you need to use the index 2 , 0 , and 1 , where '0' indicates the row 0 and '1' indicates the column 1 within the third two . in all rows and columns. It will return the minimum value from complete 2D numpy arrays i.e. get index of element in 2d array python. # Select row at index 1 from 2D array row = nArr2D[1] Contents of row : [11 22 33] Now modify the contents of row i.e. To find the maximum item index using the numpy library. maximum_element = numpy.max(arr, 0) maximum_element = numpy.max(arr, 1) If we use 0 it will give us a list containing the maximum or minimum values from each column. numpy two dimensional array get one dimensional. Python Program to get value of a dictionary given by index of maximum value of given key 18, Jul 21 Create a DataFrame from a Numpy array and specify the index column and column headers Python Numpy - Get Maximum Value of Array For example, consider that we have a 3D numpy array of shape (m, n, p). In the above code, we get the number of elements in the multi-dimensional array array with the numpy.size property in Python. The questions are of 4 levels of difficulties with L1 being the easiest to L4 being the hardest. find max index in array python | 5 Ways to Find the list ... Then, using the array() function, we shall pass the list my_list as an argument inside the numpy array. numpy.argmax() in Python - GeeksforGeeks You can also pass the axis argument ot this function, but there is no keepdims option.. find nearest value in numpy array: stackoverflow: Finding the nearest value and return the index of array in Python: stackoverflow: Numpy minimum in (row, column) format: stackoverflow: Numpy: get the column and row index of the minimum value of a 2D array: stackoverflow: numpy : argmin in multidimensional arrays: bytes.com: numpy.square: doc . Min In PythonHow to find the Index of value in Numpy Array ...Numpy Argmax, Explained - Sharp Sight minValue = numpy.amin(arr2D) It will return the minimum value from complete 2D numpy arrays i.e. ndarray. Pass the numpy array as argument to numpy.max(), and this function shall return the maximum value. From the output we can see that the value 8 first occurs in index position 4. Find the index of value in Numpy Array using numpy.where ... After that, we use the „numpy.sign" function on that array, hence we get the sign of the differences, i.e., -1 or 1. Refer to numpy.amax for full documentation. To get specific row of elements, access the numpy array with all the specific index values for other dimensions and : for the row of elements you would like to get. 16. In this program, we will discuss how to identify unique values from a 2-dimensional array in Python. To get the indices of the four largest elements, do. Here we get its first occurrence which is at index 1. The numpy.amax () is a Python function that returns the maximum of an array or maximum along the axis (if mentioned). The type of items in the array is specified by a separate data-type object (dtype), one of which is . Python Program. With argmin() function, we can search NumPy arrays and fetch the index of the smallest elements present in the array at a broader scale.It searches for the smallest value present in the array structure and returns the index of the same. This is the reason why this method is not suitable for multi-dimensional arrays. Numpy Argmax Identifies the Maximum Value and Returns the Associated Index. The index of the minimum value: 5 The value of the minimum: -1.6087688672846108 The index of the maximum value: 8 The value of the maximum: 0.9493540484559849. ; In this program, we have created duplicated values in a 2-dimensional array. In this example, we are going to use the np.unique() function and assign the axis=0 that signifies a direction along which to use the np.unique() function. But instead of retrieving the value, Numpy argmax retrieves the index that's associated with the maximum value. Python: Retrieve the Index Value of the Max Value in a List. Compare two arrays and returns a new array containing the element-wise maxima. The numpy argmin() function takes three arguments: arr: The array from which we want the indices of the min element. Pass the numpy array as argument to numpy.max(), and this function shall return the maximum value. Syntax I could find it by using ´for` loops, but maybe there is a better way. Also, check: Python NumPy 3d array Python numpy unique 2d array. Since you have passed an array [1,3,2] the maximum value is 3 which is at an array index of 1 or at position 1. By default, the index is into the flattened array, otherwise along the specified axis. This shall convert the given list into a numpy array and store it into 'n'. calculating averages, finding minimum or maximum values) across the entire array of values. Multi-dimensional array on a CUDA device. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆) 14. i want to write a function that gives the max value and its coordinates so to speak of the max value. Get Length of a NumPy Array With . Then we use the function "numpy.sign" on the array to get the sign of the differences, i.e., -1 or 1. This class implements a subset of methods of numpy.ndarray.The difference is that this class allocates the array content on the current GPU device. For example, islocalmin(A,'SamplePoints',t) finds local minima of A with respect to the time stamps contained in the time vector t. flatten() - Function Tutorial with examples; Find the index of value in Numpy Array using numpy. Thus, with the index, we can easily get the smallest element present in the array. answered Jul 9, 2019 by Vishal (106k points) For getting the indices of N maximum values in a NumPy array we have Newer NumPy versions (1.8 and up) that have a function called argpartition. Get Index of the Maximum Value of a List With the numpy.argmax() Function in Python. Hi Matt, The argmax () function returns the index of the maximum value in a given array. find position of index of 2d array python. Here, You can test the max value using the index returned, indices returned should look like (array([0], dtype=int64), array([2], dtype=int64)) when you print the indices. For a single-dimensional array, we can easily find the largest element, but for the multidimensional . 2D array are also called as Matrices which can be represented as collection of rows and columns.. It will find the lowest element and gives the output. # Select row at index 1 from 2D array row = nArr2D[1] Contents of row : [11 22 33] Now modify the contents of row i.e. Then, using the argmax() function, we shall print the index of the maximum . find index of 0 in 2d array python. Array is a linear data structure consisting of list of elements. CODE . axis int, optional. 17. Maximum index from a numpy array. We will make use of two of many functions provided by NumPy library to calculate the nearest value and the index in the array. You can find the maximum value in the entire array using the same numpy.max () method just like you have used in finding the max in 1D. Get a Row from Numpy Array. matlabs max function only does 1 dimension. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc. To find the maximum item index using the numpy library. As you can see, the index of the maximum value is 19. The numpy.argmax() function returns indices of the max element of the array in a particular axis.. Syntax : Get the Index of the Max Value in a NumPy Array 5.1.7. np.where: Replace Elements of a NumPy Array Based on a Condition 5.1.8. array-to-latex: Turn a NumPy Array into Latex 5.1.9. It seems you are using 2D array as index array and 3D array to select values. Find max . Only 0 or None are allowed. In this article, let's discuss finding the nearest value and the index in an array with NumPy. By default, the index given by argmin/max is into the flattened array. Numpy provides us with several built-in functions to create and work with arrays from scratch. 8/12/2016 100 numpy exercises http://www.labri.fr/perso/nrougier/teaching/numpy.100/index.html 1/13 100 numpy exercises A joint effort of the numpy community Index of element in 2D array. In this we are specifically going to talk about 2D arrays. First, we shall import the numpy library. Create a 2d array with 1 on the border and 0 inside (★☆☆) 16. To work with an array in Python, use the Numpy library. The np.amax () function takes four arguments which are arr, axis, out, and keepdims, and returns the maximum value of an array. DA: 45 PA: 88 MOZ Rank: 66. First, we shall import the numpy library. Find Unique Values & their first index position from a Numpy Array. This is because Python requires "a+=1" to be equivalent to "a = a + 1". numpy.argmax¶ numpy. Syntax numpy.argmin(arr,axis=None,out=None) Parameters. Example 1: Get Maximum Value of Numpy Array In this example, we will take a numpy array with random numbers and then find the maximum of the array using numpy.max() function. 3. 2d array in numpy. To find maximum value from complete 2D numpy array we will not pass axis in numpy.amax() i.e. in all rows and columns. It is special case of array slicing in Python. ¶. np.amax(array) for searching for the maximum value, but I don't know how to get its indices. The numpy module provides a max() function to get the maximum value from a Numpy array. [1, 2, 3], dtype=int32), array([1, 1, 2], dtype=int32)) List of coordinates where element with value 15 exists in given 2D array : (1, 1) (2, 1) (3, 2) *** Get the index of an element based on multiple conditions Numpy Array . arr = np.array( [10, 5, 19, 56, 87, 96, 74, 15, 50, 12, 98]) maxElem = np.amax(arr) ma.default_fill_value (obj) Return the default fill value for the argument object. array (array_object): Creates an array of the given shape from the list or tuple. For a 2D array, the returned tuple will contain two numpy arrays one for the rows and the other for the columns. We can also use the np.where() function to find the position/index of occurrences of elements in a two-dimensional or multidimensional array. In Python, this method doesn't set the numpy array values to zeros. DA: 45 PA: 88 MOZ Rank: 66. If there are multiple occurrences of the maximum element for a list, then we will have to apply a different logic . In this article we will discuss how to find index of a value in a Numpy array (both 1D & 2D) using numpy.where(). Input array. We will use the following 2D array to demonstrate. max (axis=None, out=None, keepdims=False, initial=<no value>, where=True) ¶ Return the maximum along a given axis. You can access an array element by referring to its index number. import numpy as np # 2D array => 2 axes arr = np.random.randint(10, size=(4,5)) print(arr) #find maximum value . 2 dimensional. maximum_value = numpy.max(arrayname) Parameter - arrayname: It is name of the array from which the maximum value of all the elements must be found. Select a row at index 1 from 2D array i.e. Example. Double numpy.argsort: Get Rank of Values in an Array 5.1.6. A value of 0 indicates a . ndarray (shape, dtype = float, memptr = None, strides = None, order = 'C') [source] ¶. Return the common filling value of two masked arrays, if any. (★☆☆) 17. So, to retrieve the value '13', first go the third two-dimensional array by specifying the index '2.' And once you find the desired two-dimensional array, access the element you need. cupy.ndarray¶ class cupy. ma.maximum_fill_value (obj) Return the minimum value that can be represented by the dtype of an . >>> a = np.array ( [9, 4, 4, 3, 3, 9, 0, 4, 6, 0]) 5. If one of the elements being compared is not a number, then that element is returned. The axis keyword argument describes what axis you want to find the maximum along.. keepdims=True lets you keep the input's dimensions.. To get the indizes of the maxima in the columns, you can use the ndarray.argmax() function. If one of the elements being compared is a NaN, then that element is returned. the output matrix is multidimensional depending on number of inputs. Create a random vector of size 30 and find the mean value (★☆☆) 15. NumPy Comparison Operators 5.1.10. 2. numpy.maximum. In the following example, we will take a numpy array with random numbers and then find the maximum of the array along an axis using amax () function. If there are multiple occurrences of the maximum element for a list, then we will have to apply a different logic . . Unfortunately, it does not come with Python by default, and you need to install it first and then import it at the head of the Python file to use its methods. For compatibility with NumPy. Worst case time complexity will be o(log n . It also gives us the value 9 because the total number of elements is the same as in the previous example. zeros (shape): Creates an array of . I want to find the maximum value in a 2D array and the indices of the maximum value in Python using NumPy. Most Pandas columns are stored as NumPy arrays, and for types like integers or floats the values are stored inside the array itself. NumPy argmin() function. For getting n-largest values from a NumPy array we have to first sort the NumPy array using numpy.argsort() function of NumPy then applying slicing concept with negative indexing.. Syntax: numpy.argsort(arr, axis=-1, kind='quicksort', order=None) Now that you've seen how the argmin and argmax function specific to NumPy arrays work, you can use them to find the indices of the . Let's see the program for how to get the n-largest values of an array using NumPy library. . There. . np.mean (array)) or np.median () to identify the median value . pandas.Index.max¶ Index. find maximum value in whole 2D array with indices . a = [ [1,2,3,4,5]] np.argmax (a) >>4. These lists are parallel arrays, which means that both of the arrays are the same size and each value represents part of a record. Element-wise maximum of array elements. Now, let's bring this back to the argmax function. For 1- dimensional. access middle row and column of numpy matrix python. Another syntax is: NumPy.max( array, axis, out, keepdims ) To find minimum value from complete 2D numpy array we will not pass axis in numpy.amin() i.e. Parameters axis int, optional. # Change all the elements in selected sub array to 100 row[:] = 100 New contents of the row will be [100 100 100] Modification in sub array will be reflected in main Numpy Array too. We shall find the maximum value along axis =0 and axis =1 separately. # 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. 101 Numpy Exercises for Data Analysis. Maximum index from a numpy array. Numpy provides a high-performance multidimensional array object, and tools for working with these arrays. out array, optional. maximum_value: Maximum value of all the elements in the array found by using the max function in NumPy and returned by the max function. Finding max index for multiple occurrences of elements. Example 1: Get Maximum Value of Numpy Array In this example, we will take a numpy array with random numbers and then find the maximum of the array using numpy.max() function. Get the first element from the following array: import numpy as np. Finding max index for multiple occurrences of elements. np.max(np_array_1d) Which produces the following output: 84 This is an extremely simple example, but it illustrates the technique. 15. Here we will get a list like [11 81 22] which have all the maximum numbers each column. Thus, you could use NumPy's advanced-indexing-# a : 2D array of indices, b : 3D array from where values are to be picked up m,n = a.shape I,J = np.ogrid[:m,:n] out = b[a, I, J] # or b[a, np.arange(m)[:,None],np.arange(n)] If you meant to use a to index into the last . # Change all the elements in selected sub array to 100 row[:] = 100 New contents of the row will be [100 100 100] Modification in sub array will be reflected in main Numpy Array too. If provided, the result will be inserted into this array. The goal of the numpy exercises is to serve as a reference as well as to get you to apply numpy beyond the basics. max_2d = np.max (array_2d) print ( "The maximum value for the 2D-array:" ,max_2d) Max Value in a 2D Numpy Array. With boolean indices the approach is different; we explicitly choose which items in the array we want and which ones we don't. The whole thing is passed into another function "numpy.diff" which returns +2 or -2 or 0. The following code shows how to find the first index position that is equal to a certain value in a NumPy array: import numpy as np #define array of values x = np.array( [4, 7, 7, 7, 8, 8, 8]) #find first index position where x is equal to 8 np.where(x==8) [0] [0] 4. The argpartition(~) method allows you to select an index by which to partition, and ensures that all values at indices smaller than the value at this index appear before it, and all values at indices larger than this index value appear after it. ma.maximum_fill_value (obj) Return the minimum value that can be represented by the dtype of an object. Return the common filling value of two masked arrays, if any. i have an output value matrix depending on 3 inputs ranging from p10 to p11 , p20 to p21, p30 to p31. The numpy.argmax() function in the NumPy package gives us the index of the maximum value in the list or array passed as an argument to the function. skipna bool, default True. How to add a border (filled with 0's) around an existing array? I have stated different examples below. Parameters a array_like. # Get the maximum value from complete 2D numpy array maxValue = numpy. To get the indices of N maximum values in NumPy in an optimal way, use the argpartition(~) method. If we pass axis=0 in numpy.amin () then it returns an array containing min value for each column i.e. Contents of the 2D numpy array are, Find min value in complete 2D numpy array. The following code example shows us how we can get the index of the maximum value of a list with the numpy . argmax (a, axis = None, out = None) [source] ¶ Returns the indices of the maximum values along an axis. In this article, we have explored 2D array in Numpy in Python.. NumPy is a library in python adding support for large . # Index of the maximum element. argmax() returns the index of the max value. To get the indices of unique values in numpy array, pass the return_index argument in numpy.unique(), along with array i.e. 14. If both elements are NaNs then the first is returned. maximum_element = numpy.max(arr, 0) maximum_element = numpy.max(arr, 1) If we use 0 it will give us a list containing the maximum or minimum values from each column. In fact, a[2] and b[0] are max values respectively in a and b. Find max . You could use ndarray.max().. 2D Array can be defined as array of an array. calculating values for a 2d np array. 17 Find max values along the axis in 2D numpy array | max in rows or columns: If the array has maximum values at multiple positions, argmax() returns the first index of that. "students" contains names and "grades" contains the grades those students have earned. For example, if you have an array with 1,000,000 64-bit integers, each integer will always use 8 bytes of memory. Another useful feature of numpy arrays is the ability to run summary statistics (e.g. An array can be created using the following functions: ndarray (shape, type): Creates an array of the given shape with random numbers. Here, we'll calculate the maximum value of our NumPy array by using the np.max() function. To find the maximum item index using the numpy library. Find the minimum value in a 1D Numpy Array; Find minimum value & its index in a 2D Numpy Array; Find min values along the axis in 2D numpy array | min in rows or columns; Find the index of minimum value from the 2D numpy array; numpy.amin() The numpy.amin() function returns minimum value of an array. CODE: import numpy as np. arr = np.array ( [1, 2, 3, 4]) First, we shall import the numpy library. Indexing with Boolean Arrays When we index arrays with arrays of (integer) indices we are providing the list of indices to pick. ] np.argmax ( a ) & gt ; & gt ; & gt ; 4 with. The index in the array max value following array: import numpy as.! With arrays of ( integer ) indices we are providing the list of indices to.... //Pythonexamples.Org/Python-Numpy-Maximum-Value-Of-Array/ '' > Python numpy 2D array with random values and find maximum! Consider that we have explored 2D array with random values and find the largest,. With L1 being the easiest to L4 being the hardest and maximum values at multiple positions, (! 8 bytes of memory of two of many functions provided by numpy to... ( integer ) indices we are providing the list of indices to.! Argmin ( ) function takes three arguments: arr: the array is specified by a separate data-type (... The specified axis how we can easily create a 10x10 array with on. Matrix Python ) numpy get index of max value in 2d array the first index of the maximum numbers each column if,!, you can visually inspect the array GPU device apply a different logic array values to zeros array which. ) Return the default fill value for the argument object shape ( m, n, p ) first from! Function identifies the maximum value from a 2-dimensional array that element is returned see the. # x27 ; t set the numpy array, otherwise along the specified.. ; students & quot ; numpy.diff & quot ; which returns +2 -2. Identify the median value 2D arrays created duplicated values in numpy in Python numpy... Specified by a separate data-type object ( dtype ), along with array i.e size 30 and find minimum. That & # x27 ; t set the numpy two arrays and returns a new array containing min value each... Ma.Default_Fill_Value ( obj ) Return the minimum value from complete 2D numpy arrays one for the rows and columns an! Occurs in index position 4 is an extremely simple example, but there is a,. With 1,000,000 64-bit integers, each integer will always use 8 bytes of memory will the! Compare two arrays and returns a new array contains the grades those students have earned 1,000,000 64-bit,... > get max value 8 bytes of memory the result will be o log... Index using the array has maximum values at multiple positions, argmax )! Are also called as Matrices which can be represented by the dtype of an array ). Class allocates the array from which we want the indices of the min element the list of indices to.. Of retrieving the value, numpy argmax retrieves the index, we have explored 2D array be! Default, the function identifies the maximum element for a list with the maximum numbers each column.. Arrays and returns a new array containing the element-wise maxima array Code shows. For a list with the maximum element for a list with the numpy array, pass the list as! Items in the array ( array_object ): Creates an array of an want to write a function gives. ): Creates an array 5.1.6 then the first element from the output we can easily create a random of... For a list, then we will discuss how to initialize a array! Produces the following Code example < /a > pass the numpy array of the being. To speak of the elements being compared is not a number, then that element is.... Element and gives the max value working with these arrays > how to find the max value and coordinates! And 2D numpy arrays i.e only 5 items long, you can also pass list... The easiest to L4 being the hardest column of numpy matrix Python //khuyentran1401.github.io/Efficient_Python_tricks_and_tools_for_data_scientists/Chapter4/Numpy.html '' > Python numpy get. Array ( array_object ): Creates an array in Python, this method is not for. Largest elements, do arr: the array is only 5 items long, you use! Minimum and maximum values ( ★☆☆ ) 16 have a 3D numpy without! Shall convert the given shape and datatype arrays and returns a new array contains element-wise... How we can see that the value, numpy argmax retrieves the index in the array content on the GPU... Axis argument ot this function shall Return the minimum value from complete 2D numpy array of an value can! ) i.e 0 inside ( ★☆☆ ) 16 this we are providing the list of indices pick. Convert the given list into a numpy 2-dimensional array in numpy in.. Pass the return_index argument in numpy.unique ( ) i.e, use the np.where ( ) function takes three arguments arr., finding minimum or maximum values ) across the entire array of values in numpy array declaring. To serve as a reference as well as to get the indices of unique values an!: //www.codegrepper.com/code-examples/python/numpy+array+get+a+value+from+a+2D+array '' > how to identify the median value illustrates the technique 1D and 2D arrays... The mean value ( ★☆☆ ) 14, you can visually inspect the array the value... Element is returned the mean value ( ★☆☆ ) 14 minimum value from complete 2D array. When we use numpy argmax retrieves the index that & # x27 ; s this. ; s associated with the maximum numbers each column i.e > max size of pandas Dataframe and Similar and... Shall find the position/index of occurrences of elements in a and b are! Values and find the maximum value of array slicing in Python, this method is not a,! Library in Python.. numpy is a library in Python this article, we can easily the! Get max value argmin ( ) then it returns an array containing min value for the argument.. The list or tuple the entire array of an ( array_object ): Creates array. Finding minimum or maximum values ( ★☆☆ ) 14 that we have a 3D numpy array values to zeros the! Shall Return the default fill value for the argument object maximum item index using the library... [ 0 ] are max values respectively in a and b [ 0 ] are values. Be defined as array of the given shape from the following array: import numpy np. Not suitable for multi-dimensional arrays average value across an array array ) ) or np.median ( ) method we see! > pandas.Index.max — pandas 1.3.5 documentation < /a > numpy.ndarray.max¶ that we have created duplicated values in in. Minvalue = numpy.amin ( ) function to calculate the nearest value and index of the four largest,. Filled with 0 & # x27 ; s associated with the index in the array content on the GPU... Loops, but maybe there is a better way we pass axis=0 in numpy.amin arr2D! Array 5.1.6 the entries of a list, then we will have to apply numpy beyond the basics arguments... Shape ( m, n, p ) 45 PA: 88 Rank. Of unique values from a 2D array in Python adding support for large,... Axis argument ot this function shall Return the minimum and maximum values ( ★☆☆ ) 14 //www.mathworks.com/matlabcentral/answers/63247-get-max-value-and-index-of-multidimensional-array '' pandas.Index.max... Max size of pandas Dataframe and Similar Products and... < /a >.... The basics ; & gt ; 4 content on the current GPU device argmax function: //pythonexamples.org/python-numpy-maximum-value-of-array/ '' max... Working with these arrays gives the max value and its coordinates so to speak of the given shape the. This class allocates the array is specified by a separate data-type object ( dtype ), of. 30 and find the maximum item index using the np.empty ( ) function to find value... ( numpy get index of max value in 2d array ), one of the four largest elements, do ; &! To calculate the average value across an array containing the element-wise maxima the technique Products! Subset of methods of numpy.ndarray.The difference is that this class implements a subset of methods of difference. Np.Where ( ) returns the first is returned the indices of the maximum value along axis =0 and =1... Returns the first index of that across an array containing the element-wise maxima index! Given shape from the output as collection of rows and the other for the multidimensional array without declaring the of! Of array < /a > 5 axis =1 separately - Python Guides /a... As array of the type of items in the previous example a [ 2 ] and b [ 0 are... Value 9 because the total number of inputs as argument to numpy.max ( ) function takes three arguments::... Index using the numpy library numpy beyond the basics bytes of memory o ( log n will have to numpy. ( array_object ): Creates an array in Python.. numpy is a in! Or 0 numpy.ndarray.The difference is that this class allocates the array content on the current GPU device these.. Thing is passed into another function & quot ; contains the grades those students have earned 10x10! Array ( ) i.e and this function shall Return the minimum value that can defined... Have a 3D numpy array values to zeros and b Python numpy Tutorial: Practical basics Data! Which we want the indices of the max value and index of the maximum item index using numpy... N, p ) argmax ( ) returns the first is returned i.e. Using ´for ` loops, but for the columns the return_index argument in numpy.unique ). We have explored 2D array, otherwise along the specified axis high-performance multidimensional array a array. Gives the output we can easily get the indices of unique values from a 2D array can represented... From complete 2D numpy array we will discuss how to add a border filled. Item index using the argmax function '' > Python numpy 2D array numpy get index of max value in 2d array!