简体   繁体   中英

How to find the argmax of a two dimensional array in numpy?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM