# Short-term Stock Market Price Prediction using Deep Learning Models

This blog is about short-term stock market price trend prediction using a comprehensive deep learning LSTM model. Results show that the model achieves overall high accuracy for stock market trend prediction. The following end-to-end sequence provides the detailed Python/Jupyter workflow from data processing to prediction, including the data exploration:
1. Data Preparation Phase
#import libraries

import pandas as pd
import numpy as np
# To remove the scientific notation from numpy arrays
np.set_printoptions(suppress=True)

# install the nsepy library to get stock prices
!pip install nsepy

############################################
# Getting Stock data using nsepy library
from nsepy import get_history
from datetime import datetime

startDate=datetime(2021, 1,1)
endDate=datetime(2022, 10, 3)

# Fetching the data
StockData=get_history(symbol=’INFY’, start=startDate, end=endDate)
print(StockData.shape)

# Creating a column as date

# Plotting the stock prices
%matplotlib inline

2. Exploratory Data Analysis (EDA)

# Extracting the closing prices of each day
FullData=StockData[[‘Close’]].values
print(FullData[0:5])

# Feature Scaling for fast training of neural networks
from sklearn.preprocessing import StandardScaler, MinMaxScaler

# Choosing between Standardization or normalization
#sc = StandardScaler()
sc=MinMaxScaler()

DataScaler = sc.fit(FullData)
X=DataScaler.transform(FullData)
#X=FullData

print(‘### After Normalization ###’)
X[0:5]
`[[1260.45] [1288.25] [1293.8 ] [1282.1 ] [1262.15]]### After Normalization ###`
Out[3]:
`array([[0.03055179],       [0.07024056],       [0.07816404],       [0.06146049],       [0.0329788 ]])`

`# split into samplesX_samples = list()y_samples = list() NumerOfRows = len(X)TimeSteps=10  # next day's Price Prediction is based on last how many past day's prices # Iterate thru the values to create combinationsfor i in range(TimeSteps , NumerOfRows , 1):    x_sample = X[i-TimeSteps:i]    y_sample = X[i]    X_samples.append(x_sample)    y_samples.append(y_sample) ################################################# Reshape the Input as a 3D (number of samples, Time Steps, Features)X_data=np.array(X_samples)X_data=X_data.reshape(X_data.shape[0],X_data.shape[1], 1)print('\n#### Input Data shape ####')print(X_data.shape) # We do not reshape y as a 3D data  as it is supposed to be a single column onlyy_data=np.array(y_samples)y_data=y_data.reshape(y_data.shape[0], 1)print('\n#### Output Data shape ####')print(y_data.shape)`

```#### Input Data shape ####(308, 10, 1)#### Output Data shape ####(308, 1)

TestingRecords=5 # Splitting the data into train and testX_train=X_data[:-TestingRecords]X_test=X_data[-TestingRecords:]y_train=y_data[:-TestingRecords]y_test=y_data[-TestingRecords:] ############################################ # Printing the shape of training and testingprint('\n#### Training Data shape ####')print(X_train.shape)print(y_train.shape)print('\n#### Testing Data shape ####')print(X_test.shape)print(y_test.shape)

#### Training Data shape ####(303, 10, 1)(303, 1)#### Testing Data shape ####(5, 10, 1)(5, 1)

# Visualizing the input and output being sent to the LSTM modelfor inp, out in zip(X_train[0:2], y_train[0:2]):    print(inp,'--', out)

[[0.03055179] [0.07024056] [0.07816404] [0.06146049] [0.0329788 ] [0.1042901 ] [0.1958027 ] [0.18944964] [0.21143551] [0.18766507]] -- [0.15118852][[0.07024056] [0.07816404] [0.06146049] [0.0329788 ] [0.1042901 ] [0.1958027 ] [0.18944964] [0.21143551] [0.18766507] [0.15118852]] -- [0.10421872]

TimeSteps=X_train.shape[1]TotalFeatures=X_train.shape[2]print("Number of TimeSteps:", TimeSteps)print("Number of Features:", TotalFeatures)

Number of TimeSteps: 10Number of Features: 1

```

3. Fitting the RNN to the Training set

from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM

# Initialising the RNN
regressor = Sequential()

# Adding the First input hidden layer and the LSTM layer
# return_sequences = True, means the output of every time step to be shared with hidden next layer
regressor.add(LSTM(units = 10, activation = ‘relu’, input_shape = (TimeSteps, TotalFeatures), return_sequences=True))

# Adding the Second Second hidden layer and the LSTM layer
regressor.add(LSTM(units = 5, activation = ‘relu’, input_shape = (TimeSteps, TotalFeatures), return_sequences=True))

# Adding the Second Third hidden layer and the LSTM layer
regressor.add(LSTM(units = 5, activation = ‘relu’, return_sequences=False ))

from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM

# Initialising the RNN
regressor = Sequential()

# Adding the First input hidden layer and the LSTM layer
# return_sequences = True, means the output of every time step to be shared with hidden next layer
regressor.add(LSTM(units = 10, activation = ‘relu’, input_shape = (TimeSteps, TotalFeatures), return_sequences=True))

# Adding the Second Second hidden layer and the LSTM layer
regressor.add(LSTM(units = 5, activation = ‘relu’, input_shape = (TimeSteps, TotalFeatures), return_sequences=True))

# Adding the Second Third hidden layer and the LSTM layer
regressor.add(LSTM(units = 5, activation = ‘relu’, return_sequences=False ))

# Compiling the RNN
regressor.compile(optimizer = ‘adam’, loss = ‘mean_squared_error’)

##################################################
import time
# Measuring the time taken by the model to train
StartTime=time.time()

# Fitting the RNN to the Training set
regressor.fit(X_train, y_train, batch_size = 5, epochs = 100)

EndTime=time.time()
print(“## Total Time Taken: “, round((EndTime-StartTime)/60), ‘Minutes ##’)

`Epoch 1/10061/61 [==============================] - 2s 5ms/step - loss: 0.2446Epoch 2/10061/61 [==============================] - 0s 5ms/step - loss: 0.0213Epoch 3/10061/61 [==============================] - 0s 5ms/step - loss: 0.0122Epoch 4/10061/61 [==============================] - 0s 5ms/step - loss: 0.0095Epoch 5/10061/61 [==============================] - 0s 5ms/step - loss: 0.0075Epoch 6/10061/61 [==============================] - 0s 5ms/step - loss: 0.0066Epoch 7/10061/61 [==============================] - 0s 5ms/step - loss: 0.0059Epoch 8/10061/61 [==============================] - 0s 5ms/step - loss: 0.0058Epoch 9/10061/61 [==============================] - 0s 5ms/step - loss: 0.0052Epoch 10/10061/61 [==============================] - 0s 5ms/step - loss: 0.0054Epoch 11/10061/61 [==============================] - 0s 5ms/step - loss: 0.0050Epoch 12/10061/61 [==============================] - 0s 5ms/step - loss: 0.0051Epoch 13/10061/61 [==============================] - 0s 5ms/step - loss: 0.0050Epoch 14/10061/61 [==============================] - 0s 5ms/step - loss: 0.0048Epoch 15/10061/61 [==============================] - 0s 5ms/step - loss: 0.0051Epoch 16/10061/61 [==============================] - 0s 5ms/step - loss: 0.0049Epoch 17/10061/61 [==============================] - 0s 5ms/step - loss: 0.0048Epoch 18/10061/61 [==============================] - 0s 5ms/step - loss: 0.0047Epoch 19/10061/61 [==============================] - 0s 5ms/step - loss: 0.0048Epoch 20/10061/61 [==============================] - 0s 5ms/step - loss: 0.0046Epoch 21/10061/61 [==============================] - 0s 5ms/step - loss: 0.0046Epoch 22/10061/61 [==============================] - 0s 5ms/step - loss: 0.0045Epoch 23/10061/61 [==============================] - 0s 5ms/step - loss: 0.0052Epoch 24/10061/61 [==============================] - 0s 5ms/step - loss: 0.0047Epoch 25/10061/61 [==============================] - 0s 5ms/step - loss: 0.0046Epoch 26/10061/61 [==============================] - 0s 5ms/step - loss: 0.0049Epoch 27/10061/61 [==============================] - 0s 5ms/step - loss: 0.0044Epoch 28/10061/61 [==============================] - 0s 5ms/step - loss: 0.0046Epoch 29/10061/61 [==============================] - 0s 5ms/step - loss: 0.0044Epoch 30/10061/61 [==============================] - 0s 5ms/step - loss: 0.0042Epoch 31/10061/61 [==============================] - 0s 5ms/step - loss: 0.0045Epoch 32/10061/61 [==============================] - 0s 5ms/step - loss: 0.0046Epoch 33/10061/61 [==============================] - 0s 5ms/step - loss: 0.0043Epoch 34/10061/61 [==============================] - 0s 5ms/step - loss: 0.0047Epoch 35/10061/61 [==============================] - 0s 5ms/step - loss: 0.0043Epoch 36/10061/61 [==============================] - 0s 5ms/step - loss: 0.0043Epoch 37/10061/61 [==============================] - 0s 4ms/step - loss: 0.0041Epoch 38/10061/61 [==============================] - 0s 5ms/step - loss: 0.0042Epoch 39/10061/61 [==============================] - 0s 5ms/step - loss: 0.0043Epoch 40/10061/61 [==============================] - 0s 5ms/step - loss: 0.0042Epoch 41/10061/61 [==============================] - 0s 5ms/step - loss: 0.0041Epoch 42/10061/61 [==============================] - 0s 5ms/step - loss: 0.0040Epoch 43/10061/61 [==============================] - 0s 5ms/step - loss: 0.0043Epoch 44/10061/61 [==============================] - 0s 5ms/step - loss: 0.0041Epoch 45/10061/61 [==============================] - 0s 5ms/step - loss: 0.0038Epoch 46/10061/61 [==============================] - 0s 5ms/step - loss: 0.0039Epoch 47/10061/61 [==============================] - 0s 5ms/step - loss: 0.0041Epoch 48/10061/61 [==============================] - 0s 5ms/step - loss: 0.0039Epoch 49/10061/61 [==============================] - 0s 5ms/step - loss: 0.0036Epoch 50/10061/61 [==============================] - 0s 5ms/step - loss: 0.0045Epoch 51/10061/61 [==============================] - 0s 5ms/step - loss: 0.0037Epoch 52/10061/61 [==============================] - 0s 5ms/step - loss: 0.0035Epoch 53/10061/61 [==============================] - 0s 4ms/step - loss: 0.0038Epoch 54/10061/61 [==============================] - 0s 4ms/step - loss: 0.0037Epoch 55/10061/61 [==============================] - 0s 5ms/step - loss: 0.0035Epoch 56/10061/61 [==============================] - 0s 4ms/step - loss: 0.0036Epoch 57/10061/61 [==============================] - 0s 5ms/step - loss: 0.0037Epoch 58/10061/61 [==============================] - 0s 5ms/step - loss: 0.0035Epoch 59/10061/61 [==============================] - 0s 5ms/step - loss: 0.0038Epoch 60/10061/61 [==============================] - 0s 5ms/step - loss: 0.0032Epoch 61/10061/61 [==============================] - 0s 5ms/step - loss: 0.0033Epoch 62/10061/61 [==============================] - 0s 5ms/step - loss: 0.0033Epoch 63/10061/61 [==============================] - 0s 5ms/step - loss: 0.0032Epoch 64/10061/61 [==============================] - 0s 5ms/step - loss: 0.0033Epoch 65/10061/61 [==============================] - 0s 5ms/step - loss: 0.0031Epoch 66/10061/61 [==============================] - 0s 5ms/step - loss: 0.0032Epoch 67/10061/61 [==============================] - 0s 5ms/step - loss: 0.0031Epoch 68/10061/61 [==============================] - 0s 5ms/step - loss: 0.0030Epoch 69/10061/61 [==============================] - 0s 5ms/step - loss: 0.0030Epoch 70/10061/61 [==============================] - 0s 5ms/step - loss: 0.0029Epoch 71/10061/61 [==============================] - 0s 5ms/step - loss: 0.0030Epoch 72/10061/61 [==============================] - 0s 5ms/step - loss: 0.0030Epoch 73/10061/61 [==============================] - 0s 5ms/step - loss: 0.0029Epoch 74/10061/61 [==============================] - 0s 5ms/step - loss: 0.0028Epoch 75/10061/61 [==============================] - 0s 5ms/step - loss: 0.0027Epoch 76/10061/61 [==============================] - 0s 5ms/step - loss: 0.0027Epoch 77/10061/61 [==============================] - 0s 5ms/step - loss: 0.0030Epoch 78/10061/61 [==============================] - 0s 5ms/step - loss: 0.0027Epoch 79/10061/61 [==============================] - 0s 5ms/step - loss: 0.0028Epoch 80/10061/61 [==============================] - 0s 5ms/step - loss: 0.0029Epoch 81/10061/61 [==============================] - 0s 5ms/step - loss: 0.0027Epoch 82/10061/61 [==============================] - 0s 5ms/step - loss: 0.0025Epoch 83/10061/61 [==============================] - 0s 5ms/step - loss: 0.0027Epoch 84/10061/61 [==============================] - 0s 5ms/step - loss: 0.0024Epoch 85/10061/61 [==============================] - 0s 5ms/step - loss: 0.0024Epoch 86/10061/61 [==============================] - 0s 4ms/step - loss: 0.0026Epoch 87/10061/61 [==============================] - 0s 5ms/step - loss: 0.0025Epoch 88/10061/61 [==============================] - 0s 5ms/step - loss: 0.0027Epoch 89/10061/61 [==============================] - 0s 5ms/step - loss: 0.0024Epoch 90/10061/61 [==============================] - 0s 5ms/step - loss: 0.0025Epoch 91/10061/61 [==============================] - 0s 5ms/step - loss: 0.0022Epoch 92/10061/61 [==============================] - 0s 5ms/step - loss: 0.0023Epoch 93/10061/61 [==============================] - 0s 5ms/step - loss: 0.0023Epoch 94/10061/61 [==============================] - 0s 4ms/step - loss: 0.0023Epoch 95/10061/61 [==============================] - 0s 5ms/step - loss: 0.0024Epoch 96/10061/61 [==============================] - 0s 5ms/step - loss: 0.0024Epoch 97/10061/61 [==============================] - 0s 5ms/step - loss: 0.0024Epoch 98/10061/61 [==============================] - 0s 5ms/step - loss: 0.0025Epoch 99/10061/61 [==============================] - 0s 5ms/step - loss: 0.0021Epoch 100/10061/61 [==============================] - 0s 5ms/step - loss: 0.0026## Total Time Taken:  1 Minutes ##`

4. Make Predictions & QC Analysis

predicted_Price = regressor.predict(X_test)
predicted_Price = DataScaler.inverse_transform(predicted_Price)

# Getting the original price values for testing data
orig=y_test
orig=DataScaler.inverse_transform(y_test)

# Accuracy of the predictions
print(‘Accuracy:’, 100 – (100*(abs(orig-predicted_Price)/orig)).mean())

# Visualising the results
import matplotlib.pyplot as plt

plt.plot(predicted_Price, color = ‘blue’, label = ‘Predicted Volume’)
plt.plot(orig, color = ‘lightblue’, label = ‘Original Volume’)

plt.title(‘Stock Price Predictions’)
plt.ylabel(‘Stock Price’)

plt.legend()
fig=plt.gcf()
fig.set_figwidth(20)
fig.set_figheight(6)
plt.show()

`Accuracy: 99.12386255608178`

5. Output Data Visualization

TrainPredictions=DataScaler.inverse_transform(regressor.predict(X_train))
TestPredictions=DataScaler.inverse_transform(regressor.predict(X_test))

FullDataPredictions=np.append(TrainPredictions, TestPredictions)
FullDataOrig=FullData[TimeSteps:]

# plotting the full data
plt.plot(FullDataPredictions, color = ‘blue’, label = ‘Predicted Price’)
plt.plot(FullDataOrig , color = ‘red’, label = ‘Original Price’)

plt.title(‘Stock Price Predictions’)
plt.ylabel(‘Stock Price’)
plt.legend()
fig=plt.gcf()
fig.set_figwidth(20)
fig.set_figheight(8)
plt.show()

plt.scatter(FullDataPredictions,FullDataOrig)
plt.xlabel(‘Predicted Price’)
plt.ylabel(‘Original Price’)