title: rescale and inverse_transform not returning predicted and actual values in original scale for LSTM multivariate time-series forecast
body: <p>I'm currently building an LSTM multivariate time-series model to predict one output at current time (t) using 22 features from the previous timestamp (t-1) as inputs. I've been following the instructions from <a href="https://machinelearningmastery.com/multivariate-time-series-forecasting-lstms-keras/" rel="nofollow noreferrer">this example</a>, and everything seems to be working correctly, but the one issue I'm having involves the final inverse_transform() functions.</p>
  
  <p>Specifically, I'm expecting that once I invert the scaling for both the forecasted values and the actual values, the output will be in the same units as the original data before I normalized and transformed my dataset. The variable I'm trying to predict has a mean of around 29.186, but when I run inverse_transform() and plot the predictions against the actuals, the output of my results ranges from 3.30 - 3.55.</p>
  
  <p>It could be that I've made a simple error slicing the array, or it could be something completely different, but I can't seem to identify the root cause of why my predicted and actual values aren't in the same units as the data before I transformed everything.</p>
  
  <p>For additional context, here's the printed output of train_X.shape, train_y.shape, test_X.shape, test_y.shape: </p>
  
  <pre><code>(1804950, 1, 22) (1804950,) (849389, 1, 22) (849389,)
  </code></pre>
  
  <p>Here's the relevant parts of my code:</p>
  
  <pre><code># ensure all data is float
  values = ips_data.values.astype('float32')
  
  # normalize features
  scaler = MinMaxScaler(feature_range=(0, 1))
  scaled = scaler.fit_transform(values)
  
  # frame as supervised learning
  ips_reframed = series_to_supervised(scaled, 1, 1)
  
  # drop a bunch of columns I don't want to predict
  ips_reframed.drop(ips_reframed.columns[[22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,42,43]], axis=1, inplace=True)
  # print(ips_reframed.head())
  
  # split into train and test sets
  values = ips_reframed.values
  # n_train_hours = (365 * 24 * 60) * 3
  n_train_hours = int(len(values) * 0.68)
  train = values[:n_train_hours, :]
  test = values[n_train_hours:, :]
  
  # split into input and outputs
  train_X, train_y = train[:, :-1], train[:, -1]
  test_X, test_y = test[:, :-1], test[:, -1]
  
  # reshape input to be 3D [samples, timesteps, features]
  train_X = train_X.reshape((train_X.shape[0], 1, train_X.shape[1]))
  test_X = test_X.reshape((test_X.shape[0], 1, test_X.shape[1]))
  # print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)
  
  # design network
  model = Sequential()
  model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2]), return_sequences=True))
  model.add(Dropout(0.5))
  model.add(LSTM(50))
  model.add(Dropout(0.5))
  model.add(Dense(1))
  model.add(Activation("linear"))
  model.compile(loss='mae', optimizer='adam')
  
  # fit network
  history = model.fit(train_X, train_y, epochs=100, batch_size=1000, 
  validation_data=(test_X, test_y), verbose=2, shuffle=False)
  
  # plot history
  plt.plot(history.history['loss'], label='train')
  plt.plot(history.history['val_loss'], label='test')
  plt.legend()
  plt.show()
  
  # make a prediction
  yhat = model.predict(test_X)
  
  # invert scaling for forecast
  test_X = test_X.reshape((test_X.shape[0], test_X.shape[2]))
  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)
  
  # plotting the predictions
  plt.plot(inv_yhat[-100:], label='predictions')
  plt.plot(inv_y[-100:], label='actual')
  plt.title("Prediction vs. Actual")
  plt.legend()
  plt.show()
  </code></pre>
  
  <p>Please let me know if you need any additional information or context, and thank you so much for your help!</p>