Home » Python » python – Sequential LSTM with empty prediction plot?-Exceptionshub

python – Sequential LSTM with empty prediction plot?-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

Im running into an issue with the below LSTM and plot, the dataset is stock data per minute for one day. The training is everything but the last 3 minutes of data and the test data is the remainder. I use a scaler to fit the training and test data.

I am unsure if I have correctly set the inputs and features for the timeseries generator however it runs. Upon plotting the output for the normal data it shows correctly from the image below, however the prediction data is not plotted at all?

I am also having an issue with the plots horizontal features and I am unsure how to plot just the minutes instead of the date and time as they overlap? Please note timestamp is the index in this instance.

train, test = df[:-3], df[-3:]
scaler = MinMaxScaler()
scaler.fit(train)
train = scaler.transform(train)
test = scaler.transform(test)

n_input = 1
n_features = 4

generator = TimeseriesGenerator(train, train, length=n_input, batch_size=1)

model = Sequential()
model.add(LSTM(200, activation='relu', input_shape=(n_input, n_features)))
model.add(Dropout(0.15))
model.add(Dense(4))
model.compile(optimizer='adam', loss='mse')
model.fit_generator(generator, epochs=180)

pred_list = []

batch = train[-n_input:].reshape((1, n_input, n_features))

for i in range(n_input):
    pred_list.append(model.predict(batch)[0])
    batch = np.append(batch[:,1:,:],[[pred_list[i]]], axis=1)

df_predict = pd.DataFrame(scaler.inverse_transform(pred_list), index=df[-n_input:].index, columns=['OpenPredict','HighPredict','LowPredict','ClosePredict'])

df_test = pd.concat([df, df_predict], axis=1)

plt.figure(figsize=(20, 20))
plt.plot(df_test.index, df_test['open'])
plt.plot(df_test.index, df_test['OpenPredict'], color='r')
plt.show()

Data:
enter image description here
Output of plt.plot(df_test.index, df_test['open'])
enter image description here
Output of plt.plot(df_test.index, df_test['OpenPredict'], color='r')
enter image description here

How to&Answers: