【问题标题】:Some clarification on numpy indexing needed?需要对 numpy 索引进行一些说明吗?
【发布时间】:2016-06-16 13:14:56
【问题描述】:

我有以下 numpy 数组:

        boxIDx = 3
        index = np.array([boxIDs!=boxIDx]).reshape(-1,1)
        print('\nbboxes:\t\n', bboxes)
        print('\nboxIDs:\t\n', boxIDs)
        print('\nIndex:\t\n', index)

输出是:

    bboxes: 
     [[370 205  40  40]
      [200 100  40  40]
      [ 30  50  40  40]]
    boxIDs: 
     [[1]
      [2]
      [3]]
    Index:  
     [[ True]
      [ True]
      [False]]

问题:如何使用我的索引“删除”第三行(bbox)?

我试过了:

bboxes = bboxes[index,:]

还有:

bboxes = bboxes[boxIDs!=boxIDx,:]

这两个都给我以下错误:

IndexError: too many indices for array

对不起,如果这是愚蠢的 - 但我在这里遇到了麻烦:/

【问题讨论】:

    标签: python python-3.x numpy indexing


    【解决方案1】:

    发生错误是因为您尝试传递向量而不是索引数组。您可以使用reshape(-1)reshape(3) 作为您的index

    In [56]: bboxes[index.reshape(-1),:]
    Out[56]:
    array([[370, 205,  40,  40],
           [200, 100,  40,  40]])
    
    In [57]: bboxes[index.reshape(3),:]
    Out[57]:
    array([[370, 205,  40,  40],
           [200, 100,  40,  40]])
    
    In [58]: index.reshape(-1)
    Out[58]: array([ True,  True, False], dtype=bool)
    
    In [59]: index.reshape(-1).shape
    Out[59]: (3,)
    

    【讨论】:

      【解决方案2】:

      由于Index是二维的,所以必须去掉额外的维度,所以

      no_third = bboxes[Index[:,0]] 
      # array([[370, 205,  40,  40],
      #        [200, 100,  40,  40]])
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-04-03
        • 2014-10-02
        • 2011-08-02
        • 1970-01-01
        • 1970-01-01
        • 2012-07-19
        • 2013-07-17
        相关资源
        最近更新 更多