如何在多变量时间序列 LSTM 模型中逆变换预测值



>我正在建立一个多变量时间序列 LSTM 模型,其中我使用 9 个变量的历史数据和 3 个时间步长作为我的输入。我的输入维度如下:

X_train_reshape  = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 9))
X_test_reshape   = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 9))
print(X_train.shape,y_train3.shape, X_test.shape, y_test3.shape)
(1744, 3, 9) (1744, 1) (434, 3, 9) (434, 1)

我将输入缩放到(0,1(之间。

scaler = MinMaxScaler(feature_range=(0, 1))
scaler = scaler.fit(train)
train = scaler.transform(train)
test  = scaler.transform(test)

似乎我的模型正在工作并成功预测目标变量。但是,当我尝试逆变换目标变量时,我收到以下错误。

yhat_inv = scaler.inverse_transform(model.predict(X_train)).flatten()
"ValueError: non-broadcastable output operand with shape (1744,1) doesn't match the broadcast shape (1744,9)"

如何对预测值进行逆变换?

此代码用于预测测试集和逆向缩放

yhat = model.predict(test_X)
test_X = test_X.reshape((test_X.shape[0], test_X.shape[2]))
# invert scaling for forecast
inv_yhat = concatenate((yhat, test_X[:, 1:]), axis=1)
inv_yhat = scaler.inverse_transform(inv_yhat)
inv_yhat = inv_yhat[:,0]
# invert scaling for actual
test_y = test_y.reshape((len(test_y), 1))
inv_y = concatenate((test_y, test_X[:, 1:]), axis=1)
inv_y = scaler.inverse_transform(inv_y)
inv_y = inv_y[:,0]
# calculate RMSE
rmse = sqrt(mean_squared_error(inv_y, inv_yhat))
print('Test RMSE: %.3f' % rmse)

最新更新