ValueError:无法将大小为445513728的数组重塑为形状(2266,256,256,1)

时间:2019-09-30 06:24:39

标签: python-3.x machine-learning image-processing keras

这是我要运行的代码

labelled_data = [data, Label]
X,Y = [labelled_data[0],labelled_data[1]]

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.4, 
random_state=4)

x_test, x_validation, y_test, y_validation=train_test_split(X_test, 
Y_test, test_size=0.5,
 random_state=4)
import numpy as np


print(X_train.shape)

输出为:

(2266, 196608)

现在我想重塑训练集,验证集和测试集的矩阵。

X_train = X_train.reshape((X_train.shape[0],256,256,1))
x_validation = x_validation.reshape((x_validation.shape[0],256,256,1))
x_test =x_test.reshape((x_test.shape[0],256,256,1))

X_train = X_train.astype('float32')
x_validation = x_validation.astype('float32')
x_test = x_test.astype('float32')


X_train = X_train/255
x_validation = x_validation/255
x_test =x_test/255

from keras.utils import np_utils
Y_train = np_utils.to_categorical(Y_train,8)
y_validation =np_utils.to_categorical(y_validation,8)
y_test =np_utils.to_categorical(y_test,8)

运行该程序时出现错误。

ValueError                                Traceback (most recent call last)
<ipython-input-30-fc799feec008> in <module>
----> 1 X_train = X_train.reshape((X_train.shape[0],256,256,1))
      2 x_validation = x_validation.reshape((x_validation.shape[0],256,256,1))
      3 x_test =x_test.reshape((x_test.shape[0],256,256,1))
      4 
      5 X_train = X_train.astype('float32')

ValueError: cannot reshape array of size 445513728 into shape (2266,256,256,1)

请帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

如果print(X_train.shape)的输出为(2266,196608),则X_train.shape [0]为2266。

如果您再说

X_train = X_train.reshape((X_train.shape[0],256,256,1))

您正在尝试将2266 x 196608(= 445513728)调整为2266 x 256 x 256 x 1(= 148504576),以便得到消息

ValueError: cannot reshape array of size 445513728 into shape (2266,256,256,1)

这256个值之一必须为768,以使整形工作正常。