【问题标题】:Indices of k-minimum values along an axis of a numpy array沿 numpy 数组的轴的 k 最小值的索引
【发布时间】:2012-01-29 22:53:59
【问题描述】:

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

【问题讨论】:

    标签: python arrays numpy indices minim


    【解决方案1】:
    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]])
    

    【讨论】:

      猜你喜欢
      • 2016-03-17
      • 2019-06-25
      • 1970-01-01
      • 2019-11-18
      • 2021-07-20
      • 1970-01-01
      • 1970-01-01
      • 2021-06-03
      • 2017-03-27
      相关资源
      最近更新 更多