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

后端 未结 18 1438
长情又很酷
长情又很酷 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:03

    I found it most intuitive to use np.unique.

    The idea is, that the unique method returns the indices of the input values. Then from the max unique value and the indicies, the position of the original values can be recreated.

    multi_max = [1,1,2,2,4,0,0,4]
    uniques, idx = np.unique(multi_max, return_inverse=True)
    print np.squeeze(np.argwhere(idx == np.argmax(uniques)))
    >> [4 7]
    

提交回复
热议问题