Predicting Sequential Data using LSTM: An Introduction
By: George V Jose
Tags: Machine Learning, Neural Networks, Time Series Analysis, Data Analysis, Forecasting

Implementation

I have used colab to implement this code to make the visualizations easier, you can use your preferred method. We’ll start off with importing necessary libraries:

After you’ve downloaded your .csv file from yahoo finance or your source, load the data using pandas.

The info of dataframe shows somewhat like this:

<class 'pandas.core.frame.DataFrame'>

RangeIndex: 3797 entries, 0 to 3796

Data columns (total 7 columns):

Date 3797 non-null object

Open 3797 non-null float64

High 3797 non-null float64

Low 3797 non-null float64

Close 3797 non-null float64

Adj Close 3797 non-null float64

Volume 3797 non-null int64

dtypes: float64(5), int64(1), object(1)

memory usage: 207.7+ KB

For this tutorial, we require only Date and Close columns, everything else can be dropped.

Before we do the training and predictions, let's see how the data looks like. For all the visualizations, I’m using the Plotly python library. Why Plotly?… cause its simply the best graphing library and it can produce some good looking graphs.

With plotly, we can define a trace and the layout and it does everything else.

The graph is oscillating from 2018 and the sequence is not smooth… Moving on.

Data Preprocessing

For our analysis, let train the model on the first 80% of data and test it on the remaining 20%.

Before we do the training, we need to do some major modification to our data. Remember, our data is still a sequence.. a list of numbers. The neural network is trained as a supervised model. Thus we need to convert the data from sequence to supervised data 😱.

Let me explain, training a neural network of any machine learning model requires the data to be in {<features>,<target>} format. Similarly, we need to convert the given data into this format. Here, we introduce a concept of a look back.

Look back is nothing but the number of previous days’ data to use, to predict the value for the next day. For example, let us say look back is 2; so in order to predict the stock price for tomorrow, we need the stock price of today and yesterday.

Coming back to the format, at a given day x(t), the features are the values of x(t-1), x(t-2), …., x(t-n) where n is look back.

So if our data is like this,

[2, 3, 4, 5, 4, 6, 7, 6, 8, 9]

the required data format (n=3) would be this:

[2, 3, 4] -> [5]

[3, 4, 5] -> [4]

[4, 5, 4] -> [6]

[5, 4, 6] -> [7]

[4, 6, 7] -> [6]

[6, 7, 6] -> [8]

[7, 6, 8] -> [9]

Phew 😓.

Luckily, there is a module in Keras that does exactly this: TimeseriesGenerator. Please look up the documentation for more info.

I’ve set look_back as 15, but you can play around with that value.

Neural Network

Now that our data is ready, we can move on to creating and training our network.

A simple architecture of LSTM units trained using Adam optimizer and Mean Squared Loss function for 25 epochs. Note that instead of using model.fit(), we use model.fit_generator() because we have created a data generator.

To know more about LSTM network, see this awesome blog post.

Prediction

Now that we have completed training, let us see if the network performed well. We can test the model on testing data and see if the prediction and the actual values overlap.

Rather than computing loss between predicted and actual values, we can plot it.