gpt4 book ai didi

python - 根据条件删除numpy数组中的行

转载 作者:太空宇宙 更新时间:2023-11-04 00:32:08 25 4
gpt4 key购买 nike

我有一个形状为 [6,2] 的二维 numpy 数组,我想删除第三个元素包含 0 的子数组。

array([[0, 2, 1], #Input
[0, 1, 1],
[1, 1, 0],
[1, 0, 2],
[0, 2, 0],
[2, 1, 2]])

array([[0, 2, 1], #Output
[0, 1, 1],
[1, 0, 2],
[2, 1, 2]])

我的代码是 positives = gt_boxes[np.where(gt_boxes[range(gt_boxes.shape[0]),2] != 0)]

它有效,但有没有简化的方法?

最佳答案

您可以使用 bool 索引

In [413]: x[x[:, -1] != 0]
Out[413]:
array([[0, 2, 1],
[0, 1, 1],
[1, 0, 2],
[2, 1, 2]])

  1. x[:, -1] 将检索最后一列

  2. x[:, -1] != 0 返回一个 bool 掩码

  3. 使用掩码索引到原始数组

关于python - 根据条件删除numpy数组中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45427833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com