{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Partie 1 - Préparation des données" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Importing the libraries\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Importing the training set\n", "dataset_train = pd.read_csv('Google_Stock_Price_Train.csv')\n", "training_set = dataset_train.iloc[:, 1:2].values" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Feature Scaling\n", "from sklearn.preprocessing import MinMaxScaler\n", "sc = MinMaxScaler(feature_range = (0, 1))\n", "training_set_scaled = sc.fit_transform(training_set)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Creating a data structure with 60 timesteps and 1 output\n", "X_train = []\n", "y_train = []\n", "for i in range(60, 1258):\n", " X_train.append(training_set_scaled[i-60:i, 0])\n", " y_train.append(training_set_scaled[i, 0])\n", "X_train, y_train = np.array(X_train), np.array(y_train)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Reshaping\n", "X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Partie 2 - RNN" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Importing the Keras libraries and packages\n", "from keras.models import Sequential\n", "from keras.layers import Dense\n", "from keras.layers import LSTM\n", "from keras.layers import Dropout\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Initialising the RNN\n", "regressor = Sequential()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Adding the first LSTM layer and some Dropout regularisation\n", "regressor.add(LSTM(units = 50, return_sequences = True, input_shape = (X_train.shape[1], 1)))\n", "regressor.add(Dropout(0.2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Adding a second LSTM layer and some Dropout regularisation\n", "regressor.add(LSTM(units = 50, return_sequences = True))\n", "regressor.add(Dropout(0.2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Adding a third LSTM layer and some Dropout regularisation\n", "regressor.add(LSTM(units = 50, return_sequences = True))\n", "regressor.add(Dropout(0.2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Adding a fourth LSTM layer and some Dropout regularisation\n", "regressor.add(LSTM(units = 50))\n", "regressor.add(Dropout(0.2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Adding the output layer\n", "regressor.add(Dense(units = 1))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Compiling the RNN\n", "regressor.compile(optimizer = 'adam', loss = 'mean_squared_error')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Fitting the RNN to the Training set\n", "regressor.fit(X_train, y_train, epochs = 100, batch_size = 32)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Partie 3 - Prédictions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Getting the real stock price of 2017\n", "dataset_test = pd.read_csv('Google_Stock_Price_Test.csv')\n", "real_stock_price = dataset_test.iloc[:, 1:2].values" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Getting the predicted stock price of 2017\n", "dataset_total = pd.concat((dataset_train['Open'], dataset_test['Open']), axis = 0)\n", "inputs = dataset_total[len(dataset_total) - len(dataset_test) - 60:].values\n", "inputs = inputs.reshape(-1,1)\n", "inputs = sc.transform(inputs)\n", "X_test = []\n", "for i in range(60, 80):\n", " X_test.append(inputs[i-60:i, 0])\n", "X_test = np.array(X_test)\n", "X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))\n", "predicted_stock_price = regressor.predict(X_test)\n", "predicted_stock_price = sc.inverse_transform(predicted_stock_price)\n", "\n", "# Visualising the results\n", "plt.plot(real_stock_price, color = 'red', label = 'Real Google Stock Price')\n", "plt.plot(predicted_stock_price, color = 'blue', label = 'Predicted Google Stock Price')\n", "plt.title('Google Stock Price Prediction')\n", "plt.xlabel('Time')\n", "plt.ylabel('Google Stock Price')\n", "plt.legend()\n", "plt.show()" ] } ], "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.6.10" } }, "nbformat": 4, "nbformat_minor": 4 }