Python 需要对numpy索引进行一些澄清吗?
我有以下numpy阵列:Python 需要对numpy索引进行一些澄清吗?,python,python-3.x,numpy,indexing,Python,Python 3.x,Numpy,Indexing,我有以下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]
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
如果这是哑的,很抱歉-但我在这里遇到了问题:/发生错误是因为您试图传递向量而不是索引数组。您可以对
索引使用重塑(-1)
或重塑(3)
:
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,)
因为索引是二维的,所以必须去掉额外的维度,所以
no_third = bboxes[Index[:,0]]
# array([[370, 205, 40, 40],
# [200, 100, 40, 40]])