title: Predicting Future values with Keras LSTM
body: <p>I have created an LSTM sales prediction model that works really well on the train and test sets. I would now like to predict beyond the dates in the entire dataset.</p>
  
  <p>I have tried following this answer <a href="https://stackoverflow.com/questions/48760472/how-to-use-the-keras-model-to-forecast-for-future-dates-or-events">how to use the Keras model to forecast for future dates or events?</a> but I really can't figure out how to adjust my code to do future predictions.</p>
  
  <p>Also, I changed my code from</p>
  
  <pre><code>X_train, y_train = train_set_scaled[:, 1:], train_set_scaled[:, 0:1]
  X_train = X_train.reshape(X_train.shape[0], 1, X_train.shape[1])
  X_test, y_test = test_set_scaled[:, 1:], test_set_scaled[:, 0:1]
  X_test = X_test.reshape(X_test.shape[0], 1, X_test.shape[1])
  </code></pre>
  
  <p>to</p>
  
  <pre><code>X_train, y_train = train_set_scaled[:, 1:], train_set_scaled[:, 1:8]
  X_train = X_train.reshape(X_train.shape[0], 1, X_train.shape[1])
  X_test, y_test = test_set_scaled[:, 1:], test_set_scaled[:, 1:8]
  X_test = X_test.reshape(X_test.shape[0], 1, X_test.shape[1])
  </code></pre>
  
  <p>after trying the solution in <a href="https://stackoverflow.com/questions/53252152/keras-time-series-can-i-predict-next-6-month-in-one-time">Keras time series can I predict next 6 month in one time</a> </p>
  
  <p>Here is the code where training and modelling happens:</p>
  
  <pre><code># changed to initial
  for df in m:
      train_set, test_set = m[df][0:-6].values, m[df][-6:].values
  
      #apply Min Max Scaler
      scaler = MinMaxScaler(feature_range=(-1, 1))
      scaler = scaler.fit(train_set)
  
      # reshape training set
      train_set = train_set.reshape(train_set.shape[0], train_set.shape[1])
      train_set_scaled = scaler.transform(train_set)
  
      # reshape test set
      test_set = test_set.reshape(test_set.shape[0], test_set.shape[1])
      test_set_scaled = scaler.transform(test_set)
  
      #build the LSTM Model
      X_train, y_train = train_set_scaled[:, 1:], train_set_scaled[:, 0:1]
      X_train = X_train.reshape(X_train.shape[0], 1, X_train.shape[1])
      X_test, y_test = test_set_scaled[:, 1:], test_set_scaled[:, 0:1]
      X_test = X_test.reshape(X_test.shape[0], 1, X_test.shape[1])
  
      print('Fitting model for: {}'.format(df))
  
      #fit our LSTM Model
      model = Sequential()
      model.add(LSTM(4, batch_input_shape=(1, X_train.shape[1], X_train.shape[2]), stateful=True))
      model.add(Dense(1))
      model.compile(loss='mean_squared_error', optimizer='adam')
      model.fit(X_train, y_train, nb_epoch=500, batch_size=1, verbose=1, shuffle=False)
  
  #     model.save('lstm_model.h5')
  
  
      print('Predictions for: {}'.format(df))
  
      #check prediction
      y_pred = model.predict(X_test,batch_size=1)
  
      print('Inverse Transform for: {}'.format(df))
  
      #inverse transformation to see actual sales
      #reshape y_pred
      y_pred = y_pred.reshape(y_pred.shape[0], 1, y_pred.shape[1])
      #rebuild test set for inverse transform
      pred_test_set = []
      for index in range(0,len(y_pred)):
          print (np.concatenate([y_pred[index],X_test[index]],axis=1))
          pred_test_set.append(np.concatenate([y_pred[index],X_test[index]],axis=1))
      #reshape pred_test_set
      pred_test_set = np.array(pred_test_set)
      pred_test_set = pred_test_set.reshape(pred_test_set.shape[0], pred_test_set.shape[2])
      #inverse transform
      pred_test_set_inverted = scaler.inverse_transform(pred_test_set)
  
  
  </code></pre>
  
  <p>I would like the predictions to go beyond the data in the dataset.</p>
  
  <p><strong>UPDATE</strong>: I trained the model and took its predictions on the test set. Use these as input for another LSTM model to fit and predict for 12 months. It worked for me. Also changed my last Dense layer (<strong>above</strong>) to predict 1 point at a time instead of 7 as I had before.
  <em>Below is the</em> <strong>code</strong>:</p>
  
  <pre><code>from numpy import array
  for df in d:
      if df in list_df:
          # df_ADIDAS DYN PUL DEO 150 FCA5421
          #KEEP
  
          result_list = []
          sales_dates = list(d["{}".format(df)][-7:].Month)
          act_sales = list(d["{}".format(df)][-7:].Sale)
          for index in range(0,len(pred_test_set_inverted)):
              result_dict = {}
              result_dict['pred_value'] = int(pred_test_set_inverted[index][0] + act_sales[index]) #change to 0 ffrom act_sales[index]
              result_dict['date'] = sales_dates[index] #&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;REVIEW
              result_list.append(result_dict)
  
          df_result = pd.DataFrame(result_list)
          predictions = list(df_result['pred_value'])
  
          forecasts = []
          result_list
          for i in range(len(result_list)):
              forecasts.append(result_list[i]['pred_value'])
  
          def split_sequence(sequence, n_steps):
              X, y = list(), list()
              for i in range(len(sequence)):
                  # find the end of this pattern
                  end_ix = i + n_steps
                  # check if we are beyond the sequence
                  if end_ix &gt; len(sequence)-1:
                      break
                  # gather input and output parts of the pattern
                  seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
                  X.append(seq_x)
                  y.append(seq_y)
              return array(X), array(y)
  
          # choose a number of time steps
          n_steps = 4
          # split into samples
          X, y = split_sequence(forecasts, n_steps)
          # summarize the data
          #     for i in range(len(X)):
          #         print(X[i], y[i])
  
          n_features = 1
          X = X.reshape((X.shape[0], X.shape[1], n_features))
          # define model
          model = Sequential()
          model.add(LSTM(50, activation='relu', input_shape=(n_steps, n_features)))
          model.add(Dense(1))
          model.compile(optimizer='adam', loss='mse')
          # fit model
          model.fit(X, y, epochs=200, verbose=0)
          # demonstrate prediction
          x_input = array(predictions[-4:])
          x_input = x_input.reshape((1, n_steps, n_features))
          yhat = model.predict(x_input, verbose=0)
          #print(yhat)
  
          currentStep = yhat[:, -1:]
  
          print('Twelve Month Prediction for {}'.format(df))
          for i in range(12):
              if i == 0:
                  x_input = x_input.reshape((1, n_steps, n_features))
                  yhat = model.predict(x_input, verbose=0)
                  print(yhat)
              else:
                  x0_input = np.append(x_input, [currentStep[i-1]])
                  x0_input = x0_input.reshape((1, n_steps+1, n_features))
                  x_input = x0_input[:,1:]
                  yhat = model.predict(x_input)
                  currentStep = np.append(currentStep, yhat[:,-1:])
                  print(yhat)
  </code></pre>