我有250天的数据,72个训练样本特征和一列目标变量。并希望预测21351行中72个特征的未来30天。我将如何重塑我的数据输入和输出。看起来我有点困惑,而且库给了我关于形状不兼容的错误。
我正在重塑为:
trainX.reshape(1, len(trainX), trainX.shape[1])
trainY.reshape(1, len(trainX))
但给我错误:
ValueError:输入数组的采样数应与目标数组的采样数相同。找到1个输入样本和250个目标样本。
与以下内容相同的错误:
trainX.reshape(1, len(trainX), trainX.shape[1])
trainY.reshape(len(trainX), )
同样的错误是:
trainX.reshape(1, len(trainX), trainX.shape[1])
trainY.reshape(len(trainX), 1)
目前,trainX被重塑为:
trainX.reshape(trainX.shape[0], 1, trainX.shape[1])
array([[[ 4.49027601e+00, -3.71848297e-01, -3.71848297e-01, ...,
1.06175239e+17, 1.24734085e+06, 5.16668131e+00]],
[[ 2.05921386e+00, -3.71848297e-01, -3.71848297e-01, ...,
8.44426594e+17, 1.39098642e+06, 4.01803817e+00]],
[[ 9.25515792e+00, -3.71848297e-01, -3.71848297e-01, ...,
4.08800518e+17, 1.24441013e+06, 3.69129399e+00]],
...,
[[ 3.80037999e+00, -3.71848297e-01, -3.71848297e-01, ...,
1.35414902e+18, 1.23823291e+06, 3.54601899e+00]],
[[ 3.73994822e+00, -3.71848297e-01, 8.40698741e+00, ...,
3.93863169e+17, 1.25693299e+06, 3.29993440e+00]],
[[ 3.56843035e+00, -3.71848297e-01, 1.53710656e+00, ...,
3.28306336e+17, 1.22667253e+06, 3.36569960e+00]]])
列车改型为:
trainY.reshape(trainY.shape[0], )
array([[-0.7238661 ],
[-0.43128777],
[-0.31542821],
[-0.35185375],
...,
[-0.28319519],
[-0.28740503],
[-0.24209411],
[-0.3202021 ]])
而testX则被重塑为:
testX.reshape(1, testX.shape[0], testX.shape[1])
array([[[ -3.71848297e-01, -3.71848297e-01, -3.71848297e-01, ...,
-3.71848297e-01, 2.73982042e+06, -3.71848297e-01],
[ -3.71848297e-01, -3.71848297e-01, -3.71848297e-01, ...,
-3.71848297e-01, 2.73982042e+06, -3.71848297e-01],
[ -3.71848297e-01, -3.71848297e-01, -3.71848297e-01, ...,
2.00988794e+18, 1.05992636e+06, 2.49920150e+01],
...,
[ -3.71848297e-01, -3.71848297e-01, -3.71848297e-01, ...,
-3.71848297e-01, -3.71848297e-01, -3.71848297e-01],
[ -3.71848297e-01, -3.71848297e-01, -3.71848297e-01, ...,
-3.71848297e-01, -3.71848297e-01, -3.71848297e-01],
[ -3.71848297e-01, -3.71848297e-01, -3.71848297e-01, ...,
-3.71848297e-01, -3.71848297e-01, -3.71848297e-01]]])
错误是:
ValueError:检查时出错:预期lstm_25_输入具有形状(无,1,72),但获得具有形状(12895067,72)的数组
编辑1:
以下是我的模型代码:
trainX = trainX.reshape(trainX.shape[0], 1, trainX.shape[1])
trainY = trainY.reshape(trainY.shape[0], )
testX = testX.reshape(1, testX.shape[0], testX.shape[1])
model = Sequential()
model.add(LSTM(100, return_sequences=True, input_shape = trainX.shape[0], trainX.shape[2])))
model.add(LSTM(100))
model.add(Dense(1, activation='linear'))
model.compile(loss='mse', optimizer='adam')
model.fit(trainX, trainY, epochs=500, shuffle=False, verbose=1)
model.save('model_lstm.h5')
model = load_model('model_lstm.h5')
prediction = model.predict(testX, verbose=0)
ValueError Traceback(最近的调用最后)in()43model.compile(损失='mse',优化器='adam')44---
D:\AnacondaIDE\lib\site-包\keras\models.py适合(自我,x,y,batch_size,时代,冗长,回调,validation_split,validation_data,洗牌,class_weight,sample_weight,initial_epoch,**kwargs)865class_weight=class_weight,866sample_weight=sample_weight,-
D:\AnacondaIDE\lib\site packages\keras\engine\training。拟合中的py(自我、x、y、批次大小、年代、详细、回调、验证分割、验证数据、洗牌、类别权重、样本权重、初始纪元、每纪元步长、验证步长、**kwargs)1520
类别权重=类别权重,1521检查批次轴=假-
D:\AnacondaIDE\lib\site packages\keras\engine\training。py在用户数据(自身、x、y、样本重量、类别重量、检查批次轴、批次大小)1376
self中_进给输入形状,1377
检查批次轴=假-
D:\AnacondaIDE\lib\site-包\keras\引擎\training.py_standardize_input_data(数据,名称,形状,check_batch_axis,exception_prefix)142'有形状'str(形状[i])143'但有形状的数组-
ValueError:检查输入时出错:预期lstm_31_输入具有形状(无,250,72),但获得具有形状(21351,1,72)的数组
编辑2:
在尝试了@Paddy的更新解决方案后,我在调用predict()时出现以下错误:
()1模型中的ValueError回溯(最后一次调用)模型=加载模型('modellstm.h5')2----
d:\AnacondaIDE\lib\site-包\keras\models.py在预测(自我,x,batch_size,冗长)911如果不是self.built: 912self.build()-
D:\AnacondaIDE\lib\site-包\keras\Engine\training.py在预测(自我,x,batch_size,详细,步骤)1693 x=_standardize_input_data(x,自我。_feed_input_names,1694自我。_feed_input_shapes,-
D:\AnacondaIDE\lib\site packages\keras\engine\training。py in _standarize_input_data(数据、名称、形状、检查批处理轴、异常前缀)130'具有'str(len(shapes[i])131'维度,但具有形状的数组'--
ValueError:检查时出错:预期lstm_64_输入有3个维度,但得到了具有形状的数组(2895067,72)
你有:
trainX = trainX.reshape(trainX.shape[0], 1, trainX.shape[1])
trainY = trainY.reshape(trainY.shape[0], )
testX = testX.reshape(1, testX.shape[0], testX.shape[1])
你想要:
trainX = trainX.reshape(trainX.shape[0], 1, trainX.shape[1])
trainY = trainY.reshape(trainY.shape[0], )
testX = testX.reshape(testX.shape[0],1, testX.shape[1])
你在testX中混淆了样本和时间步长维度
尝试此重塑:
trainX.reshape(len(trainX),1, trainX.shape[1])
trainY.reshape(len(trainX), 1)
但是,通常有两种方法,要么重塑输入数据,要么改变模型参数。
请看错误信息,上面写的都是!
好的,下面是代码的更新:
trainX = trainX.reshape(trainX.shape[0], trainX.shape[1],1)
trainY = trainY.reshape(trainY.shape[0],)
testX = testX.reshape(testX.shape[0], testX.shape[1], 1)
model = Sequential()
model.add(LSTM(100, return_sequences= True, input_shape=(trainX.shape[1],1) ))
model.add(LSTM(100, return_sequences= False))
model.add(Dense(1, activation='linear'))
model.compile(loss='mse', optimizer='adam')
model.fit(trainX, trainY, epochs=500, shuffle=False, verbose=1)
model.save('model_lstm.h5')
model = load_model('model_lstm.h5')
prediction = model.predict(testX, verbose=0)