Univariate Time Series Forecasting using RNN(LSTM)

Soubhik Khankary
4 min readJan 25, 2022

--

In this Post we will be discussing about Univariate Time Series Forecasting using LSTM method which seems to be more accurate in doing the forecast for few of the Time Series Problems. My post will also help you all in understanding the basics of the problem and model implementation.

Let’s start with the coding stuffs.

Importing the basic modules of python to do some analysis on training data set.

importing basic modules

There are two datasets training dataset and testing dataset . We have to fit our LSTM model on training dataset and then try to predict stock values on testing dataset. Let us view the testing dataset sample which has got 6 columns and we will play around with only one column “Open”. Sample data view below:

Let’s try to analyze the trend of the data on training dataset using python.

Trend : Price is going upward
One column-Univariate Time Series

As the distribution of the column is not the type of Gaussian Distribution type . Hence I have applied the MinMaxScaler . Though I feel StandardScaler will also work perfectly fine. Please try that out and let me know if it works better than this.

Feature Ranges from 0–1

Here Comes the most important section about time series forecasting. We have to look back the previous values of the stock prices and it could hop in different ways that could be 3,6,9,12,30,60,90 anything. Let me explain it with simple List example. Let us consider a list below which is a time series:

a1=[10,20,30,40,50,60]

In case above if we try to see the previous 3 values and try to predict the next value then our dataset looks like below.

Seems quite easy right now

Applying the same programmatically below:

Hopping is 60
total count reduced by 60(1258–60). Considering the last 3 months data as every month has 20 working days.

Here we go with the RNN-LSTM model below:

Importing the necessary module from Keras.

I have created two LSTM layers with 50 units each and connected the same with the Dense layer. I have also added Dropout layer so that the model doesn’t overfit. I have dropped 20% of my units to make sure my model doesn’t overfit. I am also using ADAM optimizer where as I could have gone with RMSPROP or any other optimizer. Please try out with other optimizers and let me know about the results.

Let us fit the model on the training dataset and get the model trained and acquainted with the dataset. You could see after 75 epochs the MSE error is not getting reduced further . Hope our weights are adjusted correctly and model trained perfectly.

Now time to prepare test data set and try predicting values for the same.

Please make sure to apply scaling transformations and reshaping the test data set as well . It is because we did the same on training data set.

Hope you understood why we pulled in last 60 records from the training dataset as we are trying to collect 60 different values from the previous days and trying to predict next value.

Concatenating both the values into a single dataframe.

Plotting the predicted and Actual value on the graph. When I looked into it I found the results were pretty good. It was able to identify the dips and growth in the actual graph and was able to follow the trend.

Trend is captured in prediction

I feel by now you must be clear about how to do univariate time series forecasting using LSTM . In case of any doubts please feel free to comment below. I will soon be coming up with next post where we would try doing multivariate forecasting using LSTM model. Happy deep learning.

--

--

Soubhik Khankary

Data Engineer by job , Teaching computers by stats and love to learn never endless math.