Published in · 7 min read · Oct 26, 2021
--
Long Short-Term Memory (LSTM) is one type of recurrent neural network which is used to learn order dependence in sequence prediction problems. Due to its capability of storing past information, LSTM is very useful in predicting stock prices. This is because the prediction of a future stock price is dependent on the previous prices.
In this article, we will go through the steps to build a LSTM model to predict the stock prices in Python.
Disclaimer: The writing of this article is only aimed at demonstrating the steps to build a LSTM model to predict stock prices in Python. It doesn’t serve any purpose of promoting any stock or giving any specific investment advice.
- yFinance — https://pypi.org/project/yfinance/
- Numpy — https://numpy.org/
- Matplotlib — https://matplotlib.org/
- Pandas — https://pandas.pydata.org/
- Scikit-Learn — https://scikit-learn.org/stable/
- Tensorflow — https://www.tensorflow.org/
The original full source codes presented in this article are available on my Github Repo. Feel free to download it (stock_price_lstm.ipynb) if you wish to use it to follow my article.
1. Acquisition of Stock Data
Firstly, we are going to use yFinance to obtain the stock data. yFinance is an open-source Python library that allows us to acquire stock data from Yahoo Finance without any cost.
In this case, we are going to acquire the stock prices of AAPL over the last 5 years.
Line 1–9: Import all the required libraries.
Line 11–12: Use the yFinance download method to acquire the stock data started from 1 Jan…