Find the index of the k smallest values of a numpy array

后端 未结 4 778
南旧
南旧 2020-12-07 14:44

In order to find the index of the smallest value, I can use argmin:

import numpy as np
A = np.array([1, 7, 9, 2, 0.1, 17, 17, 1.5])
print A.arg         


        
4条回答
  •  既然无缘
    2020-12-07 15:23

    You can use numpy.argsort with slicing

    >>> import numpy as np
    >>> A = np.array([1, 7, 9, 2, 0.1, 17, 17, 1.5])
    >>> np.argsort(A)[:3]
    array([4, 0, 7], dtype=int32)
    

提交回复
热议问题