How do I get indices of N maximum values in a NumPy array?

后端 未结 18 1444
长情又很酷
长情又很酷 2020-11-22 04:25

NumPy proposes a way to get the index of the maximum value of an array via np.argmax.

I would like a similar thing, but returning the indexes of the

18条回答
  •  天涯浪人
    2020-11-22 05:00

    The following is a very easy way to see the maximum elements and its positions. Here axis is the domain; axis = 0 means column wise maximum number and axis = 1 means row wise max number for the 2D case. And for higher dimensions it depends upon you.

    M = np.random.random((3, 4))
    print(M)
    print(M.max(axis=1), M.argmax(axis=1))
    

提交回复
热议问题