首页 > 解决方案 > Python - 使用另一个数组的数组子集,得到 IndexError

问题描述

我在根据另一个数组的值对数组进行切片的简单任务时遇到了麻烦。

我有数组scores,形状为:

scores.shape = (1, 100, 1)

为批次中的每个图像中的 100 次检测提供置信度分数(但我使用的是单个图像,因此批次中只有一个元素)。因此,对于第一个也是唯一一个图像,我有 100 个检测值::

scores[0] -> [score00, ..., score99]

然后,我有另一个类似的数组bboxes ...对于批次中的每个图像(同样,只使用一个图像),并且对于每个图像中的所有 100 个检测,它包含 4 个值。所以形状是:

bboxes.shape = (1, 100, 4)

对于唯一的图像,我有 100 个四倍的值

bboxes[0] -> [ [x_min, y_min, x_max, y_max], ..., [x_min, y_min, x_max, y_max] ]

并且,在这 100 个四元组中,我只需要提取与分数中值高于某个阈值(0.5)的元素相对应的那些。所以,假设只有前 2 个分数高于阈值,我只想要前 2 个四倍。

我正在尝试类似的东西:

print(bboxes[0][scores[0]>0.5])

但我得到了错误:

IndexError: boolean index did not match indexed array along dimension 1; dimension is 4 but corresponding boolean dimension is 1

我究竟做错了什么?

标签: pythonnumpy-ndarraynumpy-slicing

解决方案


试试这个:

for score, box in zip(scores[0], bboxes[0]):
    if score > 0.5:
        print(box)

推荐阅读