How do you predict future values with this model? I've tried changing the timestep window to a higher value greater than the stock database, but I only get an errr saying that the tuple index is out of range. How do I predict future values instead of testing the model on already existing data? Here is my code:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset_train = pd.read_csv(r'/path', error_bad_lines = False)
training_set = dataset_train.iloc[:, 1:2].values
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range = (0, 1))
sc_training_set = sc.fit_transform(training_set)
X_train = []
y_train = []
for i in range (1, 220):
X_train.append(sc_training_set[i-1:i, 0])
y_train.append(sc_training_set[i, 0])
X_train, y_train = np.array(X_train), np.array(y_train)
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
regressor = Sequential()
regressor.add(LSTM(units = 64, return_sequences = True, input_shape = (X_train.shape[1], 1)))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 128, return_sequences = True))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 256, return_sequences = True))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 512, return_sequences = True))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 256, return_sequences = True))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 128, return_sequences = True))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 64))
regressor.add(Dropout(0.2))
regressor.add(Dense(units = 1))
regressor.compile(optimizer = 'adam', loss = 'mean_squared_error', metrics = ['accuracy'])
regressor.fit(X_train, y_train, epochs = 10, batch_size = 32)
dataset_test = []
X_test = []
for i in range(220, 500):
X_test.append(sc_training_set[i-1:i, 0])
X_test = np.array(X_test)
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
pred_stock = regressor.predict(X_test)
pred_stock = sc.inverse_transform(pred_stock)
Here is some pseudo code for future predictions. Essentially, you need to continually add your most recent prediction into your time series.
You can't just increase the size of your timestep or you will end up trying to access indices that are out of bounds.
predictions = []
last_x = (the last x value in your data)
while len(predictions) < #_of_predictions_you_want:
p = model.predict(last_x)
predictions.append(p)
last_x = np.roll(x, -1)
last_x[-1] = p