Using LSTM’s to Predict Future Stock Prices

SFHS ML Club
Visionary Hub
Published in
4 min readApr 17, 2021

Model Developed in Python using SciKitLearn and Keras

Written by: Ruhi Yusuf, Siddhant Hullur
Edited by: Fazal Mittu, Pranav Gopal
Project Team: Fazal Mittu, Varun Nair, Michael Puell, Swathi Badrinarayanan, Koena Jaware, Sandeep Bajamahal, Ruhi Yusuf, Siddhant Hullur

Introduction

This project utilizes common machine learning python libraries, as well as public data to produce a trained model able to accurately predict stock prices, in turn, providing assistance when trading and understanding the market. This project specifically used data containing Apple’s stock prices.

Purpose/Real-World Application

The practical application of this project is to assist stock traders in their buying decisions by using historical data of stock pricing to identify trends and predict future prices. This product was built to provide assurance when making financial decisions.

Implementation/Main ML Concept

Diagram of LSTM

Long Term Short Term Memory networks are an extremely powerful time-series model. They are a type of Recurrent Neural Network (RNN), that are able to learn long-term dependencies. LTSMs have been used for applying RNN’s in a variety of problems including image captioning, speech recognition, language modeling, and translation.

LSTMs solve the problem of short-term memory, or vanishing and exploding gradients and gradient shrinking as a model backpropagates through time. The LSTM has gates that are able to regulate what to keep or forget/throw away. By doing so, it learns to use relevant information to make predictions. The LSTM is able to store information from the past which helps especially predict stock price fluctuations based on past prices. The market changes constantly, so making timely accurate decisions and predictions is extremely crucial for accurate results.

LSTM processes data sequentially. The LSTM passes data as it propagates forward and uses gates to decide whether to keep or forget information. Each LSTM cell contains a forget state, input gate, output gate, and cell state, along with two activation functions, Sigmoid and Tanh. The cell state contains the long term memory, with input gates updating the cell state. The forget gate decides what information needs to be thrown away. The value 0 indicates nonessential, while 1 indicates important. The output (hidden state) is used as the prediction.

Sigmoid activation ensures that values stay between 0 and 1. Any value multiplied by 0 is forgotten while any value multiplied by 1 is kept. Tanh is used to determine the candidate values to be added to the cell state. Tanh activation ensures that the values stay between a positive (1) and negative (-1) number, allowing for increase or decrease in the value for cell state.

Code Explanation (Cell by Cell Analysis)

Libraries/Frameworks Used: SciKitLearn, Keras, Matplotlib, Numpy, Pandas

Cells 1–2: Importing necessary libraries

For this project, we used a variety of libraries. SciKitlearn and Keras were used to build, train, and test the model. Matplotlib, numpy, and pandas were used to preprocess and view the data.

Cells 3–8: Pulling dataset and extracting necessary data

The first part of this code block uses the requests library to pull a csv file containing Apple’s stock prices every day for the past year. This csv file is then made into a dataset using the pandas library. The rest of the code is used to extract the data that is needed to train/test the model and store them in smaller arrays. These datasets are then split into train/test sets.

Cells 9–11: Reshaping data to prepare it for training

These cells first use feature scaling on the dataset which made each value between 0 and 1. Then, it reshaped the data and prepared it to be passed into the model.

Cells 12–13: Training the model to fit data

These cells were where the model was built and run on the data. The model consists of 2 dense, hidden layers.

Cells 15–17: Creating test dataset to plot the final result

These cells reshape the test set to make a prediction. After the test set is reshaped, matplotlib is used to graph the prediction alongside the real value.

Here we have our final prediction! The green line is our prediction while the orange line represents the true values.

Conclusion

We have learned the structure of LSTM’s and how they can be applied in real life. Further, we implemented the model using the SciKitLearn library and Keras framework.

To check out the code for this project, click on the link below!

Sources

https://www.researchgate.net/publication/342225624_Stock_Market_Prediction_Using_Machine_LearningMLAlgorithms

https://www.researchgate.net/publication/259240183_A_Machine_Learning_Model_for_Stock_Market_Prediction

--

--

SFHS ML Club
Visionary Hub

Welcome to the Saint Francis High School Machine Learning Club Medium page. This profile is used to document the projects we do together as a team.