首页 > 解决方案 > 我想使用 numpy 从以下数组中删除所有 16

问题描述

在此处输入图像描述 我想使用 numpy 从以下数组中删除所有 16

标签: python-3.xnumpy

解决方案


正如所有评论所述,请添加代码,如果需要,请添加图像。

当您说“删除所有 16 个”时,您的意思是用其他值替换它们还是只选择一个没有 16 个的子数组?

似乎 16 总是放在每行的末尾,因此你可以这样做

img # array containing the 16, of shape (X,Y,4)
img_no16 = img[:,:,:3]  # this gets you rid of the last column and returns an array of shape (X,Y,3)

但是,如果您想替换16数组中的所有元素,您有两个选择

  1. 如果它们总是放在每行的末尾,你可以做类似上面的事情 img[:,:,:3] = 0 # or any other number

  2. 如果您不知道 16 在哪里并且只想将它们全部替换为 -1,您可以这样做 img[np.where(img == 16)] = -1


推荐阅读