Stock Prices Prediction Using Long Short-Term Memory (LSTM) Model in Python (2024)

Stock Prices Prediction Using Long Short-Term Memory (LSTM) Model in Python (3)

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.

  1. yFinancehttps://pypi.org/project/yfinance/
  2. Numpy https://numpy.org/
  3. Matplotlibhttps://matplotlib.org/
  4. Pandashttps://pandas.pydata.org/
  5. Scikit-Learn https://scikit-learn.org/stable/
  6. 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…

Stock Prices Prediction Using Long Short-Term Memory (LSTM) Model in Python (2024)
Top Articles
Latest Posts
Article information

Author: Sen. Ignacio Ratke

Last Updated:

Views: 5542

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Sen. Ignacio Ratke

Birthday: 1999-05-27

Address: Apt. 171 8116 Bailey Via, Roberthaven, GA 58289

Phone: +2585395768220

Job: Lead Liaison

Hobby: Lockpicking, LARPing, Lego building, Lapidary, Macrame, Book restoration, Bodybuilding

Introduction: My name is Sen. Ignacio Ratke, I am a adventurous, zealous, outstanding, agreeable, precious, excited, gifted person who loves writing and wants to share my knowledge and understanding with you.