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()
>>> maxindex
3
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)
Answer from Chandrika Joshi on Stack OverflowRefer 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()
>>> maxindex
3
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 np
a = 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)
python - Finding the Max value in a two dimensional Array - Stack Overflow
Python index of max value in 2D list
python - Find index of max value in each row of 2D array - Stack Overflow
python - how to get the max of each column from an 2d array + index of the max value - Stack Overflow
Max of max numbers (map(max, numbers) yields 1, 2, 2, 3, 4):
>>> numbers = [0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0]
>>> map(max, numbers)
<map object at 0x0000018E8FA237F0>
>>> list(map(max, numbers)) # max numbers from each sublist
[1, 2, 2, 3, 4]
>>> max(map(max, numbers)) # max of those max-numbers
4
Another way to solve this problem is by using function numpy.amax()
>>> import numpy as np
>>> arr = [0, 0, 1, 0, 0, 1] , [0, 1, 0, 2, 0, 0] , [0, 0, 2, 0, 0, 1] , [0, 1, 0, 3, 0, 0] , [0, 0, 0, 0, 4, 0]
>>> np.amax(arr)
You could use ndarray.max().
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.
You can also pass the axis argument ot this function, but there is no keepdims option.
In both commands axis=0 describes the columns, axis=1 describes the rows.
The standard value axis=None would search the maximum in the entire flattened array.
Example:
import numpy as np
A = np.asarray(
[[1, 2, 3, 4, 5],
[2, 4, 5, 8, 7],
[9, 8, 4, 5, 2],
[1, 2, 4, 7, 2],
[5, 9, 8, 7, 6],
[1, 2, 5, 4, 3]])
print(A)
max = A.max(axis=0, keepdims=True)
max_index = A.argmax(axis=0)
print('Max:', max)
print('Max Index:', max_index)
This prints:
[[1 2 3 4 5]
[2 4 5 8 7]
[9 8 4 5 2]
[1 2 4 7 2]
[5 9 8 7 6]
[1 2 5 4 3]]
Max: [[9 9 8 8 7]]
Max Index: [2 4 4 1 1]
you can use numpy as well.
Example:
import numpy as np
A = [[1, 2, 3, 4, 5],
[2, 4, 5, 8, 7],
[9, 8, 4, 5, 2],
[1, 2, 4, 7, 2],
[5, 9, 8, 7, 6],
[1, 2, 5, 4, 3]]
print(A)
A=np.array(A)
print(A.max(axis=0))