2016-03-03 33 views
2

我有以下numpy的数组:需要对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]] 

问:我怎么用我的指数 '删除' 的第三排(的bboxes)?

我曾尝试:

bboxes = bboxes[index,:] 

还有:

bboxes = bboxes[boxIDs!=boxIDx,:] 

这两者给我下面的错误:

IndexError: too many indices for array 

很抱歉,如果这是愚蠢的 - 但我在这里遇到麻烦:/

回答

2

发生此错误的原因是您尝试传递向量而不是数组索引。你可以使用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,) 
+0

如何智障我的!谢谢! –

1

由于Index是二维的,你必须摆脱额外的维度,所以

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