Looks like you want the row that contains the maximum value, right?
max(axis=0) returns the maximum of [1,0] and [2,4] independently.
argmax without axis parameter finds the maximum over the whole array - in flattened form. To turn that index into row number we have to use unravel_index:
In [464]: a.argmax()
Out[464]: 3
In [465]: np.unravel_index(3,(2,2))
Out[465]: (1, 1)
In [466]: a[1,:]
Out[466]: array([0, 4])
or in one expression:
In [467]: a[np.unravel_index(a.argmax(), a.shape)[0], :]
Out[467]: array([0, 4])
As you can see from the length of the answer it's not the usual definition of maximum along/over an axis.
Sum along axis in numpy array may give more insight into the meaning of 'along axis'. The same definitions apply to the sum, mean and max operations.
===================
To pick row with the largest norm, first calculate the norm. norm uses the axis parameter in the same way.
In [537]: np.linalg.norm(a,axis=1)
Out[537]: array([ 2.23606798, 4. ])
In [538]: np.argmax(_)
Out[538]: 1
In [539]: a[_,:]
Out[539]: array([0, 4])
Answer from hpaulj on Stack OverflowLooks like you want the row that contains the maximum value, right?
max(axis=0) returns the maximum of [1,0] and [2,4] independently.
argmax without axis parameter finds the maximum over the whole array - in flattened form. To turn that index into row number we have to use unravel_index:
In [464]: a.argmax()
Out[464]: 3
In [465]: np.unravel_index(3,(2,2))
Out[465]: (1, 1)
In [466]: a[1,:]
Out[466]: array([0, 4])
or in one expression:
In [467]: a[np.unravel_index(a.argmax(), a.shape)[0], :]
Out[467]: array([0, 4])
As you can see from the length of the answer it's not the usual definition of maximum along/over an axis.
Sum along axis in numpy array may give more insight into the meaning of 'along axis'. The same definitions apply to the sum, mean and max operations.
===================
To pick row with the largest norm, first calculate the norm. norm uses the axis parameter in the same way.
In [537]: np.linalg.norm(a,axis=1)
Out[537]: array([ 2.23606798, 4. ])
In [538]: np.argmax(_)
Out[538]: 1
In [539]: a[_,:]
Out[539]: array([0, 4])
a = np.array([
[1,2],
[0,4]
])
np.max(a, axis=0)
For a two-dimensional array, we have two axis, axis=0 and axis=1.
axis=0 means going along columns and axis=1 means going along rows.
The output of the code is an array [1,4], which means 1 is the maximum along the 1st column and 4 is the maximum along the 2nd column.

I have an (N, N, 3) numpy array of floats as input.
I want to return an (N, N, 3) array which has, along the third axis, a 1.0 in the position of the highest value for that vector, and 0.0s in the other two values.
That is to say, for N=2 with input:
in = array([[[0.3767, 0.5967, 0.4188],
[0.3749, 0.5432, 0.8066]],
[[0.3265, 0.9366, 0.6033],
[0.2315, 0.9459, 0.2973]]])I want to return output:
out = array([[[0.0000, 1.0000, 0.0000],
[0.0000, 0.0000, 1.0000]],
[[0.0000, 1.0000, 0.0000],
[0.0000, 1.0000, 0.0000]]])Right now I can get this output using:
import numpy as np
np.set_printoptions(precision=4, floatmode='fixed')
N = 2
inp = np.random.random((N, N, 3))
out = np.empty_like(inp)
for i in range(N):
for j in range(N):
out[i, j, :] = 1.0 * (inp[i, j, :] == np.max(inp[i, j, :]))
print(f'in = {inp}')
print(f'out = {out}')It seems like there should be a way to (at least explicitly) avoid those nested for loops, but I don't know how.
Is there a neater/more efficient way to do this?