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]