如何将两个numpy数组合为一体?

时间:2020-05-20 21:10:54

标签: python arrays numpy matrix numpy-ndarray

我有一个数组preds,其值是0,1 or 2

preds的形状和值如下所示:

torch.Size([1, 256, 256])
tensor([[[1, 1, 0,  ..., 1, 1, 2],
         [1, 0, 1,  ..., 1, 1, 1],
         [0, 0, 0,  ..., 1, 1, 1],
         ...,
         [0, 1, 1,  ..., 1, 1, 1],
         [1, 1, 1,  ..., 1, 1, 1],
         [1, 1, 1,  ..., 1, 1, 1]]])

我只关心第二维和第三维(256 x 256)。我想使用matplotlib创建一个遮罩,分别绘制012。基本上,我想可视化图像中拥有0的所有位置,拥有1的位置以及拥有2的位置的位置。我要塞巴蒂的阴谋。

我在想类似的东西。例如1类:

  1. 获取我们有1个地方的地点并将其赋值 1,并为其他索引分配值0。
  2. 使用plt.show
  3. 绘制

示例:

zero = (preds==0).nonzero() #index of all values which have 0 
nonzero = (preds!=0).nonzero()    # index of all value which are not 0
# assign 1 to all zero
# assign 0 to all nonzero
# join the above two array
# plot the array

我只是不知道如何在我的伪代码中添加这两个数组(nonzerozero)。

还有更多的Python方式吗?我不想遍历所有索引值,然后为它们分配值。

1 个答案:

答案 0 :(得分:0)

我可以使用np.where来做评论中建议的人。看起来像这样:

#preds in the matrix where I want the 0 and 1s
preds_clear = np.where(preds==0, 1, 0)
plt.imshow(preds_clear[0,:,:])