【问题标题】:How to inverse transform the predicted values in a multivariate time series LSTM Model如何对多元时间序列 LSTM 模型中的预测值进行逆变换
【发布时间】:2019-12-04 14:27:50
【问题描述】:

我正在建立一个多元时间序列 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)"

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

【问题讨论】:

    标签: python-3.x keras scikit-learn


    【解决方案1】:

    This code is for prediction of test set and inverse scaling

    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)
    

    【讨论】:

      猜你喜欢
      • 2019-12-01
      • 2019-09-01
      • 1970-01-01
      • 2021-10-21
      • 2020-06-29
      • 1970-01-01
      • 2020-03-03
      • 2017-10-03
      相关资源
      最近更新 更多