To find the maximum value of an array, you can use the array.max () method. This is likely to be more efficient than the for loop described in another answer, which, in addition to being non-pythonic, is not actually written in python. (if you want to take elements from an array one by one for comparison, you can use ndenumerate, but you will sacrifice some performance advantages of arrays).
The reason numpy.where () yields results in the form of tuples is because more than one position can be equal to max ... and this is the extreme case that would do something simple (for example, taking array [ 0]) error prone. Per Is there a Numpy function to return the first index of something to an array? ,
"The result is a tuple with all the first row indices, then all the column indices."
Your example uses a 1-D array, so you get the results that you want to get directly from the provided array. This is a tuple with one element (one array of indices), and although you can iterate over ind_1d [0] directly, I converted it to a list for readability only.
>>> peakIndex_1d array([ 1. , 1.1, 1.6, 1. , 1.6, 0.8]) >>> ind_1d = numpy.where( peakIndex_1d == peakIndex_1d.max() ) (array([2, 4]),) >>> list( ind_1d[0] ) [2, 4]
For a two-dimensional array with 3 values โโequal to max, you can use:
>>> peakIndex array([[ 0. , 1.1, 1.5], [ 1.1, 1.5, 0.7], [ 0.2, 1.2, 1.5]]) >>> indices = numpy.where( peakIndex == peakIndex.max() ) >>> ind2d = zip(indices[0], indices[1]) [(0, 2), (1, 1), (2, 2)]