<jupyter_start><jupyter_text>Importing the libraries<jupyter_code>import pandas as pd 
import numpy as np
from scipy.stats import norm
import datetime
import pandas_datareader.data as web
from pandas import Series, DataFrame

start = datetime.datetime(2000,1, 1)
end = datetime.datetime(2020, 12, 23)

import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import LSTM,Dense,Dropout
import matplotlib.pyplot as plt
import tensorflow as tf<jupyter_output><empty_output><jupyter_text>Defining the parameters<jupyter_code># Lookback day is the number of days the model will refer to for predicting the value
look_back = 90
# forward day is the number of days for prediction
foward_days = 5
# Importing the datavalue
df= web.DataReader('^NSEI',"yahoo",start,end)
df
# Droping the Open,Volume and Adj close values and keeping just the high, low and close values
df = df.drop('Open',axis=1)
df = df.drop('Volume',axis=1)
df = df.drop('Adj Close',axis=1)
df
# changing the type of file and few item type modifications
df.to_csv("Data.csv")
df = pd.read_csv('Data.csv')
df['Date'] = pd.to_datetime(df['Date'])
df.set_index('Date', inplace=True)

df.head()
# visualizing the Chart
plt.figure(figsize = (15,10))
plt.plot(df)
plt.show()
# Reshaping the viewing the value
array = df.values.reshape(df.shape[0],3)
array[:5]
# Scaling the price values.
from sklearn.preprocessing import MinMaxScaler
scl = MinMaxScaler()
array = scl.fit_transform(array)
array[:5]
# number of period is the number of segments in which the model would learn
num_periods = 20

division = len(array) - num_periods*foward_days
array_test = array[division-look_back:]
array_train = array[:division]
# Pre-processing the data, creating an array which is suitable for the input
def processData(data, look_back, foward_days,jump=1):
    X,Y = [],[]
    for i in range(0,len(data) -look_back -foward_days +1, jump):
        X.append(data[i:(i+look_back)])
        Y.append(data[(i+look_back):(i+look_back+foward_days)])
    return np.array(X),np.array(Y)

# Spliting the training and testing values.
X_test,y_test = processData(array_test,look_back,foward_days,foward_days)
y_test = np.array([list(a.ravel()) for a in y_test])

X,y = processData(array_train,look_back,foward_days)
y = np.array([list(a.ravel()) for a in y])

from sklearn.model_selection import train_test_split
X_train, X_validate, y_train, y_validate = train_test_split(X, y, test_size=0.20, random_state=42)

#Displaying the shapes of inputs
print(X_train.shape)
print(X_validate.shape)
print(X_test.shape)
print(y_train.shape)
print(y_validate.shape)
print(y_test.shape)

# Defining the model 
NUM_NEURONS_FirstLayer = 256
NUM_NEURONS_SecondLayer = 128
NUM_NEURONS_ThirdLayer = 64
NUM_NEURONS_FourthLayer = 32
EPOCHS = 150


model = Sequential()
model.add(LSTM(NUM_NEURONS_FirstLayer,input_shape=(look_back,3), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(NUM_NEURONS_SecondLayer,return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(NUM_NEURONS_ThirdLayer,return_sequences=True))
model.add(LSTM(NUM_NEURONS_FourthLayer))
model.add(Dense(64))
model.add(Dense(foward_days*3))
model.compile(loss='mean_squared_error', optimizer='adam')

model.summary()
# Creating a callback of model checkpoint to save all the values
model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
    filepath='./',
    save_weights_only=True,
    monitor='val_loss',
    mode='max',
    save_best_only=True)
# Reducing the LR incase the loss stabalizes for some epochs
reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.2,
                              patience=5, min_lr=0.001)
# fitting the model
history = model.fit(X_train,y_train,epochs=EPOCHS,validation_data=(X_validate,y_validate),shuffle=True,batch_size=8, verbose=1,callbacks = [model_checkpoint_callback,reduce_lr])
#Saving the model
file_name = 'Nifty'
model.save(file_name)
print("Saved model `{}` to disk".format(file_name))

# Displaying the loss values.
plt.figure(figsize = (15,10))
plt.plot(history.history['loss'], label='loss')
plt.plot(history.history['val_loss'], label='val_loss')
plt.legend(loc='best')
plt.show()<jupyter_output><empty_output><jupyter_text>Creating samples for predicting the future value.<jupyter_code>Xt = model.predict(X_test)
division = len(array) - num_periods*foward_days

leftover = division%foward_days+1

array_test = array[division-look_back:]
array_train = array[leftover:division]

Xtrain,ytrain = processData(array_train,look_back,foward_days,foward_days)
Xtest,ytest = processData(array_test,look_back,foward_days,foward_days)
Xtrain = model.predict(Xtrain)
Xtrain = Xtrain.ravel()

Xtest = model.predict(Xtest)
Xtest = Xtest.ravel()
y = np.concatenate((ytrain, ytest), axis=0)
array[-5:]
scl.inverse_transform(array[-5:].reshape(-1,3))
y_test.shape
Z = []
Z.append(array[-40:])
Z  = np.array(Z)
Z.shape
out = model.predict(Z)
out = out.ravel()
out
scl.inverse_transform(Z.reshape(-1,3))
scl.inverse_transform(out.reshape(-1,3))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12327688066167783, 'fraction_numerical': 0.01654194564789287, 'mean_word_length': 6.436310395314788, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '422871', 'license': 'MIT', 'n_tokens_mistral': 1936, 'n_tokens_neox': 1830, 'n_words': 396, 'path': 'Stock_Price_Prediction.ipynb', 'repo_name': 'ShreyPandit/StockPrice-Prediction', 'size': '5078'}