<jupyter_start><jupyter_text>Building LSTM Model<jupyter_code>'''
To use the model, we have to instantiate the model,
the add different layers which accept differnt parameters.
Dropout is used to aviod overfitting.
'''
model = Sequential()

#first layer
model.add(LSTM(units=64, activation='relu', return_sequences=True,
              input_shape=(X_train.shape[1],5)))
model.add(Dropout(0.3))

#second layer
model.add(LSTM(units=80, activation='relu', return_sequences=True))
model.add(Dropout(0.25))

#third layer
model.add(LSTM(units=64, activation='relu', return_sequences=True))
model.add(Dropout(0.3))
#fourth layer     
model.add(LSTM(units=120, activation='relu'))
model.add(Dropout(0.5))
#last layer       
model.add(Dense(units=1))
#the .summary() method displays the details of the arc.
model.summary()
#compiling our model allow us to chose the optimizer,loss and other parameters
model.compile(optimizer='adam',loss='mse',metrics=['accuracy'])
#training the LSTM model with 10 epochs and batch size of 32
model.fit(X_train,y_train, epochs=10,batch_size=32)
test_data.head()
#test = test.drop(['Date','Adj Close'], axis=1)
test = scaler.fit_transform(test_data)
#breaking the test data into 7 days as well
X_test = []
y_test = []

for i in range(7, test.shape[0]):
    X_test.append(test[i-7:i])
    y_test.append(test[i,0])
X_test,y_test = np.array(X_test), np.array(y_test)
X_test.shape,y_test.shape
#predicting the 7 days price
y_pred = model.predict(X_test)
scaler.scale_[0]
scale = 1/scaler.scale_[0]
#Getting the original price before scaling
y_pred = y_pred * scale
y_test = y_test* scale
y_pred
plt.figure(figsize=(14,5))
plt.plot(y_test,label='Actual')
plt.plot(y_pred,label='Predicted')
plt.xlabel('Time')
plt.ylabel('Stock Price')
plt.legend()
#plt.xlim('2018-01', '2020-08')
plt.show()
#importing mean_squared_error from sklearn
from sklearn.metrics import mean_squared_error
'''
    calculating the root mean_squared_error
    of the regression problem, a lower number
    tells us how well our model is doing as we
    experimentally train our model.
'''
rmse = np.sqrt(mean_squared_error(y_test,y_pred))
print(round(rmse,3))<jupyter_output>8.747
{'fraction_non_alphanumeric': 0.13535911602209943, 'fraction_numerical': 0.02716390423572744, 'mean_word_length': 5.964743589743589, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '557146', 'license': 'MIT', 'n_tokens_mistral': 825, 'n_tokens_neox': 770, 'n_words': 191, 'path': 'stockmarket.ipynb', 'repo_name': 'izzyx6/stock_market', 'size': '2172'}