Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024

Stock Price Prediction Using LSTM

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Predicting stock prices is an uncertain task using machine learning. There are a lot of tools used for stock market prediction. The stock market is considered to be dynamic and complex. An accurate forecast of future prices may lead to a higher profit yield for investors through stock investments. Investors will pick stocks that may give a higher return as per the predictions.

Over the years, various machine learning algorithms have been used in stock market prediction. Still, with the increased amount of data and expectation of more accurate forecasts, deep learning models are being used nowadays, proving their advantage over traditional machine learning methods in terms of accuracy and speed of prediction.

Traditionally, most machine learning models use some observations as input, but there is no time dimension in the dataset. Time-series forecasting models can predict future values based on past observed values. We widely use Time-series forecasting for non-stationary data. Non-stationary data are the data whose statistical values, i.e., the mean and standard deviation, are not constant but instead, these metrics vary over time.

These non-stationary input data (used to input these models) are usually called time-series. Some examples of time series include the temperature values over time, stock price over time, price of house overtime, etc. So, the input is a signal defined by observations taken sequentially in time.

This article will discuss the LSTM Network, one of the popular deep learning techniques used in stock price prediction. In this article, we will fetch the historical data of stock automatically using python libraries and fit the LSTM model on this data to predict the future prices of the store.

LSTM

Long short-term memory is an artificial recurrent neural network (RNN) architecture used in deep learning. Unlike standard feedforward neural networks, Long short-term Memory has feedback connections. It can process single data points (e.g., images) and entire data sequences (such as speech or video inputs).

LSTM models can store information over some time. They have a memory capacity.

This characteristic is beneficial when we deal with Time-Series or Sequential Data. When using an LSTM model, we can decide what information will be stored and discarded. We do that using the "gates."

LSTM has three gates:

  • The input gate: This gate adds information to the cell state,
  • The forget gate: It removes the knowledge that the model no longer requires,
  • The output gate: This gate at LSTM selects the information shown as output.

However, some variants of the LSTM model, such as Gated Recurrent Units (GRUs), don't have an output gate. Long short-term memory networks are popularly used on time-series data for classification and making predictions. Its popularity in time-series applications is that there can be several lags of unknown duration between important events in a time series.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Implementation 

Importing Modules

We can download the dataset from the given link. The dataset contains 1259 entries with seven different columns. The close column is the target feature. The data is from 2015 to 2020. After downloading the dataset, import all the required modules.

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
from sklearn import model_selection
from sklearn.metrics import confusion_matrix
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
df=pd.read_csv("TSLA.csv")
df.head(10)

Output

Train-Test Split

training = df.iloc[:800, 1:2].values
testing = df.iloc[800:, 1:2].values

Scaling the Data

LSTM is very sensitive to the scale of the data, Here the scale of the Close value is in a kind of scale, we should always try to transform the value.

Here we will use min-max scalar to transform the values from 0 to 1.We should reshape so that we can use fit transform.

sc = MinMaxScaler(feature_range = (0, 1))
training_scaled = sc.fit_transform(training_set)

Data Preprocessing

x_train = []
y_train = []
for iter in range(60, 800):
    x_train.append(training_scaled[iter-60:iter, 0])
    y_train.append(training_scaled[iter, 0])
x_train, y_train=np.array(x_train), np.array(y_train)
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))

LSTM Model

Now, it's time to build the model. We will make the LSTM with 50 neurons and four hidden layers. Finally, we will use one neuron in the output layer to predict the normalized stock price. We will use Mean Square Error(MSE) loss function and the Adam stochastic gradient descent optimizer.

algo = Sequential()
algo.add(LSTM(units = 50, return_sequences = True, input_shape = (x_train.shape[1], 1)))
algo.add(Dropout(0.2))
algo.add(LSTM(units = 50, return_sequences = True))
algo.add(Dropout(0.2))
algo.add(LSTM(units = 50, return_sequences = True))
algo.add(Dropout(0.2))
algo.add(LSTM(units = 50))
algo.add(Dropout(0.2))
algo.add(Dense(units = 1))
algo.compile(optimizer = 'adam', loss = 'mean_squared_error')
algo.fit(x_train, y_train, epochs = 100, batch_size = 32)

Prediction

data_train = df.iloc[:800, 1:2]
data_test = df.iloc[800:, 1:2]
data_total = pd.concat((data_train, data_test), axis = 0)
toal_input = data_total[len(data_total) - len(data_test) - 60:].values
total_input = total_input.reshape(-1,1)
total_input = sc.transform(total_input)
x_test = []
for iter in range(60, 519):
    x_test.append(total_input[iter-60:iter, 0])
x_test = np.array(x_test)
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
print(x_test.shape)

predicted_price = algo.predict(x_test)
predicted_price = sc.inverse_transform(predicted_price)

Plotting

plt.plot(df.loc[800:, 'Date'],data_test.values, color = 'red', label = 'Real TESLA Stock Price')
plt.plot(df.loc[800:, 'Date'],predicted_price, color = 'blue', label = 'Predicted TESLA Stock Price')
plt.xticks(np.arange(0,459,50))
plt.title('TESLA Stock Price Prediction')
plt.xlabel('Time')
plt.ylabel('TESLA Stock Price')
plt.legend()
plt.show()

Our model performed very well. It can accurately follow most of the unexcepted jumps and drops.

Frequently Asked Questions

1. Is it possible to predict stock prices?

Ans. There are chances that we can predict or instead forecast some market trends to get a higher chance of success in the market, as this is essentially what market analysts do. Still, these forecasts are closer to educated guesses than 99% accurate, precise predictions.

2. What is the use of stock price prediction?

Ans. Stock market prediction is the act of trying to determine the future value of company stock or other financial instruments traded on an exchange. The successful prediction of a stock's future price could yield significant profit.

3. Which ML model is best for stock prediction?

Ans. Introduction to LSTMs for the time-series data. Now, let's move on to the LSTM model. LSTM, short for Long Short-term Memory, is a robust algorithm for time series. It can capture historical trend patterns and predict future values with high accuracy.

Key Takeaways

Let us brief the article.

Firstly, we briefly introduced the stock price prediction and why different cant performs well with time-series data. Moving on, we saw the idea behind the working of LSTM. Lastly, we implemented the coding part using the LSTM model. 

Stock prices are influenced by company news and other factors such as demonetization or company mergers and demergers. In addition, many intangible variables are difficult to predict in advance.

I hope you all like this article. 

Happy Learning Ninjas!

 

Topics covered
1.
Introduction
2.
LSTM
3.
Implementation 
3.1.
Importing Modules
3.2.
Train-Test Split
3.3.
Scaling the Data
3.4.
Data Preprocessing
3.5.
LSTM Model
3.6.
Prediction
3.7.
Plotting
4.
Frequently Asked Questions
5.
Key Takeaways