0

How do I obtain the row number that myMax is in?

import numpy as np

myArray = np.arange(20).reshape(4,5)
print(myArray)
myMax = max(myArray[:,3])
print(myMax)
Captain Jack Sparrow
  • 971
  • 1
  • 11
  • 28
nicomp
  • 4,344
  • 4
  • 27
  • 60
  • Does this answer your question? [How to get the index of a maximum element in a numpy array along one axis](https://stackoverflow.com/questions/5469286/how-to-get-the-index-of-a-maximum-element-in-a-numpy-array-along-one-axis) – AMC Apr 08 '20 at 01:45
  • As an aside, variable and function names should generally follow the `lower_case_with_underscores` style. – AMC Apr 08 '20 at 01:45
  • 1
    @AMC lol about `snake_case` ;) – Captain Jack Sparrow Apr 08 '20 at 17:16

2 Answers2

2

You can either use np.where :

In [1]: np.where(myArray == myMax)
Out[1]: (array([3], dtype=int64), array([3], dtype=int64))

Or you can use .argmax()

In [2]: myArray[:,3].argmax()
Out[2]: 3
0

Use argmax

arg_max = myArray[:, 3].argmax()
myArray[arg_max,:]

If you don't have the column like in the case above, argmax returns the index of the flattened array, for example:

np.random.seed(0)
myArray = np.random.choice(20, 20).reshape(4, 5) # max is [1,2]
arg_max = myArray.argmax() # returns 7
rows, cols = myArray.shape
myArray[int(arg_max/cols), :]
jcaliz
  • 3,891
  • 2
  • 9
  • 13