<jupyter_start><jupyter_text>Data Proprocessing<jupyter_code>def Dataset(Data, column, datecol,date):

  Train_Data = Data[column][Data[datecol] < date].to_numpy()
  Data_Train = []
  Data_Train_X = []
  Data_Train_Y = []
  for i in range(0, len(Train_Data), 5):
    try:
      Data_Train.append(Train_Data[i : i + 5])
    except:
      pass

  if len(Data_Train[-1]) < 5:
    Data_Train.pop(-1)
  
  Data_Train_X = Data_Train[0 : -1]
  Data_Train_X = np.array(Data_Train_X)
  Data_Train_X = Data_Train_X.reshape((-1, 5, 1))
  Data_Train_Y = Data_Train[1 : len(Data_Train)]
  Data_Train_Y = np.array(Data_Train_Y)
  Data_Train_Y = Data_Train_Y.reshape((-1, 5, 1))

  Test_Data = Data[column][Data[datecol] >= date].to_numpy()
  Data_Test = []
  Data_Test_X = []
  Data_Test_Y = []
  for i in range(0, len(Test_Data), 5):
    try:
      Data_Test.append(Test_Data[i : i + 5])
    except:
      pass

  if len(Data_Test[-1]) < 5:
    Data_Test.pop(-1)
  
  Data_Test_X = Data_Test[0 : -1]
  Data_Test_X = np.array(Data_Test_X)
  Data_Test_X = Data_Test_X.reshape((-1, 5, 1))
  Data_Test_Y = Data_Test[1 : len(Data_Test)]
  Data_Test_Y = np.array(Data_Test_Y)
  Data_Test_Y = Data_Test_Y.reshape((-1, 5, 1))

  return Data_Train_X, Data_Train_Y, Data_Test_X, Data_Test_Y<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>def Model():
  model = tf.keras.models.Sequential([
                                      tf.keras.layers.LSTM(200, input_shape = (5, 3), activation = tf.nn.leaky_relu, return_sequences = True),
                                      tf.keras.layers.LSTM(200, activation = tf.nn.leaky_relu),
                                      tf.keras.layers.Dense(200, activation = tf.nn.leaky_relu),
                                      tf.keras.layers.Dense(100, activation = tf.nn.leaky_relu),
                                      tf.keras.layers.Dense(50, activation = tf.nn.leaky_relu),
                                      tf.keras.layers.Dense(5, activation = tf.nn.leaky_relu)
                                      ])
  return model
model = Model()
tf.keras.utils.plot_model(model, show_shapes=True)
model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 lstm (LSTM)                 (None, 5, 200)            163200    
                                                                 
 lstm_1 (LSTM)               (None, 200)               320800    
                                                                 
 dense (Dense)               (None, 200)               40200     
                                                                 
 dense_1 (Dense)             (None, 100)               20100     
                                                                 
 dense_2 (Dense)             (None, 50)                5050      
                                                                 
 dense_3 (Dense)             (None, 5)                 255       
                                                        [...]<jupyter_text>Custom Learning Rate<jupyter_code>lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
    initial_learning_rate=1e-2,
    decay_steps=10000,
    decay_rate=0.9)
callback = tf.keras.callbacks.EarlyStopping(
    monitor="val_loss",
    min_delta=0,
    patience=10,
    verbose=0,
    mode="auto",
    baseline=None,
    restore_best_weights=True,
)<jupyter_output><empty_output><jupyter_text>TCS<jupyter_code>TCS.tail()
TCS.info()
# Change Dtype of date column
TCS["date"] = pd.to_datetime(TCS["date"])
TCS_SMA_sub['time'] = pd.to_datetime(TCS_SMA_sub['time'])
TCS_EMA_sub['time'] = pd.to_datetime(TCS_EMA_sub['time'])<jupyter_output><empty_output><jupyter_text>Split the Data into Training and Test set    Training Period: 2019-01-01 - 2021-08-01    Testing Period:  2021-08-01 - 2021-10-29<jupyter_code>TCS_date = '2021-08-01'
TCS_Train_X, TCS_Train_Y, TCS_Test_X, TCS_Test_Y = Dataset(TCS,'adj close', 'date', TCS_date)
TCS_Train_SMA_X, TCS_Train_SMA_Y, TCS_Test_SMA_X, TCS_Test_SMA_Y = Dataset(TCS_SMA_sub,'SMA', 'time', TCS_date)
TCS_Train_EMA_X, TCS_Train_EMA_Y, TCS_Test_EMA_X, TCS_Test_EMA_Y = Dataset(TCS_EMA_sub,'EMA', 'time', TCS_date)
TCS_Train_X.shape, TCS_Train_Y.shape, TCS_Test_X.shape, TCS_Test_Y.shape
TCS['adj close'].head(20)
TCS_Train_X[0], TCS_Train_Y[0]
TCS_Train_X[1], TCS_Train_Y[1]
TCS_Train_SMA_X.shape, TCS_Train_SMA_Y.shape, TCS_Test_SMA_X.shape, TCS_Test_SMA_X.shape
TCS_Train_EMA_X.shape, TCS_Train_EMA_Y.shape, TCS_Test_EMA_X.shape, TCS_Test_EMA_X.shape
TCS_Train_X[0], TCS_Train_SMA_X[0]
TCS_Final_Train_X = np.concatenate((TCS_Train_X, TCS_Train_SMA_X, TCS_Train_EMA_X), axis=2)
TCS_Final_Test_X = np.concatenate((TCS_Test_X, TCS_Test_SMA_X, TCS_Test_EMA_X), axis=2)
TCS_Final_Train_X.shape, TCS_Train_Y.shape, TCS_Final_Test_X.shape, TCS_Test_Y.shape<jupyter_output><empty_output><jupyter_text>Model Fitting<jupyter_code>TCS_Model = Model()
TCS_Model.compile(optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule), loss = 'mse', metrics = tf.keras.metrics.RootMeanSquaredError())
TCS_hist = TCS_Model.fit(TCS_Final_Train_X, TCS_Train_Y, epochs = 10000, validation_data = (TCS_Final_Test_X, TCS_Test_Y), callbacks=[callback])
history_dict = TCS_hist.history

loss = history_dict["loss"]
root_mean_squared_error = history_dict["root_mean_squared_error"]
val_loss = history_dict["val_loss"]
val_root_mean_squared_error = history_dict["val_root_mean_squared_error"]

epochs = range(1, len(loss) + 1)
fig, (ax1, ax2) = plt.subplots(1, 2)

fig.set_figheight(5)
fig.set_figwidth(15)

ax1.plot(epochs, loss, label = 'Training Loss')
ax1.plot(epochs, val_loss, label = 'Validation Loss')
ax1.set(xlabel = "Epochs", ylabel = "Loss")
ax1.legend()

ax2.plot(epochs, root_mean_squared_error, label = "Training Root Mean Squared Error")
ax2.plot(epochs, val_root_mean_squared_error, label = "Validation Root Mean Squared Error")
ax2.set(xlabel = "Epochs", ylabel = "Loss")
ax2.legend()

plt.show()<jupyter_output><empty_output><jupyter_text>Predicting the closing stock price of TCS<jupyter_code>TCS_prediction = TCS_Model.predict(TCS_Final_Test_X)
rmse = math.sqrt(mean_squared_error(TCS_Test_Y.reshape(-1, 5), TCS_prediction))
mape = np.mean(np.abs(TCS_prediction - TCS_Test_Y.reshape(-1, 5))/np.abs(TCS_Test_Y.reshape(-1, 5)))
print(f'RMSE: {rmse}')
print(f'MAPE: {mape}')<jupyter_output>RMSE: 169.08418343515388
MAPE: 0.018315252193243817<jupyter_text>HDFC<jupyter_code>HDFC.tail()
HDFC.info()
# Change Dtype of date column
HDFC["date"] = pd.to_datetime(HDFC["date"])<jupyter_output><empty_output><jupyter_text>Split the Data into Training and Test set    Training Period: 2019-01-01 - 2021-08-01    Testing Period:  2021-08-01 - 2021-10-29<jupyter_code>HDFC_date = '2021-08-01'
HDFC_Train_X, HDFC_Train_Y, HDFC_Test_X, HDFC_Test_Y = Dataset(HDFC,'adj close', 'date', HDFC_date)
HDFC_Train_SMA_X, HDFC_Train_SMA_Y, HDFC_Test_SMA_X, HDFC_Test_SMA_Y = Dataset(HDFC_SMA_sub,'SMA', 'time', HDFC_date)
HDFC_Train_EMA_X, HDFC_Train_EMA_Y, HDFC_Test_EMA_X, HDFC_Test_EMA_Y = Dataset(HDFC_EMA_sub,'EMA', 'time', HDFC_date)
HDFC_Train_X.shape, HDFC_Train_Y.shape, HDFC_Test_X.shape, HDFC_Test_Y.shape
HDFC['adj close'].head(20)
HDFC_Train_X[0], HDFC_Train_Y[0]
HDFC_Train_X[1], HDFC_Train_Y[1]
HDFC_Train_SMA_X.shape, HDFC_Train_SMA_Y.shape, HDFC_Test_SMA_X.shape, HDFC_Test_SMA_X.shape
HDFC_Train_EMA_X.shape, HDFC_Train_EMA_Y.shape, HDFC_Test_EMA_X.shape, HDFC_Test_EMA_X.shape
HDFC_Train_X[0], HDFC_Train_SMA_X[0]
HDFC_Final_Train_X = np.concatenate((HDFC_Train_X, HDFC_Train_SMA_X, HDFC_Train_EMA_X), axis=2)
HDFC_Final_Test_X = np.concatenate((HDFC_Test_X, HDFC_Test_SMA_X, HDFC_Test_EMA_X), axis=2)
HDFC_Final_Train_X.shape, HDFC_Train_Y.shape, HDFC_Final_Test_X.shape, HDFC_Test_Y.shape
HDFC_Train_X.shape, HDFC_Train_Y.shape, HDFC_Test_X.shape, HDFC_Test_Y.shape<jupyter_output><empty_output><jupyter_text>Model Fitting<jupyter_code>HDFC_Model = Model()
HDFC_Model.compile(optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule), loss = 'mse', metrics = tf.keras.metrics.RootMeanSquaredError())
HDFC_hist = HDFC_Model.fit(HDFC_Final_Train_X, HDFC_Train_Y, epochs = 10000, validation_data = (HDFC_Final_Test_X, HDFC_Test_Y), callbacks=[callback])
history_dict = HDFC_hist.history

loss = history_dict["loss"]
root_mean_squared_error = history_dict["root_mean_squared_error"]
val_loss = history_dict["val_loss"]
val_root_mean_squared_error = history_dict["val_root_mean_squared_error"]

epochs = range(1, len(loss) + 1)
fig, (ax1, ax2) = plt.subplots(1, 2)

fig.set_figheight(5)
fig.set_figwidth(15)

ax1.plot(epochs, loss, label = 'Training Loss')
ax1.plot(epochs, val_loss, label = 'Validation Loss')
ax1.set(xlabel = "Epochs", ylabel = "Loss")
ax1.legend()

ax2.plot(epochs, root_mean_squared_error, label = "Training Root Mean Squared Error")
ax2.plot(epochs, val_root_mean_squared_error, label = "Validation Root Mean Squared Error")
ax2.set(xlabel = "Epochs", ylabel = "Loss")
ax2.legend()

plt.show()<jupyter_output><empty_output><jupyter_text>Predicting the closing stock price of HDFC<jupyter_code>HDFC_prediction = HDFC_Model.predict(HDFC_Final_Test_X)
plt.figure(figsize=(20, 5))
plt.plot(HDFC['date'][HDFC['date'] < '2021-08-01'], HDFC['adj close'][HDFC['date'] < '2021-08-01'], label = 'Training')
plt.plot(HDFC['date'][HDFC['date'] >= '2021-08-01'], HDFC['adj close'][HDFC['date'] >= '2021-08-01'], label = 'Testing')
plt.plot(HDFC['date'][HDFC['date'] >= '2021-08-01'][:-7], HDFC_prediction.reshape(-1), label = 'Predictions')
plt.xlabel('Time')
plt.ylabel('Closing Price')
plt.legend(loc = 'best')
rmse = math.sqrt(mean_squared_error(HDFC_Test_Y.reshape(-1, 5), HDFC_prediction))
mape = np.mean(np.abs(HDFC_prediction - HDFC_Test_Y.reshape(-1, 5))/np.abs(HDFC_Test_Y.reshape(-1, 5)))
print(f'RMSE: {rmse}')
print(f'MAPE: {mape}')<jupyter_output>RMSE: 97.0194573739215
MAPE: 0.02780011097631451<jupyter_text>HUL<jupyter_code>HUL.tail()
HUL.info()
# Change Dtype of date column
HUL["date"] = pd.to_datetime(HUL["date"])<jupyter_output><empty_output><jupyter_text>Split the Data into Training and Test set    Training Period: 2019-01-01 - 2021-08-01    Testing Period:  2021-08-01 - 2021-10-29<jupyter_code>HUL_date = '2021-08-01'
HUL_Train_X, HUL_Train_Y, HUL_Test_X, HUL_Test_Y = Dataset(HUL,'adj close', 'date', HUL_date)
HUL_Train_SMA_X, HUL_Train_SMA_Y, HUL_Test_SMA_X, HUL_Test_SMA_Y = Dataset(HUL_SMA_sub,'SMA', 'time', HUL_date)
HUL_Train_EMA_X, HUL_Train_EMA_Y, HUL_Test_EMA_X, HUL_Test_EMA_Y = Dataset(HUL_EMA_sub,'EMA', 'time', HUL_date)
HUL_Train_X.shape, HUL_Train_Y.shape, HUL_Test_X.shape, HUL_Test_Y.shape
HUL['adj close'].head(20)
HUL_Train_X[0], HUL_Train_Y[0]
HUL_Train_X[1], HUL_Train_Y[1]
HUL_Train_SMA_X.shape, HUL_Train_SMA_Y.shape, HUL_Test_SMA_X.shape, HUL_Test_SMA_X.shape
HUL_Train_EMA_X.shape, HUL_Train_EMA_Y.shape, HUL_Test_EMA_X.shape, HUL_Test_EMA_X.shape
HUL_Train_X[0], HUL_Train_SMA_X[0]
HUL_Final_Train_X = np.concatenate((HUL_Train_X, HUL_Train_SMA_X, HUL_Train_EMA_X), axis=2)
HUL_Final_Test_X = np.concatenate((HUL_Test_X, HUL_Test_SMA_X, HUL_Test_EMA_X), axis=2)
HUL_Final_Train_X.shape, HUL_Train_Y.shape, HUL_Final_Test_X.shape, HUL_Test_Y.shape<jupyter_output><empty_output><jupyter_text>Model Fitting<jupyter_code>HUL_Model = Model()
HUL_Model.compile(optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule), loss = 'mse', metrics = tf.keras.metrics.RootMeanSquaredError())
HUL_hist = HUL_Model.fit(HUL_Final_Train_X, HUL_Train_Y, epochs = 10000, validation_data = (HUL_Final_Test_X, HUL_Test_Y), callbacks=[callback])
history_dict = HUL_hist.history

loss = history_dict["loss"]
root_mean_squared_error = history_dict["root_mean_squared_error"]
val_loss = history_dict["val_loss"]
val_root_mean_squared_error = history_dict["val_root_mean_squared_error"]

epochs = range(1, len(loss) + 1)
fig, (ax1, ax2) = plt.subplots(1, 2)

fig.set_figheight(5)
fig.set_figwidth(15)

ax1.plot(epochs, loss, label = 'Training Loss')
ax1.plot(epochs, val_loss, label = 'Validation Loss')
ax1.set(xlabel = "Epochs", ylabel = "Loss")
ax1.legend()

ax2.plot(epochs, root_mean_squared_error, label = "Training Root Mean Squared Error")
ax2.plot(epochs, val_root_mean_squared_error, label = "Validation Root Mean Squared Error")
ax2.set(xlabel = "Epochs", ylabel = "Loss")
ax2.legend()

plt.show()<jupyter_output><empty_output><jupyter_text>Predicting the closing stock price of HUL<jupyter_code>HUL_prediction = HUL_Model.predict(HUL_Final_Test_X)
plt.figure(figsize=(20, 5))
plt.plot(HUL['date'][HUL['date'] < '2021-08-01'], HUL['adj close'][HUL['date'] < '2021-08-01'], label = 'Training')
plt.plot(HUL['date'][HUL['date'] >= '2021-08-01'], HUL['adj close'][HUL['date'] >= '2021-08-01'], label = 'Testing')
plt.plot(HUL['date'][HUL['date'] >= '2021-08-01'][:-7], HUL_prediction.reshape(-1), label = 'Predictions')
plt.xlabel('Time')
plt.ylabel('Closing Price')
plt.legend(loc = 'best')
rmse = math.sqrt(mean_squared_error(HUL_Test_Y.reshape(-1, 5), HUL_prediction))
mape = np.mean(np.abs(HUL_prediction - HUL_Test_Y.reshape(-1, 5))/np.abs(HUL_Test_Y.reshape(-1, 5)))
print(f'RMSE: {rmse}')
print(f'MAPE: {mape}')<jupyter_output>RMSE: 137.8977195169357
MAPE: 0.04284848878931893<jupyter_text>MARUTI<jupyter_code>MARUTI.tail()
MARUTI.info()
# Change Dtype of date column
MARUTI["date"] = pd.to_datetime(MARUTI["date"])<jupyter_output><empty_output><jupyter_text>Split the Data into Training and Test set    Training Period: 2019-01-01 - 2021-08-01    Testing Period:  2021-08-01 - 2021-10-29<jupyter_code>MARUTI_EMA_sub.head()
MARUTI_date = '2021-08-01'
MARUTI_Train_X, MARUTI_Train_Y, MARUTI_Test_X, MARUTI_Test_Y = Dataset(MARUTI,'adj close', 'date', MARUTI_date)
MARUTI_Train_SMA_X, MARUTI_Train_SMA_Y, MARUTI_Test_SMA_X, MARUTI_Test_SMA_Y = Dataset(MARUTI_SMA_sub,'SMA', 'time', MARUTI_date)
MARUTI_Train_EMA_X, MARUTI_Train_EMA_Y, MARUTI_Test_EMA_X, MARUTI_Test_EMA_Y = Dataset(MARUTI_EMA_sub,'EMA', 'time', MARUTI_date)
MARUTI_Train_X.shape, MARUTI_Train_Y.shape, MARUTI_Test_X.shape, MARUTI_Test_Y.shape
MARUTI['adj close'].head(20)
MARUTI_Train_X[0], MARUTI_Train_Y[0]
MARUTI_Train_X[1], MARUTI_Train_Y[1]
MARUTI_Train_SMA_X.shape, MARUTI_Train_SMA_Y.shape, MARUTI_Test_SMA_X.shape, MARUTI_Test_SMA_X.shape
MARUTI_Train_EMA_X.shape, MARUTI_Train_EMA_Y.shape, MARUTI_Test_EMA_X.shape, MARUTI_Test_EMA_X.shape
MARUTI_Train_X[0], MARUTI_Train_SMA_X[0]
MARUTI_Final_Train_X = np.concatenate((MARUTI_Train_X, MARUTI_Train_SMA_X, MARUTI_Train_EMA_X), axis=2)
MARUTI_Final_Test_X = np.concatenate((MARUTI_Test_X, MARUTI_Test_SMA_X, MARUTI_Test_EMA_X), axis=2)
MARUTI_Final_Train_X.shape, MARUTI_Train_Y.shape, MARUTI_Final_Test_X.shape, MARUTI_Test_Y.shape<jupyter_output><empty_output><jupyter_text>Model Fitting<jupyter_code>MARUTI_Model = Model()
MARUTI_Model.compile(optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule), loss = 'mse', metrics = tf.keras.metrics.RootMeanSquaredError())
MARUTI_hist = MARUTI_Model.fit(MARUTI_Final_Train_X, MARUTI_Train_Y, epochs = 10000, validation_data = (MARUTI_Final_Test_X, MARUTI_Test_Y), callbacks=[callback])
history_dict = MARUTI_hist.history

loss = history_dict["loss"]
root_mean_squared_error = history_dict["root_mean_squared_error"]
val_loss = history_dict["val_loss"]
val_root_mean_squared_error = history_dict["val_root_mean_squared_error"]

epochs = range(1, len(loss) + 1)
fig, (ax1, ax2) = plt.subplots(1, 2)

fig.set_figheight(5)
fig.set_figwidth(15)

ax1.plot(epochs, loss, label = 'Training Loss')
ax1.plot(epochs, val_loss, label = 'Validation Loss')
ax1.set(xlabel = "Epochs", ylabel = "Loss")
ax1.legend()

ax2.plot(epochs, root_mean_squared_error, label = "Training Root Mean Squared Error")
ax2.plot(epochs, val_root_mean_squared_error, label = "Validation Root Mean Squared Error")
ax2.set(xlabel = "Epochs", ylabel = "Loss")
ax2.legend()

plt.show()<jupyter_output><empty_output><jupyter_text>Predicting the closing stock price of MARUTI<jupyter_code>MARUTI_prediction = MARUTI_Model.predict(MARUTI_Final_Test_X)
plt.figure(figsize=(20, 5))
plt.plot(MARUTI['date'][MARUTI['date'] < '2021-08-01'], MARUTI['adj close'][MARUTI['date'] < '2021-08-01'], label = 'Training')
plt.plot(MARUTI['date'][MARUTI['date'] >= '2021-08-01'], MARUTI['adj close'][MARUTI['date'] >= '2021-08-01'], label = 'Testing')
plt.plot(MARUTI['date'][MARUTI['date'] >= '2021-08-01'][:-7], MARUTI_prediction.reshape(-1), label = 'Predictions')
plt.xlabel('Time')
plt.ylabel('Closing Price')
plt.legend(loc = 'best')
rmse = math.sqrt(mean_squared_error(MARUTI_Test_Y.reshape(-1, 5), MARUTI_prediction))
mape = np.mean(np.abs(MARUTI_prediction - MARUTI_Test_Y.reshape(-1, 5))/np.abs(MARUTI_Test_Y.reshape(-1, 5)))
print(f'RMSE: {rmse}')
print(f'MAPE: {mape}')<jupyter_output>RMSE: 241.0893902679114
MAPE: 0.026907316884690256
{'fraction_non_alphanumeric': 0.14012513280604416, 'fraction_numerical': 0.040963286506905915, 'mean_word_length': 5.2359219727640784, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 19, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '179500', 'license': 'MIT', 'n_tokens_mistral': 7719, 'n_tokens_neox': 6944, 'n_words': 976, 'path': '2_Model_DL_v2.ipynb', 'repo_name': 'sidusit/MTech-Dissertation', 'size': '16942'}