<jupyter_start><jupyter_text>Step to build stock prediction model * Data Preprocessing* Building RNN* Making the prediction and visualization I use data from this link<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import StandardScaler, MinMaxScaler<jupyter_output><empty_output><jupyter_text>Data Preprocessing<jupyter_code>data = pd.read_csv('GOOG.csv', date_parser = True)
data.head()
# print("The size of the data: {}".format(data.size))
data_training = data[data['Date'] < '2019-01-01'].copy()
data_training.head()
data_test = data[data['Date'] >= '2019-01-01'].copy()
data_test
# Drop columns
training_data = data_training.drop(['Date','Adj Close'], axis = 1)
training_data
#Scale the data between 0 and 1
scaler = MinMaxScaler()
training_data = scaler.fit_transform(training_data)
training_data
training_data.shape[0]
X_train = []
y_train = []

for i in range(60, training_data.shape[0]):
    X_train.append(training_data[i-60: i])
    y_train.append(training_data[i, 0])
#convert list to numpy array
X_train, y_train = np.array(X_train), np.array(y_train)
X_train.shape
y_train.shape<jupyter_output><empty_output><jupyter_text>Building LSTM<jupyter_code>from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, LSTM, Dropout
# regressor for model selection
regressor = Sequential()

# layer1
regressor.add(LSTM(units = 50, activation = 'relu', return_sequences = True, input_shape = (X_train.shape[1], 5)))
regressor.add(Dropout(0.2))
# layer2
regressor.add(LSTM(units = 60, activation = 'relu', return_sequences = True))
regressor.add(Dropout(0.3))
# layer3
regressor.add(LSTM(units = 80, activation = 'relu', return_sequences = True))
regressor.add(Dropout(0.4))
# layer4
regressor.add(LSTM(units = 120, activation = 'relu'))
regressor.add(Dropout(0.5))
# final layer5
regressor.add(Dense(units = 1))
regressor.summary()
regressor.compile(optimizer = 'adam', loss= 'mean_squared_error')
regressor.fit(X_train, y_train, epochs = 10, batch_size = 32)<jupyter_output>Epoch 1/10
112/112 [==============================] - 17s 152ms/step - loss: 0.0129
Epoch 2/10
112/112 [==============================] - 16s 147ms/step - loss: 0.0050
Epoch 3/10
112/112 [==============================] - 16s 143ms/step - loss: 0.0039
Epoch 4/10
112/112 [==============================] - 16s 141ms/step - loss: 0.0035
Epoch 5/10
112/112 [==============================] - 16s 142ms/step - loss: 0.0030
Epoch 6/10
112/112 [==============================] - 16s 144ms/step - loss: 0.0030
Epoch 7/10
112/112 [==============================] - 16s 143ms/step - loss: 0.0028
Epoch 8/10
112/112 [==============================] - 16s 142ms/step - loss: 0.0026
Epoch 9/10
112/112 [==============================] - 15s 137ms/step - loss: 0.0024
Epoch 10/10
112/112 [==============================] - 16s 142ms/step - loss: 0.0023<jupyter_text>Testing To focus on testing, I must recall last 60 days' trained data.<jupyter_code>past_60_days = data_training.tail(60)
df = past_60_days.append(data_test , ignore_index= True)
df = df.drop(['Date', 'Adj Close'], axis = 1)
df.head()
inputs = scaler.transform(df)
inputs
X_test = []
y_test = []

for i in range(60, inputs.shape[0]):
    X_test.append(inputs[i-60: i])
    y_test.append(inputs[i, 0])
X_test, y_test  = np.array(X_test), np.array(y_test)
X_test.shape, y_test.shape
y_pred = regressor.predict(X_test)
y_pred<jupyter_output><empty_output><jupyter_text>This is scaled value. let's print scaled value!<jupyter_code># the data are scaled with these values
scaler.scale_
# actual value
scale = 1/8.18605127e-04
scale
# to get unscaled value
y_pred = y_pred * scale
y_test = y_pred * scale
y_pred
y_test<jupyter_output><empty_output><jupyter_text>Visualization<jupyter_code>plt.figure(figsize = (14,5))
plt.plot(y_test, color = 'red', label = 'Real Google Stock Price')
plt.plot(y_pred, color = 'blue', label = "Predicted Google Stock Price")
plt.title('Google Stock Price Prediction')
plt.xlabel('Time')
plt.ylabel('Google Stock Price')
plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20004855547462977, 'fraction_numerical': 0.06700655498907501, 'mean_word_length': 6.030716723549488, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '602184', 'license': 'Unlicense', 'n_tokens_mistral': 1696, 'n_tokens_neox': 1437, 'n_words': 338, 'path': 'Neural Networks/Recurrent Neural Networks/Google Stock Price Prediction using RNN - LSTM-checkpoint.ipynb', 'repo_name': 'neirinzaralwin/Deep-Learning', 'size': '4119'}