I have a numpy ndarray:
array = np.array([[1,2,3],[4,5,6],[7,8,9]])
Whenever I do, np.argmax(array)
, it doesn't return a tuple of row and column.
Why not?
You didn't specify an axis
. It returns an index into the flattened array, as documented.
>>> L = [[1,2,3],[4,5,6],[7,8,9]]
>>> np.argmax(L)
8
>>> np.array(L).ravel()[np.argmax(L)]
9
If you want to get the row and column from that index, unravel it:
>>> np.unravel_index(8, np.array(L).shape)
(2, 2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With