【发布时间】:2017-10-16 12:44:00
【问题描述】:
我有 250 天的数据、72 个训练样本特征和一列目标变量。并希望为 21351 行中的每一行预测接下来的 30 天,其中包含 72 个特征。我将如何重塑我的输入和输出数据。似乎我有点困惑,图书馆给了我关于形状不兼容的错误。
我正在重塑:
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 改写为:
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_input 具有形状 (None, 1, 72) 但得到了形状为 (1, 2895067, 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(最近调用 最后)在() 43 model.compile(损失='mse',优化器='adam') 44 ---> 45 model.fit(exog, endog, epochs=50, shuffle=False, verbose=1) 46 47 start_date = endog_end + timedelta(days = 1)
D:\AnacondaIDE\lib\site-packages\keras\models.py in fit(self, x, y, batch_size、epochs、verbose、回调、validation_split、 验证数据,洗牌,类权重,样本权重,初始时期, **kwargs) 第865章 第866章 --> 867 初始时期=初始时期) 868 第869章
D:\AnacondaIDE\lib\site-packages\keras\engine\training.py in fit(self, x,y,batch_size,epochs,详细,回调,validation_split, 验证数据,洗牌,类权重,样本权重,初始时期, steps_per_epoch, validation_steps, **kwargs) 1520
类重量=类重量,1521 check_batch_axis=假, -> 1522 batch_size=batch_size) 1523 # 准备验证数据。第1524章D:\AnacondaIDE\lib\site-packages\keras\engine\training.py 在 _standardize_user_data(self, x, y, sample_weight, class_weight, check_batch_axis, batch_size) 1376
self._feed_input_shapes,1377
check_batch_axis=False, -> 1378 exception_prefix='input')1379 y = _standardize_input_data(y,self._feed_output_names,
1380 输出形状,D:\AnacondaIDE\lib\site-packages\keras\engine\training.py 在 _standardize_input_data(数据、名称、形状、check_batch_axis、exception_prefix) 142 ' 具有形状 ' + str(shapes[i]) + 143 ' 但得到了形状为 ' + 的数组 --> 144 str(array.shape)) 145个返回数组 146
ValueError:检查输入时出错:预期 lstm_31_input 有 形状 (None, 250, 72) 但得到了形状 (21351, 1, 72) 的数组
编辑 2:
在尝试@Paddy 的更新解决方案后,我在调用 predict() 时收到此错误:
ValueError Traceback(最近调用 最后)在() 1 模型 = load_model('model_lstm.h5') 2 ----> 3 预测 = model.predict(exog_test, verbose=0) 4 # for x in range(0, len(exog_test)):
D:\AnacondaIDE\lib\site-packages\keras\models.py in predict(self, x, batch_size,详细) 911如果不是self.built: 第912章 --> 913 返回 self.model.predict(x, batch_size=batch_size, verbose=verbose) 914 915 def predict_on_batch(self, x):
D:\AnacondaIDE\lib\site-packages\keras\engine\training.py 在 预测(自我,x,batch_size,详细,步骤)1693 x = _standardize_input_data(x,self._feed_input_names,1694 self._feed_input_shapes, -> 1695 check_batch_axis=False) 1696 if self.stateful: 1697 if x[0].shape[0] > batch_size 和 x[0].shape[0] % batch_size != 0:
D:\AnacondaIDE\lib\site-packages\keras\engine\training.py 在 _standardize_input_data(数据、名称、形状、check_batch_axis、exception_prefix) 130 ' 拥有 ' + str(len(shapes[i])) + 131 ' 尺寸,但得到了形状为 ' + 的数组 --> 132 str(array.shape)) 133 for j, (dim, ref_dim) in enumerate(zip(array.shape, shapes[i])): 134 如果不是 j 也不是 check_batch_axis:
ValueError:检查时出错:预期 lstm_64_input 有 3 尺寸,但得到了形状为 (2895067, 72) 的数组
【问题讨论】:
-
我已经成功训练了模型。但它在调用 predict() 时给了我错误。
-
您将错误的尺寸传递给 testX
-
@djk47463 那么你能提供我重塑 testX 的路线吗?这就是我正在努力解决的问题。
-
主要关注的是根据需求得到想要的输出。
-
@djk47463 在聊天讨论中,您建议了一个连接和重塑数组的步骤。但它不适用于我的原始数据集。我有 274 行和 72 个特征,并且想要制作长度为 92 的时间步长。但它给了我关于大小不兼容的错误。请帮帮我。
标签: python-3.x time-series keras lstm forecasting