ValueError: cannot reshape array of size 15525000 into shape (260,260) in Python?

ValueError: cannot reshape array of size 15525000 into shape (260,260) in Python?

我在为实现 CNN 重塑数据帧时遇到问题。

我的数据框形状:train.shape -> (230, 67502).

然后我写了如下代码。

Y_train = train["Label"]
X_train = train.drop(labels = ["Label"],axis = 1) 

当我 运行 下面的代码用于 iloc 绘制一些图像时,它会抛出一个错误

img = X_train.iloc[0].to_numpy()
img = np.pad(img, (0, (67600-img.shape[0])), 'constant').reshape((260, 260))
plt.imshow(img,cmap='gray')
plt.title(train.iloc[0,0])
plt.axis("off")
plt.show()

然后我归一化X_train

X_train = X_train / 255.0
print("x_train shape: ",X_train.shape)

当我重塑 X_train 时,它会抛出一个错误

X_train = X_train.values.reshape(-1, 260, 260)
print("x_train shape: ",X_train.shape)

ValueError: cannot reshape array of size 15525000 into shape (260,260)

我该如何解决这个问题?

嗯,260*26067600 而不是 67500。所以你不能将你的数组转换成这些维度。

要实际将其转换为那些维度,您需要填充或规范化源图像数组。例如,查看有关处理此类问题的 Keras pad_sequences 功能文档。

您确定需要 260 * 260 图片吗?作为67500 == 270 * 250,你可以试试看效果如何! 否则您将需要 PAD - 具体如何取决于您的图像。

但是,最简单的方法之一可能是在末尾添加 100 更多 0's 使其成为 67600 - 因此 260 * 260

解决方案:

X_train = np.pad(X_train, ((0,0), (0, (67600-X_train.shape[1]))), 'constant').reshape(-1, 260, 260)