Implementing LSTM For Stock Price Prediction (Time Series)
By: Mayank Yogi
Tags: Machine Learning, Deep Learning, Time Series Forecasting, Data Science, Lstm

Stock Price (Image by Shutter Stock)

A stock price is a given for every share issued by a publicly traded company. The price is a reflection of the company’s value — what the public is willing to pay for a piece of the company. It can and will rise and fall, based on a variety of factors in the global landscape and within the company itself.

You can download the data set and code from this Link

What is LSTM

Long short-term memory is an artificial recurrent neural network architecture used in the field of deep learning. Unlike standard feed forward neural networks, LSTM has feedback connections. It can not only process single data points, but also entire sequences of data.

Insight of LSTM Architecture (Image by towardsdatascience)

Major Fields/Columns:

Date — The date of the observation

Symbol — There are 2 Bank in this Data Set UTI Bank and Axis Bank

Open— The Price when the stock is open at particular day

Close — The Price when the stock is close at the end of day

High — Means the highest price in a given period of time

Low — Means the lowest price in a given period of time

Volume — the number of shares of a security traded during a given period of time, The higher the volume during a price move, the more significant the move

Problem description:

In this model i will choose Open as input variable and take first 60 values as input and try to predict 61 value and try to visualize the result through graph.

This will help to predict the future stock price of any company(only for Learning Purpose)

Importing Library:

Importing all Library (Image by author)

I am importing the all library which I required for EDA, visualization, prediction . The reason of doing this is that it become easier to use all the import statement at one go and we do not require to import the statement again at each point. We could find all the importing statement at one place without finding it on whole notebook and can update also.

Here I am loading the data set into a variable i.e. “df” and processing the first 5 rows.

Removing Null Values:

Image by author

In the data set there are lots of null values , so there are various ways to remove the null values, i just replace by its mean value

Making data set balance and well structured is best practice

Plotting Graph

Line plot for “Open” (Image by author)

Here we can take any feature and try to check through visualization that what is the analysis and flow of the particular feature

I am doing with feature“Open” because i will generate model using feature “Open”.

Long Short Term Memory networks — usually just called “LSTMs” — are a special kind of RNN, capable of learning long-term dependencies. They were introduced by Hochreiter & Schmidhuber (1997), and were refined and popularized by many people in following work.1 They work tremendously well on a large variety of problems, and are now widely used.

LSTMs are explicitly designed to avoid the long-term dependency problem. Remembering information for long periods of time is practically their default behavior, not something they struggle to learn!

Image by author

Now i created a new data frame called “data” which include all the values of “Open” feature and then convert the data frame into numpy array

Now we have to train the data so for that i am using 80% of the total data and assign to variable name “training_data_len”

Now i am scaling the data between 0 to 1, As MinMaxScaler preserves the shape of the original distribution. It doesn’t meaningfully change the information embedded in the original data. Note that MinMaxScaler doesn’t reduce the importance of outliers. The default range for the feature returned by MinMaxScaler is 0 to 1

Image by author

Now create scaled training data set named as “train_data” which contain the scaled data from index 0 to training_data_len including all features

Also split the data into train and test i.e train_x and train_y , where train_x contains all the independent features and train_y contain all dependent features or target variable

Now i used for loop starting from range 60 to the length of train_data and assigning the passed 60 values to the train_x which means it contains values from 0 to 59 and train_y contain from value 60 to length of train_data

NOTE : Index of array starts from 0

Now i am printing the train_x and train_y value and in the above figure we clearly see that , train_x contain first 60 values and train_y contain the 61 values its pretty simple

if we increase the value of index i then it again show the next 60 value in train_x and 61 value in train_y , try to use “i< = 62” and then check the array you will get better insight.

Training Data Set (Image by author)

Now converting the train_x and train_y to the numpy array so that we can use them to train the LSTM model and then checking the shape of train_x i.e 2D array shape

But LSTM model takes 3D array that is no of sample , no of features and timestamps but now our train_x only contain no of rows i.e 4018 and timestamps i.e 60 so we have to reshape it to 3D array

On reshaping i am taking the train_x and train_x.shape[0] which is nothing but value = 4018 , train_x.shape[1] = 60 and feature as 1, because i am using only “Open” feature for prediction

Now we are ready with our training set and now i am creating LSTM model

Parameters :

units = 50 ,this is the number of neurons , we can give any number of neurons

return_sequences = True, means i have to use another LSTM layer so i am assign it as True, if we have single layer or last layer then assign it as False

input_shape = As for first layer i have to provide the input shape i.e (60,1)

I am also using the Dropout, at each iteration 0.2 of total neurons will get drop out from the model

And at last i am using Dense layer just to make the fully connected layer so taking neurons as 25 and at last taking only 1 neurons at output side

Flowchart of Model (Image by author)

So i am using graphviz library to make the flowchart of our model architecture, this flowchart will help when we have multiple layers may be more then 15 and by seeing the flowchart we can easily understand the model architecture

Image by author

Now i am compiling my model and uses optimizer as Adam , though the problem is regression so i am using loss as mean_squared_error

After that fitting the model and taking batch size which is total no of training examples present in single batch that is 32 and epochs is no of iteration for entire data set in forward and backward direction, i am using 3 epochs

Batch size and epochs vary according to data set

So we are done with training the model and the loss is also minimum,we can minimize the loss by increasing the epochs value, less the loss better will be the model

Now creating the test set for prediction

Test Data (Image by author)

Now i am creating a new array containing the scaled values from 4018 to 5097 that is test_data also contain all the feature

Now i am creating the data sets i.e test_x and test_y

test_y will contains all the value which our model need to predict

test_x will contain all the passed 60 values means from 0 to 59 index values and then convert the test_x into an numpy array same as train_x and then reshaping the array into 3D shape

text_x = (total rows -rest 20% of total rows-60) = (5097–4018–60) = 1019, i guess now it clear

so we assign the test_x to our training model and also we want our result is to match with the original form so i did the inverse scaling to make the value match with test_y and after that i am calculating the root mean squared error

Prediction Graph (Image by author)

Now in above graph we can clearly see that the actual line and prediction line are similar, not exact but very close to each other , which means our prediction is fine.

So this is the time series model where we are taking the first 60 values to predict the 61 value and so on .

For Further Query you may connect me on my LinkedIn