【问题标题】:numpy get column indices where all elements are greater than thresholdnumpy 获取所有元素都大于阈值的列索引
【发布时间】:2017-07-25 15:01:54
【问题描述】:

我想找到一个 numpy 数组的列索引,其中列的所有元素都大于阈值。

例如,

 X = array([[ 0.16,  0.40,  0.61,  0.48,  0.20],
            [ 0.42,  0.79,  0.64,  0.54,  0.52],
            [ 0.64,  0.64,  0.24,  0.63,  0.43],
            [ 0.33,  0.54,  0.61,  0.43,  0.29],
            [ 0.25,  0.56,  0.42,  0.69,  0.62]])

在上述情况下,如果阈值为0.4,我的结果应该是1,3。

【问题讨论】:

    标签: python numpy


    【解决方案1】:

    您可以使用np.where 与每列的min 进行比较:

    large = np.where(X.min(0) >= 0.4)[0]
    

    【讨论】:

      【解决方案2】:
      x = array([[ 0.16,  0.40,  0.61,  0.48,  0.20],
              [ 0.42,  0.79,  0.64,  0.54,  0.52],
              [ 0.64,  0.64,  0.24,  0.63,  0.43],
              [ 0.33,  0.54,  0.61,  0.43,  0.29],
              [ 0.25,  0.56,  0.42,  0.69,  0.62]])
      
      threshold = 0.3
      size = numpy.shape(x)[0]
      for it in range(size):
          y = x[it] > threshold
          print(y.all())
      

      请尝试。

      【讨论】:

        【解决方案3】:

        使用列表理解的通用解决方案

        threshold = 0.4
        rows_nb, col_nb = shape(X)
        rows_above_threshold = [col for col in range(col_nb) \
            if all([X[row][col] >= threshold for row in range(rows_nb)])]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-03-27
          • 1970-01-01
          • 2013-06-26
          • 1970-01-01
          • 2020-07-30
          • 2018-10-07
          • 2019-12-08
          相关资源
          最近更新 更多