문제

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

도움이 되었습니까?

해결책

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]])
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top