【问题标题】:How to filter numpy array by list of indices?如何按索引列表过滤numpy数组?
【发布时间】:2013-11-06 19:50:13
【问题描述】:

我有一个 numpy 数组 filtered__rows,由 LAS 数据 [x, y, z, intensity, classification] 组成。我创建了一个点的cKDTree 并找到了最近的邻居query_ball_point,它是该点及其邻居的索引列表。

有没有办法过滤filtered__rows 以创建一个仅包含索引在query_ball_point 返回的列表中的点的数组?

【问题讨论】:

    标签: python numpy scipy nearest-neighbor


    【解决方案1】:

    看起来你只需要一个基本的integer array indexing:

    filter_indices = [1,3,5]
    np.array([11,13,155,22,0xff,32,56,88])[filter_indices] 
    

    【讨论】:

    • 你知道多维数组是如何转换的吗?
    • 我回答了multi-dimm版本,但不介意是否有人将它与这个答案合并。
    【解决方案2】:

    numpy.take 非常有用,并且适用于多维数组。

    import numpy as np
    
    filter_indices = [1, 2]
    array = np.array([[1, 2, 3, 4, 5], 
                      [10, 20, 30, 40, 50], 
                      [100, 200, 300, 400, 500]])
    
    axis = 0
    print(np.take(array, filter_indices, axis))
    # [[ 10  20  30  40  50]
    #  [100 200 300 400 500]]
    
    axis = 1
    print(np.take(array, filter_indices, axis))
    # [[  2   3]
    #  [ 20  30]
    # [200 300]]
    

    【讨论】:

      【解决方案3】:

      使用文档:https://docs.scipy.org/doc/numpy-1.13.0/user/basics.indexing.html 以下实现应该适用于某些 numpy ndarray 的任意数量的维度/形状。

      首先我们需要一组多维索引和一些示例数据:

      import numpy as np
      y = np.arange(35).reshape(5,7)
      print(y) 
      indexlist = [[0,1], [0,2], [3,3]]
      print ('indexlist:', indexlist)
      

      要真正提取直观的结果,诀窍是使用转置:

      indexlisttranspose = np.array(indexlist).T.tolist()
      print ('indexlist.T:', indexlisttranspose)
      print ('y[indexlist.T]:', y[ tuple(indexlisttranspose) ])
      

      进行以下终端输出:

      y: [[ 0  1  2  3  4  5  6]
       [ 7  8  9 10 11 12 13]
       [14 15 16 17 18 19 20]
       [21 22 23 24 25 26 27]
       [28 29 30 31 32 33 34]]
      indexlist: [[0, 1], [0, 2], [3, 3]]
      indexlist.T: [[0, 0, 3], [1, 2, 3]]
      y[indexlist.T]: [ 1  2 24]
      

      元组...修复了我们可能会导致的未来警告:

      print ('y[indexlist.T]:', y[ indexlisttranspose ])
      
      FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`.
      In the future this will be interpreted as an array index,
      `arr[np.array(seq)]`, which will result either in an error or a
      different result.
          print ('y[indexlist.T]:', y[ indexlisttranspose ])
      y[indexlist.T]: [ 1  2 24]
      

      【讨论】:

        【解决方案4】:

        你知道多维数组是如何转换的吗?

        它可以扩展为多维数组,方法是为每个索引提供一个 1d 数组,因此对于 2d 数组 filter_indices=np.array([[1,0],[0,1]]) array=np.array([[0,1],[1,2]]) print(array[filter_indices[:,0],filter_indices[:,1])

        会给你: [1,1]

        Scipy 解释了如果你打电话会发生什么: print(array[filter_indices])

        https://docs.scipy.org/doc/numpy-1.13.0/user/basics.indexing.html

        【讨论】:

        • 我认为这比元组转换的其他两个答案更快。
        【解决方案5】:

        最快的方法是X[tuple(index.T)],其中X 是包含元素的ndarray,index 是希望检索的索引的ndarray。

        【讨论】:

          猜你喜欢
          • 2021-11-17
          • 2013-11-10
          • 1970-01-01
          • 1970-01-01
          • 2012-08-04
          • 2013-09-19
          • 1970-01-01
          相关资源
          最近更新 更多