{"cells":[{"cell_type":"markdown","metadata":{},"source":["# Google Stock Price Prediction RNN"]},{"cell_type":"markdown","metadata":{},"source":["## Load libraries"]},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"outputs":[],"source":["import numpy as np # linear algebra\n","import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n","import matplotlib.pyplot as plt"]},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"outputs":[],"source":["dataset_train = pd.read_csv(\"https://static-1300131294.cos.ap-shanghai.myqcloud.com/data/deep-learning/RNN/trainset.csv\")"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"9de88d689da1a279477470f6cef999c0a45bf98a","trusted":true},"outputs":[],"source":["dataset_train"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"99cca4c05240bd82c4492c6c3c200925c5c096e5","trusted":true},"outputs":[],"source":["trainset = dataset_train.iloc[:, 1:2].values"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"1f00141023a0cc15593c8ef1351883177ca3ba21","trusted":true},"outputs":[],"source":["trainset"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"1abc6fe47fb5c4443767a2e2053cf9999c8a9a5c","trusted":true},"outputs":[],"source":["from sklearn.preprocessing import MinMaxScaler\n","\n","sc = MinMaxScaler(feature_range=(0, 1))\n","training_scaled = sc.fit_transform(trainset)"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"15362ed79df83c13fbddff1889a71f21e968fbc2","trusted":true},"outputs":[],"source":["training_scaled"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"d0d549e9a4af9ab516c3304fb44d650a815dcedc","trusted":true},"outputs":[],"source":["x_train = []\n","y_train = []"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"4c4f8ba0c635e7da0ba1bc52abf0a42068acdc89","trusted":true},"outputs":[],"source":["for i in range(60, 1259):\n"," x_train.append(training_scaled[i - 60 : i, 0])\n"," y_train.append(training_scaled[i, 0])\n","x_train, y_train = np.array(x_train), np.array(y_train)"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"360eb79902c77fbba3f16ab47a5ff7593a177478","trusted":true},"outputs":[],"source":["x_train.shape"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"316c5775b5c0a984b432f138792d2c0e9a484491","trusted":true},"outputs":[],"source":["x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"28f5242c6f1961914d1f568e70228fd30c1cb7fa","trusted":true},"outputs":[],"source":["from keras.models import Sequential\n","from keras.layers import Dense\n","from keras.layers import LSTM\n","from keras.layers import Dropout"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"ed24d733423254641f134c0e84f44a8ce175589f","trusted":true},"outputs":[],"source":["regressor = Sequential()\n","regressor.add(LSTM(units=50, return_sequences=True, input_shape=(x_train.shape[1], 1)))"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"e7b3dd133e58cb2acc860a8cfcd1483ba70e6b1b","trusted":true},"outputs":[],"source":["regressor.add(Dropout(0.2))"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"6b746176cb04357ac1e4c3e4c91ecd70717fed47","trusted":true},"outputs":[],"source":["regressor.add(LSTM(units=50, return_sequences=True))\n","regressor.add(Dropout(0.2))"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"73fe0b95d62b599e8aae4e2a53a10bc7165f464c","trusted":true},"outputs":[],"source":["regressor.add(LSTM(units=50, return_sequences=True))\n","regressor.add(Dropout(0.2))"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"8c81a80d2ea3cdf653675def0bbba5f9572e8770","trusted":true},"outputs":[],"source":["regressor.add(LSTM(units=50))\n","regressor.add(Dropout(0.2))"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"2edc2684995e0033ae8a4997b61b92a66c5bceee","trusted":true},"outputs":[],"source":["regressor.add(Dense(units=1))"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"6681ff55d9a29b1ad0f37cdc82318fb2c8fbc0bd","trusted":true},"outputs":[],"source":["regressor.compile(optimizer=\"adam\", loss=\"mean_squared_error\")"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"b0287226b32109016b659a8ba484b5eb51e0d61a","trusted":true},"outputs":[],"source":["regressor.fit(x_train, y_train, epochs=50, batch_size=32)"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["# if you need a model trained, you can use this cell\n","import tensorflow as tf\n","from tensorflow.keras.models import load_model\n","from tensorflow.keras.utils import get_file\n","\n","model_url = \"https://static-1300131294.cos.ap-shanghai.myqcloud.com/data/deep-learning/RNN/RNN_model.h5\"\n","model_path = get_file(\"RNN_model.h5\", model_url)\n","LSTM_model = load_model(model_path)"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"16e063f81273faee696d9db103673ad2b88fda50","trusted":true},"outputs":[],"source":["dataset_test = pd.read_csv(\"https://static-1300131294.cos.ap-shanghai.myqcloud.com/data/deep-learning/RNN/testset.csv\")"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"6acd6a66dc0f9c013ea008c0d99734697757cae6","trusted":true},"outputs":[],"source":["real_stock_price = dataset_test.iloc[:, 1:2].values"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"b65a0a5b78f98a5ac13dee98e9738fd61426bfd0","trusted":true},"outputs":[],"source":["dataset_total = pd.concat((dataset_train[\"Open\"], dataset_test[\"Open\"]), axis=0)\n","dataset_total"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"31aa33ed70b08e71d0e8bfe4c1abae7901ab947d","trusted":true},"outputs":[],"source":["inputs = dataset_total[len(dataset_total) - len(dataset_test) - 60 :].values\n","inputs"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"7d74568c439acc8dda77dc4365d4b413095508e1","trusted":true},"outputs":[],"source":["inputs = inputs.reshape(-1, 1)"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"17b170e861d1353ece8aec66f700886106da4aef","trusted":true},"outputs":[],"source":["inputs"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"b4003370fb6454d0493ef80b1b50ea34061de7bc","trusted":true},"outputs":[],"source":["inputs = sc.transform(inputs)\n","inputs.shape"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"ef076f0fdbe6ec72d85e7326c3e8aa73fcbad667","trusted":true},"outputs":[],"source":["x_test = []\n","for i in range(60, 185):\n"," x_test.append(inputs[i - 60 : i, 0])"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"1fa15b6bb87ff2da8a730a6e1de20d5e5baf1571","trusted":true},"outputs":[],"source":["x_test = np.array(x_test)\n","x_test.shape"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"b244d0115671f29d46d2cb290f92e6a0fe38bc1d","trusted":true},"outputs":[],"source":["x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))\n","x_test.shape"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"24730042bdad3a140e033a78ae9b170304f8cefb","trusted":true},"outputs":[],"source":["predicted_price = regressor.predict(x_test)"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"4b06462fa06e7399373d8a552cd159176fbeef33","trusted":true},"outputs":[],"source":["predicted_price = sc.inverse_transform(predicted_price)\n","predicted_price"]},{"cell_type":"code","execution_count":null,"metadata":{"_uuid":"e8b30d578fafb1564da4448efb1f994537f6f588","trusted":true},"outputs":[],"source":["plt.plot(real_stock_price, color=\"red\", label=\"Real Price\")\n","plt.plot(predicted_price, color=\"blue\", label=\"Predicted Price\")\n","plt.title(\"Google Stock Price Prediction\")\n","plt.xlabel(\"Time\")\n","plt.ylabel(\"Google Stock Price\")\n","plt.legend()\n","plt.show()"]},{"cell_type":"markdown","metadata":{},"source":["# Acknowledgements\n","\n","Thanks to [Priya](https://www.kaggle.com/ptheru) for creating [Google stock price prediction - RNN](https://www.kaggle.com/code/ptheru/google-stock-price-prediction-rnn). It inspires the majority of the content in this chapter."]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.9.16"}},"nbformat":4,"nbformat_minor":4}