gpt4 book ai didi

Python:将 1×28×28 数组转换为 28×28 矩阵

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

我有一个存储为 1×28×28 数组 (uint8) 的灰度图像,我希望使用 matplotlib 显示它。

相关代码如下:

import matplotlib.pyplot as plt

image = get_image()
print image.shape # prints (1, 28, 28)
plt.imshow(image)

此代码导致 TypeError:图像数据的尺寸无效

为了与 imshow() 兼容,我需要将数组压缩为 28×28(删除冗余维度)或将 2D 矩阵(28×28)复制到3 (RGB) 或 4 (RGBA) channel 。

我倾向于使用第一种方法,但是 squeeze() 不会删除冗余维度。

如何将 1×28×28 数组转换为 28×28 矩阵?

最佳答案

你可以通过 squeeze 或 reshape:

 imshow(np.squeeze(image))


imshow(image.reshape((28,28))

关于Python:将 1×28×28 数组转换为 28×28 矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30180566/

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