42.104. Google Stock Price Prediction RNN#

42.104.1. Load libraries#

import numpy as np  # linear algebra
import pandas as pd  # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt
dataset_train = pd.read_csv("https://static-1300131294.cos.ap-shanghai.myqcloud.com/data/deep-learning/RNN/trainset.csv")
dataset_train
trainset = dataset_train.iloc[:, 1:2].values
trainset
from sklearn.preprocessing import MinMaxScaler

sc = MinMaxScaler(feature_range=(0, 1))
training_scaled = sc.fit_transform(trainset)
training_scaled
x_train = []
y_train = []
for i in range(60, 1259):
    x_train.append(training_scaled[i - 60 : i, 0])
    y_train.append(training_scaled[i, 0])
x_train, y_train = np.array(x_train), np.array(y_train)
x_train.shape
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
regressor = Sequential()
regressor.add(LSTM(units=50, return_sequences=True, input_shape=(x_train.shape[1], 1)))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units=50, return_sequences=True))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units=50, return_sequences=True))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units=50))
regressor.add(Dropout(0.2))
regressor.add(Dense(units=1))
regressor.compile(optimizer="adam", loss="mean_squared_error")
regressor.fit(x_train, y_train, epochs=50, batch_size=32)
# if you need a model trained, you can use this cell
import tensorflow as tf
from tensorflow.keras.models import load_model
from tensorflow.keras.utils import get_file

model_url = "https://static-1300131294.cos.ap-shanghai.myqcloud.com/data/deep-learning/RNN/RNN_model.h5"
model_path = get_file("RNN_model.h5", model_url)
LSTM_model = load_model(model_path)
dataset_test = pd.read_csv("https://static-1300131294.cos.ap-shanghai.myqcloud.com/data/deep-learning/RNN/testset.csv")
real_stock_price = dataset_test.iloc[:, 1:2].values
dataset_total = pd.concat((dataset_train["Open"], dataset_test["Open"]), axis=0)
dataset_total
inputs = dataset_total[len(dataset_total) - len(dataset_test) - 60 :].values
inputs
inputs = inputs.reshape(-1, 1)
inputs
inputs = sc.transform(inputs)
inputs.shape
x_test = []
for i in range(60, 185):
    x_test.append(inputs[i - 60 : i, 0])
x_test = np.array(x_test)
x_test.shape
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
x_test.shape
predicted_price = regressor.predict(x_test)
predicted_price = sc.inverse_transform(predicted_price)
predicted_price
plt.plot(real_stock_price, color="red", label="Real Price")
plt.plot(predicted_price, color="blue", label="Predicted Price")
plt.title("Google Stock Price Prediction")
plt.xlabel("Time")
plt.ylabel("Google Stock Price")
plt.legend()
plt.show()

42.105. Acknowledgements#

Thanks to Priya for creating Google stock price prediction - RNN. It inspires the majority of the content in this chapter.