0

I am trying to find the position of the maximum value in a specific column of a numpy array.

The code to set up my array is:

m=np.zeros((3,4),dtype=int)

for i in range (0,3):
    m[0][i]=1;

m[1][1]=1
m[2][0]=1
m[2][1]=1
m[0][3]=2
m[2][3]=1
print(m)

(I know this is a silly way to set it up but this is part of a much larger program and needs to be set up in this way)

Using this code:

b=0
m_e_p=np.max(m[:,b])
        print(m_e_p)

I get the answer 1 since 1 is the maximum value in column 0, but I need its position. How do I get this?

smash97
  • 1
  • 1
  • 1

1 Answers1

1

Use np.argmax on the ndarray sliced by the column of interest:

print(m)
array([[1, 1, 1, 2],
       [0, 1, 0, 0],
       [1, 1, 0, 1]])

np.argmax(m[:,col])

Example

np.argmax(m[:,3])
# 0
yatu
  • 86,083
  • 12
  • 84
  • 139