I have fitted a LSTM model first using keras.

Data:I have a time series with 560 observations. From that I trained the model using first 500 observations and then evaluated the model using last 60 observations.

I have only one predictor(X) where X is the lag price of the time series. So basically I am trying to predict the current price from the previous days price by fitting a LSTM model.

I used the following setup to fit the model:

model = Sequential()
model.add(LSTM(32,  activation = 'relu', return_sequences = True,input_shape=(dataXtrain.shape[1], dataXtrain.shape[2])))

model.compile(loss='mean_absolute_error', optimizer='adam')
model.fit(dataXtrain, dataYtrain, epochs=1000, batch_size=16) 

Then I evaluated the model for test data. The comparison between the actual price and the predicted price for test data looks like this:

enter image description here

Based on the above plot, It seems that the model is kind of capturing the shape of the actual price. But it is underestimating the price all the time.

Can anyone suggest anything to improve this model?

I am beginner to model fitting in deep learning. So any help will be highly useful.

Thank you.

Update I tried following setup and the results improved drastically:

model = Sequential()
model.add(LSTM(50,  activation = 'relu', return_sequences = True,input_shape=(dataXtrain.shape[1], dataXtrain.shape[2])))
model.compile(loss='mean_absolute_error', optimizer='adam')
model.fit(dataXtrain, dataYtrain, epochs=1000, batch_size=16)

enter image description here

However it seems that the predicted price capturing the actual trend after some delay. Will it be possible to fix this?

Any advice will be really helpful.


I am updating the question with the code that I used to obtain the fitted values and make the plot.

First I Stored the predictions based on test data:

 y_pred = model.predict(dataXtest)

When I am fitting the model, I reshaped the data so that it is a subsequence of 2 observations. (For an example 500 training observations are reshaped as (250,2,1). Also the 60 test data reshaped as (30,2,1) )

Then I reshaped the predictions to the original form.


0   5933    5837.184570
1   5991    5851.091797
2   6023    5947.239258
3   5972    5939.657227
4   6028    5928.400391
5   5429    5944.728516
6   5697    5390.026367
7   5841    5619.818359
8   5980    5798.516602
9   6548    5897.926758
10  6928    6499.494141

This is how I plotted the data:

plt.plot(dataYtest, color = 'red', label = 'Actual Price')
plt.plot(y_pred, color = 'blue', label = 'Predicted Price')
plt.title('Test data')

1 Answer 1


It looks like your indices for the predicted data are off by one. You could try to fix it with

predicted = predicted[1:]
  • $\begingroup$ Hi Thank you for the answer. Can you explain how is it possible ? I used model.predict(dataXtest) to obtain the predictions. $\endgroup$ Oct 18, 2020 at 13:38
  • $\begingroup$ It could be because your training data is off by one, some issue with the code you use for plotting or something else entirely. $\endgroup$
    – Tom Dörr
    Oct 18, 2020 at 13:46
  • $\begingroup$ I updated the question with more information regarding how I stored predictions and how I did the plotting. Please have a look. Thank you. $\endgroup$ Oct 18, 2020 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.