Indices of k-minimum values ​​along the axis of a numpy array

Is there a way to return indices of k-minimum values ​​along the axis of a numpy array without using loops?

+4
source share
1 answer
import numpy as np x = np.array([[5, 2, 3],[1, 9, 2]]) # example data k = 2 # return the indices of the 2 smallest values np.argsort(x, axis=1)[:,0:k] # by row array([[1, 2], [0, 2]]) 
+7
source

Source: https://habr.com/ru/post/1388467/


All Articles