# LSTM STOCK MARKET PREDICTION USING RNN #### INTRODUCTION:

Stock market is a place where people buy and sell stocks where it is virtual and complex in nature. Predicting stock prices is not an easy task . So people have started using fundamental and technical analysis to predict the stock price. However, machine learning and deep learning are used in financial industries i.e, stock market. There are several machine learning models used for prediction of stock prices proven their advantage over traditional machine learning methods in terms of accuracy and speed of prediction.

In this article, lets discuss about Recurrent Neural Network using Long short term memory model to predict the future stock prices .

#### LSTM Recurrent Neural Network:

An LSTM will take the data through what it calls “cells” as you can see in the diagram above depicted in the middle rectangle. It is a recurrent network because of the feedback connections in its architecture. It has an advantage over traditional neural networks due to its capability to process the entire sequence of data. Its architecture comprises the cell, input gate, output gate and forget gate.

Let’s begin!

Firstly lets import library functions

``````import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
``````

``````df_infy = dr.DataReader('infy.ns', 'yahoo', '2017-3-25', '2021-04-17')
df_infy.shape

data_training = df_infy [0: 800]
data_testing = df_infy [800:]
print(data_training.shape, data_testing.shape)
training_data = data_training.drop(['Adj Close'], axis = 1)

sc = MinMaxScaler()
training_data = sc.fit_transform(training_data)
training_data.shape
training_data

train_data_x = []
train_data_y = []
for counter in range(50, training_data.shape): train_data_x.append(training_data[counter-50:counter])
train_data_y.append(training_data[counter, 0])
train_data_x, train_data_y = np.array(train_data_x), np.array(train_data_y)

from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, LSTM, Dropout
model = Sequential()
model.add(LSTM(units = 50, activation = 'relu', return_sequences = True, input_shape = (train_data_x.shape, 5)))
model.add(LSTM(units = 60, activation = 'relu', return_sequences = True))
model.add(LSTM(units = 80, activation = 'relu', return_sequences = True))
model.add(LSTM(units = 120, activation = 'relu'))

model.fit(train_data_x, train_data_y, epochs=1, batch_size=32)
offset_data = data_training.tail(50)

new_test_data = offset_data.append(data_testing, ignore_index = True)
new_test_data = new_test_data.drop(['Adj Close'], axis = 1)
new_test_data.shape

new_scaled_data = sc.transform(new_test_data)
new_scaled_data.shape

test_data_x = []
test_data_y = []

for i in range(60, new_scaled_data.shape):
test_data_x.append(new_scaled_data[i-60:i])
test_data_y.append(new_scaled_data[i, 0])
test_data_x, test_data_y = np.array(test_data_x), np.array(test_data_y)
test_data_x.shape, test_data_y.shape

predicted_y = model.predict(test_data_x)

sc.scale_
scale = 1/7.63358779e-03
scale
predicted_y = predicted_y*scale
test_data_y = test_data_y*scale

``````

Now lets visualize the results:

``````plt.figure(figsize=(14,5))
plt.plot(test_data_y, color = 'black', label = 'Ashok Leyland Stock Price')
plt.plot(predicted_y, color = 'maroon', label = 'Predicted infy Stock Price')
plt.title('NSE Stock Price Prediction')
plt.xlabel('Time')
plt.ylabel(‘infy Stock Price’)
plt.legend()
plt.show()

``````

### CONCLUSION :

Here we can conclude that this model can be used to find the nearest assumption of the stock market prediction.