SANJAY P — Published On May 19, 2021
Deep Learning Intermediate Time Series Forecasting

This article was published as a part of the Data Science Blogathon 


Trying to predict how the securities exchange will work is one of the most difficult tasks. There are so many variables involved with the expectation – physical elements versus psychological factors, rational and irrational behaviour, and so on.
All of these factors combine to make share costs unpredictable and difficult to predict with any degree of certainty.
Is it possible to use AI to our advantage in this space?AI approaches will potentially reveal examples and insights we hadn’t seen before, and these can be used to make unerringly exact expectations, using features like the most recent declarations of an organization, their quarterly income figures, and so on.
We will work with published information regarding a freely recorded organization’s stock costs in this report.
We’ll use a combination of AI calculations to forecast this company’s future stock price with LSTM.
This article’s main purpose is to demonstrate how these calculations are carried out. I’ll provide a quick overview of the process and make key connections to revisit the concepts as needed. If you’re new to the world of time management, I suggest starting with the articles below.

Table of Contents:

  1. Loading the Data
  2. Train and Test Split
  3. Data Preprocessing
  4. LSTM
  5. Prediction
  6. Conclusion

Loading the Data

The Apple data is up to 22-05-2020. Let’s take the close column for the stock prediction. We can use the same strategy.
We should reset the index
so that the data will be clear
Let us plot the Close value graph using pyplot
From 2015-2020
stock price prediction lstm plot data

Now get into the Solution:

LSTM is very sensitive to the scale of the data, Here the scale of the Close value is in a kind of scale, we should always try to transform the value.

Here we will use min-max scalar to transform the values from 0 to 1.We should reshape so that we can use fit transform.


from sklearn.preprocessing import MinMaxScaler

Train and Test Split

Whenever training Timeseries data we should divide the data differently we should train the data with the respective date.

Always remember that in time-series data the one data is dependent on other data. The training size should be 65% of the total length of the data frame, the test size should be the difference between the length of the dataset and the training size.



Train data and Test data is ready.

Data Preprocessing

Now consider the time steps, if I want to predict the price of the stock in a day that how previous data should be considered.

Now the timestep value will be 100. Let’s split the data X, Y. In the 0th iteration the first 100 elements goes as your first record and the 101 elements will be put up in the X. The 100 elements will be put up in the Y.


import numpy
def create_dataset(dataset, time_step=1):
dataX, dataY = [], []
for i in range(len(dataset)-time_step-1):
a = dataset[i:(i+time_step), 0]
dataY.append(dataset[i + time_step, 0])
return numpy.array(dataX), numpy.array(dataY)
time_step = 100
X_train, y_train = create_dataset(train_data, time_step)
X_test, ytest = create_dataset(test_data, time_step)


LSTMs are widely used for sequence prediction problems and have proven to be extremely effective. The reason they work so well is that LSTM can store past important information and forget the information that is not.
LSTM has three gates:
  • The input gate: The input gate adds information to the cell state,
  • The forget gate: It removes the information that is no longer required by the model,
  • The output gate: Output Gate at LSTM selects the information to be shown as output.


While Implementing any LSTM, we should always reshape our X train in 3-D, add 1 the reason behind is the time step and the 1 is given to the LSTM.


X_train =X_train.reshape(X_train.shape[0],X_train.shape[1] , 1)
X_test = X_test.reshape(X_test.shape[0],X_test.shape[1] , 1)

Then import required modules for the stacked LSTM.


from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM

We will be using a sequential model and adding the layers of the LSTM as said, in the above sentence. The first layer should be the time step in 1.



Let’s see the summary.

Now the final part is to fit the X_train and the y_train.


Predict both the X_train and the X_test, now let’s scaler inverse transform because I want to see the root mean square performance.




import math
from sklearn.metrics import mean_squared_error

Here the time step is 100, Whatever the values in train predict and test predict. I got I am just plotting it don’t forget we have to inverse the scaler transform.

stock price prediction lstm prediction


  • Green indicates the Predicted Data
  • Blue indicates the Complete Data
  • Orange indicates the Train Data


If I consider the last date in the test data as of 22-05-2020, I want to predict the output of 23-05-2020. We need the previous 100 data for that I am taking the data and reshaping it.



So, you can predict the prices of preferred stocks using this strategy.


Oh my goodness! Various parameters of the LSTM model can be tweaked, such as the number of LSTM layers, the dropout value, and the number of epochs. Are the LSTM projections, however, precise enough to predict whether the stock price will rise or fall? Without a doubt.
As I stated at the outset of this article, stock prices are influenced by company news as well as other factors such as demonetization or company mergers and demergers. In addition, many intangible variables are difficult to predict in advance.


In this article we have seen how to predict a stock price, this is a simple algorithm. You can make use of auto-ml so that the adding of new data will be easy. Refer a lot of Deep Learning Algorithms, Machine Learning … etc.

Thanks for spending your timing in reading the article.

The media shown in this article are not owned by Analytics Vidhya and is used at the Author’s discretion. 

One thought on "Stock Price Prediction and Forecasting using Stacked LSTM."

andrey says: September 12, 2022 at 10:05 pm
I think you have a leak in the data scaling you should scale the training and testing data seperatly Reply

Leave a Reply Your email address will not be published. Required fields are marked *