沿numpy数组的轴的k-最小值的指数

时间:2011-12-29 14:46:03

标签: python arrays numpy indices minim

有没有办法在不使用循环的情况下沿numpy数组的轴返回k-minimum值的索引?

1 个答案:

答案 0 :(得分:7)

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]])