Search code examples
pythondeep-learninglstmprediction

lSTM: how to make multiple days prediction


I have been following this tutorial. However I'm not able to find proper Stock prediction model that takes multiple input columns and based on that give output for multiple days.

In above tutorial I have got the result for single days. but since my model is of 50X5 dimensions after predicting it gives me output for single output column and for single day only. How can i make it for multiple days.

Please suggest me any better tutorial url which takes multiple input and give output for multiple days

Or suggest changes in my code

Here is my code:

print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)

(480, 50, 5) (480,) (132, 50, 5) (132,)

model = Sequential()
n_neurons = x_train.shape[1] * x_train.shape[2]
print(n_neurons, x_train.shape[1], x_train.shape[2])
model.add(LSTM(n_neurons, return_sequences=True, input_shape=(x_train.shape[1], x_train.shape[2]))) 
model.add(LSTM(n_neurons, return_sequences=False))
model.add(Dense(5))
model.add(Dense(1))

model.compile(optimizer='adam', loss='mse')
epochs = 100
batch_size = 16
early_stop = EarlyStopping(monitor='loss', patience=5, verbose=1)
history = model.fit(x_train, y_train, 
                    batch_size=batch_size, 
                    epochs=epochs,
                    validation_data=(x_test, y_test)
                   )
pred_price_scaled = model.predict(np.array(to_predict_scaled))
pred_price_unscaled = scaler.inverse_transform(pred_price_scaled.reshape(-1, 1))

price_today = np.round(data.loc[data.index[-1], "Close Price"])
predicted_price = np.round(pred_price_unscaled.ravel()[0], 2)
change_percent = np.round(100 - (price_today * 100)/predicted_price, 2)

print(f'The predicted close price is {predicted_price} ({"+" if change_percent > 0 else "-"}{change_percent}%)  today={price_today}')

Output: The predicted close price is 1322.5 (-0.72%) today=1332.0

I have tried to make prediction for 30days by following code. But since model.predict takes 50X5 dimension input I cannot append my next day output(which is 1X1 Dimention) into this

while(i<30):
    yhat = model.predict(np.array(X_test_new))
    X_test_new.extend(yhat[0].tolist())
    lst_output.extend(yhat.tolist())
    i=i+1

Solution

  • You need to make two changes:

    First, change the input format of both training and test data to include several time steps as the target. So the target is not a data point but a series of points. For 5 time steps, the shape of the data needs to look like this:

    (480, 50, 5) (480, 5) (132, 50, 5) (132, 5)
    

    Second, change the number of outputs in the final dense layer of the network. The steps need to correspond with the format of your training and test data. You can simply delete the last dense layer so that the final layer of your CNN looks like this:

    model.add(Dense(5))
    

    Here is an example from relataly.com:

    multivariate multi-ouput predictions