2016-03-03 22 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]])