How to find maximum value in whole 2D array with indices [duplicate] How to find maximum value in whole 2D array with indices [duplicate] numpy numpy

How to find maximum value in whole 2D array with indices [duplicate]


Refer to this answer, which also elaborates how to find the max value and its (1D) index, you can use argmax()

>>> a = array([[10,50,30],[60,20,40]])>>> maxindex = a.argmax()>>> maxindex3

You can then use unravel_index(a.argmax(), a.shape) to get the indices as a tuple:

>>> from numpy import unravel_index>>> unravel_index(a.argmax(), a.shape)(1, 0)


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.

import numpy as npa = np.array([[200,300,400],[100,50,300]])indices = np.where(a == a.max())print(a[indices]) # prints [400]# Index for max value found two times (two locations)a = np.array([[200,400,400],[100,50,300]])indices = np.where(a == a.max())print(a[indices]) # prints [400 400] because two indices for max#Now lets print the location (Index)for index in indices:    print(index)


You can use argmax() to get the index of your maximum value.

a = np.array([[10,50,30],[60,20,40]])maxindex = a.argmax()print maxindex

It should return:

3

Then you just have to compute this value to get the line and column indices.

Best