Stock Market Price Prediction

Sunny Agrawal
5 min readNov 6, 2021

Hello guys, I hope everyone is good.

What is Stock Market?
The stock market refers to the collection of markets and exchanges where regular activities of buying, selling, and issuance of shares of publicly-held companies take place. Such financial activities are conducted through institutionalized formal exchanges or over-the-counter (OTC) marketplaces which operate under a defined set of regulations. There can be multiple stock trading venues in a country or a region which allow transactions in stocks and other forms of securities.

I am using Google Colab because it is a free cloud service and now it supports free GPU! You can improve your Python programming language coding skills. Develop deep learning applications using popular libraries such as Keras, TensorFlow, PyTorch, and OpenCV.

In this blog I will show you how to write a python program that predicts the price of stocks using a machine learning technique called Long Short-Term Memory (LSTM). This program is really simple and I doubt any major profit will be made from this program, but it’s slightly better than guessing! Remember the stock price can be affected by many different things.

Long short-term memory (LSTM) is an artificial recurrent neural network (RNN) architecture used in the field of deep learning. Unlike standard feed forward neural networks, LSTM has feedback connections. It can not only process single data points (such as images), but also entire sequences of data (such as speech or video). — Wikipedia

LSTMs are widely used for sequence prediction problems and have proven to be extremely effective. The reason they work so well is because LSTM is able to store past information that is important, and forget the information that is not. -Stackabuse.com

Common Architecture of LSTM:

Forget Gate
Input Gate
Output Gate

Project Details:

This program will show you the predicted prices of any company(here Apple Inc.) I will get the stock quote for the company ‘Apple Inc.’ using the companies stock ticker (AAPL) from January 1st, 2012 to October 1st, 2020.

I will show you the visualization of the data but only its closing price history.

Visualize the closing price history
Graph: Close price($) vs Years

Now, scale the data set to be values between 0 and 1 inclusive, I do this because it is generally good practice to scale your data before giving it to the neural network. Data preprocessing is an integral step in Machine Learning as the quality of data and the useful information that can be derived from it directly affects the ability of our model to learn; therefore, it is extremely important that we preprocess our data before feeding it into our model.

Now, we will train the data in two data sets named x-train and y-train. The x-train contains the data from 0th index to 59th index that is total data of past 60 days.

Reshape the data to be 3-dimensional in the form [number of samples, number of time steps, and number of features]. The LSTM model is expecting a 3-dimensional data set.

Build the LSTM model to have two LSTM layers with 50 neurons and two Dense layers, one with 25 neurons and the other with 1 neuron.

Compile the model using the mean squared error (MSE) loss function and the adam optimizer.

Train the model using the training data sets. Note, fit is another name for train. Batch size is the total number of training examples present in a single batch, and epoch is the number of iterations when an entire data set is passed forward and backward through the neural network.

Creating a test data set.

Then convert the independent test data set ‘x_test’ to a numpy array so it can be used for testing the LSTM model.

Get the root mean squared error (RMSE), which is a good measure of how accurate the model is. A value of 0 would indicate that the models predicted values match the actual values from the test data set perfectly.

The lower the value the better the model performed. But usually it is best to use other metrics as well to truly get an idea of how well the model performed.

Now, I will show you the code for plotting the graph of the data on which model is trained on, predicted values and valid values.

Graph: Trained data, valid data, predictions

Showing you the data of the valid and predicted values.

Summary

I have learned how we can create a program that can predict the stock market price using LSTM. Still I want you to consider a point that the price of a stock market is affected by many reasons and due to that we can not always determine its actual or consistent closing prices.

Thank you for reading this article. Hope you find out it useful.

Source Code:

https://github.com/SunnyAgrawal1208/StockMarketPricePredictionUsingLSTMModel

--

--