You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ML-For-Beginners/7-TimeSeries/3-SVR/solution/notebook.ipynb

1023 lines
619 KiB

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "fv9OoQsMFk5A"
},
"source": [
"# Time series prediction using Support Vector Regressor"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this notebook, we demonstrate how to:\n",
"\n",
"- prepare 2D time series data for training an SVM regressor model\n",
"- implement SVR using RBF kernel\n",
"- evaluate the model using plots and MAPE"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Importing modules"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"sys.path.append('../../')"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"id": "M687KNlQFp0-"
},
"outputs": [],
"source": [
"import os\n",
"import warnings\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import pandas as pd\n",
"import datetime as dt\n",
"import math\n",
"\n",
"from sklearn.svm import SVR\n",
"from sklearn.preprocessing import MinMaxScaler\n",
"from common.utils import load_data, mape"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Cj-kfVdMGjWP"
},
"source": [
"## Preparing data"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8fywSjC6GsRz"
},
"source": [
"### Load data"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 363
},
"id": "aBDkEB11Fumg",
"outputId": "99cf7987-0509-4b73-8cc2-75d7da0d2740"
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>load</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2012-01-01 00:00:00</th>\n",
" <td>2698.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2012-01-01 01:00:00</th>\n",
" <td>2558.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2012-01-01 02:00:00</th>\n",
" <td>2444.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2012-01-01 03:00:00</th>\n",
" <td>2402.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2012-01-01 04:00:00</th>\n",
" <td>2403.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" load\n",
"2012-01-01 00:00:00 2698.0\n",
"2012-01-01 01:00:00 2558.0\n",
"2012-01-01 02:00:00 2444.0\n",
"2012-01-01 03:00:00 2402.0\n",
"2012-01-01 04:00:00 2403.0"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"energy = load_data('../../data')[['load']]\n",
"energy.head(5)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "O0BWP13rGnh4"
},
"source": [
"### Plot the data"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 486
},
"id": "hGaNPKu_Gidk",
"outputId": "7f89b326-9057-4f49-efbe-cb100ebdf76d"
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA4kAAAH1CAYAAABbUbvGAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAC4S0lEQVR4nOzdd7wU1d0G8OfcQu9dQUA6goI0RQXFimL3jb3FrjHRmNg19hJrYo29d8WKFQQFC0WlSu8gvVzqBe695/1jd/bOzk7vs/t88/GTy+7M7NnZmTOn/o6QUoKIiIiIiIgIAIqiTgARERERERHFByuJRERERERElMFKIhEREREREWWwkkhEREREREQZrCQSERERERFRBiuJRERERERElFESdQKi0KxZM9m+ffuok0FERERERBSJX375Za2UsrneewVZSWzfvj0mTZoUdTKIiIiIiIgiIYRYbPQeh5sSERERERFRBiuJRERERERElMFKIhEREREREWUU5JxEIiIiIiIiANi1axeWLVuG8vLyqJMSiFq1aqFNmzYoLS21vQ8riUREREREVLCWLVuG+vXro3379hBCRJ0cX0kpsW7dOixbtgx77rmn7f043JSIiIiIiApWeXk5mjZtmncVRAAQQqBp06aOe0lZSSQiIiIiooKWjxVEhZvvxkoiERERERFRhOrVq+fLcW6//XY89NBDno/DSiIRERERERFlsJJIREREREQUA1JKXHvttejZsyf23ntvvPPOOwCALVu24LDDDkOfPn2w99574+OPP87sc88996BLly446KCDMHv2bF/SweimREREREREAO74dAZ+/2OTr8fca/cGuO24Hra2HT58OCZPnowpU6Zg7dq16N+/PwYPHozmzZvjww8/RIMGDbB27Vrsv//+OP744/Hrr7/i7bffxuTJk1FRUYE+ffqgb9++ntPMnkQiIiIiIqIYGDduHM444wwUFxejZcuWOPjggzFx4kRIKXHTTTdhn332weGHH47ly5dj1apVGDt2LE466STUqVMHDRo0wPHHH+9LOtiTSEREREREBNju8QvbG2+8gTVr1uCXX35BaWkp2rdv73hZCyfYk0hERERERBQDgwYNwjvvvIPKykqsWbMG33//PQYMGICysjK0aNECpaWlGD16NBYvXgwAGDx4MD766CNs374dmzdvxqeffupLOtiTSEREREREFAMnnXQSfvrpJ/Tq1QtCCDzwwANo1aoVzjrrLBx33HHYe++90a9fP3Tr1g0A0KdPH5x22mno1asXWrRogf79+/uSDiGl9OVASdKvXz85adKkqJNBREREREQRmzlzJrp37x51MgKl9x2FEL9IKfvpbc/hpkRERERERJTBSiIREVECvPbTIrS/YQQ2le+KOilERJTnWEkkIiJKgOfHLQQArNuyM+KUEBFRvmMlkYiIKAF2VVQB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1xBR+ATHkxakXZVVAIDVm9iTSP6p5IRl8hEriVSwynexVY2I4sGwbKdbh2RBkIhyVen0JCqYa5BTrCRSwXp05Jyok0BEZI4lOyKyyawnkWMWyKmSqBNAFJW1m3f6cpzv56xBw9qlvhyLiAqLZXRTykv83SkIVRxuSj5iJZEKlmE0QYfOfXGCL8chosLFsl1hkvzhiSimONyUChafzUQUe+xxyksi3ZXIxxD5SS8QVlH6WlP3Mj4/dgG63vJFaOmiZGIlkQpWWC24H/22HJvLd+m+t2jt1lDSQETxpBTpnKyTGOcGrvJdlfjHu1OwehMX7jbDuj8FQW8Yc1G6pK+uJN49YiZ2VFSFlCpKKlYSqWCFUc6avrwMV78zGTcMn6b7/v/976cQUkFEFI6vZqzEB78uw90jZkadlERwU+HfsNWf+fRUGKp7EnPf43BnMsNKIhWsMPLG7el
"text/plain": [
"<Figure size 1080x576 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"energy.plot(y='load', subplots=True, figsize=(15, 8), fontsize=12)\n",
"plt.xlabel('timestamp', fontsize=12)\n",
"plt.ylabel('load', fontsize=12)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "IPuNor4eGwYY"
},
"source": [
"### Create training and testing data"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"id": "ysvsNyONGt0Q"
},
"outputs": [],
"source": [
"train_start_dt = '2014-11-01 00:00:00'\n",
"test_start_dt = '2014-12-30 00:00:00'"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 548
},
"id": "SsfdLoPyGy9w",
"outputId": "d6d6c25b-b1f4-47e5-91d1-707e043237d7"
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA4kAAAITCAYAAACqpFnEAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAEAAElEQVR4nOz9d7gkyVUmjL+RmVV1bdvpHitpRiOLJCQhCdCHkxAsEny4FQiz2mXNhzD74yfWsAgjFrMLfBghBAtIeCuMkLBCFllkRjNyYzTe9vS073v7+qrKjO+PyBMZmRURGSdv3+rb3fE+Tz/3dt2Kyqw0kXHO+573CCklIiIiIiIiIiIiIiIiIiIAILnQOxARERERERERERERERGxexCDxIiIiIiIiIiIiIiIiAiNGCRGREREREREREREREREaMQgMSIiIiIiIiIiIiIiIkIjBokRERERERERERERERERGjFIjIiIiIiIiIiIiIiIiNDILvQOXAhcccUV8vrrr7/QuxEREREREREREREREXFBcMstt5ySUh6y/e2yDBKvv/563HzzzRd6NyIiIiIiIiIiIiIiIi4IhBAPuf4W5aYRERERERERERERERERGjFIjIiIiIiIiIiIiIiIiNCIQWJERERERERERERERESExmVZkxgRERERERERERERcXljNBrhyJEj2NzcvNC7sqOYmZnBddddh16vFzwmBokREREREREREREREZcdjhw5gsXFRVx//fUQQlzo3dkRSClx+vRpHDlyBDfccEPwuCg3jYiIiIiIiIiIiIi47LC5uYmDBw9esgEiAAghcPDgQTZbGoPEiIiIiIiIiIiIiIjLEpdygEjo8h1jkBgRERERERERERERETFlLC0t4Td+4zfY4772a78WS0tL53+HDMQgMSIiIiIiIiIiIiIiYspwBYnj8dg77u1vfzv27du3Q3ulEI1rIiIiIiIiIiIiIiIipozXvOY1uO+++/Cc5zwHvV4PMzMz2L9/P+68807cfffd+KZv+iY88sgj2NzcxKtf/Wq86lWvAgBcf/31uPnmm7G6uoqXvexl+NIv/VJ85CMfwbXXXou//du/xezs7Lb3LQaJERERERERERERERGXNX7q72/HHUfPndfP/Lxr9uB/fv0znH//+Z//edx222349Kc/jfe///34uq/7Otx2223ahfT3fu/3cODAAWxsbOAFL3gBXv7yl+PgwYO1z7jnnnvw5je/Gb/927+NV7ziFfjrv/5rvPKVr9z2vscgMSIiIiIiIiIiI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IiIiQiMGiRERERERERGXLca50pvGmsSIiIiICjFIjIiIiIiIiLgsIaXEuCxKjExiRERERIUYJEZERERERERclhiVLCIAbI1jn8SIiMsRUsr2N13k6PIdY5AYERERERERcVliZJjVRCYxIuLyw8zMDE6fPn1JB4pSSpw+fRozMzOscdHdNCIiIiIiIuKyxNhgEqO7aUTE5YfrrrsOR44cwcmTJy/0rvCweQ4YrQGLVwe9fWZmBtdddx1rEzFIjIiIiIiIiLgsMSoikxgRcTmj1+vhhhtuuNC7wcdP7lU//+cSIMSObCLKTSMiIiIiIiIuS0S5aURExEWNrZUd++gYJEZERERERERclohy04iIiIsaazsnk41BYkRERERERMRliWFkEiMiIi5GzOxTP9dP79gmYpAYERERERERcVmCmMQsEdiKQWJERMTFgtl96ufaqR3bRAwSIyIiIiIiIi5LUE3iXD+NTGJERMTFg9n96ud6DBIjIiIiIiIiIs4rKEhcGGSRSYyIiLh4QEFiZBIjIiIiIiIiIs4vxoWSm84NMgzHOWvs0aUNrG2Nd2K3IiIiIvxI++rnaGP
"text/plain": [
"<Figure size 1080x576 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"energy[(energy.index < test_start_dt) & (energy.index >= train_start_dt)][['load']].rename(columns={'load':'train'}) \\\n",
" .join(energy[test_start_dt:][['load']].rename(columns={'load':'test'}), how='outer') \\\n",
" .plot(y=['train', 'test'], figsize=(15, 8), fontsize=12)\n",
"plt.xlabel('timestamp', fontsize=12)\n",
"plt.ylabel('load', fontsize=12)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "XbFTqBw6G1Ch"
},
"source": [
"### Preparing data for training"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, you need to prepare the data for training by performing filtering and scaling of your data."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "cYivRdQpHDj3",
"outputId": "a138f746-461c-4fd6-bfa6-0cee094c4aa1"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Training data shape: (1416, 1)\n",
"Test data shape: (48, 1)\n"
]
}
],
"source": [
"train = energy.copy()[(energy.index >= train_start_dt) & (energy.index < test_start_dt)][['load']]\n",
"test = energy.copy()[energy.index >= test_start_dt][['load']]\n",
"\n",
"print('Training data shape: ', train.shape)\n",
"print('Test data shape: ', test.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Scale the data to be in the range (0, 1)."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 363
},
"id": "3DNntGQnZX8G",
"outputId": "210046bc-7a66-4ccd-d70d-aa4a7309949c"
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>load</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2014-11-01 00:00:00</th>\n",
" <td>0.101611</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-11-01 01:00:00</th>\n",
" <td>0.065801</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-11-01 02:00:00</th>\n",
" <td>0.046106</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-11-01 03:00:00</th>\n",
" <td>0.042525</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-11-01 04:00:00</th>\n",
" <td>0.059087</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" load\n",
"2014-11-01 00:00:00 0.101611\n",
"2014-11-01 01:00:00 0.065801\n",
"2014-11-01 02:00:00 0.046106\n",
"2014-11-01 03:00:00 0.042525\n",
"2014-11-01 04:00:00 0.059087"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"scaler = MinMaxScaler()\n",
"train['load'] = scaler.fit_transform(train)\n",
"train.head(5)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 206
},
"id": "26Yht-rzZexe",
"outputId": "20326077-a38a-4e78-cc5b-6fd7af95d301"
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>load</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2014-12-30 00:00:00</th>\n",
" <td>0.329454</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-12-30 01:00:00</th>\n",
" <td>0.290063</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-12-30 02:00:00</th>\n",
" <td>0.273948</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-12-30 03:00:00</th>\n",
" <td>0.268129</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-12-30 04:00:00</th>\n",
" <td>0.302596</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" load\n",
"2014-12-30 00:00:00 0.329454\n",
"2014-12-30 01:00:00 0.290063\n",
"2014-12-30 02:00:00 0.273948\n",
"2014-12-30 03:00:00 0.268129\n",
"2014-12-30 04:00:00 0.302596"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test['load'] = scaler.transform(test)\n",
"test.head(5)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "x0n6jqxOQ41Z"
},
"source": [
"### Creating data with time-steps"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fdmxTZtOQ8xs"
},
"source": [
" For our SVR, we transform the input data to be of the form `[batch, timesteps]`. So, we reshape the existing `train_data` and `test_data` such that there is a new dimension which refers to the timesteps. For our example, we take `timesteps = 5`. So, the inputs to the model are the data for the first 4 timesteps, and the output will be the data for the 5<sup>th</sup> timestep."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"id": "Rpju-Sc2HFm0"
},
"outputs": [],
"source": [
"# Converting to numpy arrays\n",
"\n",
"train_data = train.values\n",
"test_data = test.values"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# Selecting the timesteps\n",
"\n",
"timesteps=5"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "O-JrsrsVJhUQ",
"outputId": "c90dbe71-bacc-4ec4-b452-f82fe5aefaef"
},
"outputs": [
{
"data": {
"text/plain": [
"(1412, 5)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Converting data to 2D tensor\n",
"\n",
"train_data_timesteps=np.array([[j for j in train_data[i:i+timesteps]] for i in range(0,len(train_data)-timesteps+1)])[:,:,0]\n",
"train_data_timesteps.shape"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "exJD8AI7KE4g",
"outputId": "ce90260c-f327-427d-80f2-77307b5a6318"
},
"outputs": [
{
"data": {
"text/plain": [
"(44, 5)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Converting test data to 2D tensor\n",
"\n",
"test_data_timesteps=np.array([[j for j in test_data[i:i+timesteps]] for i in range(0,len(test_data)-timesteps+1)])[:,:,0]\n",
"test_data_timesteps.shape"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"id": "2u0R2sIsLuq5"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1412, 4) (1412, 1)\n",
"(44, 4) (44, 1)\n"
]
}
],
"source": [
"x_train, y_train = train_data_timesteps[:,:timesteps-1],train_data_timesteps[:,[timesteps-1]]\n",
"x_test, y_test = test_data_timesteps[:,:timesteps-1],test_data_timesteps[:,[timesteps-1]]\n",
"\n",
"print(x_train.shape, y_train.shape)\n",
"print(x_test.shape, y_test.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8wIPOtAGLZlh"
},
"source": [
"## Creating SVR model"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"id": "EhA403BEPEiD"
},
"outputs": [],
"source": [
"# Create model using RBF kernel\n",
"\n",
"model = SVR(kernel='rbf',gamma=0.5, C=10, epsilon = 0.05)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "GS0UA3csMbqp",
"outputId": "d86b6f05-5742-4c1d-c2db-c40510bd4f0d"
},
"outputs": [
{
"data": {
"text/plain": [
"SVR(C=10, cache_size=200, coef0=0.0, degree=3, epsilon=0.05, gamma=0.5,\n",
" kernel='rbf', max_iter=-1, shrinking=True, tol=0.001, verbose=False)"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Fit model on training data\n",
"\n",
"model.fit(x_train, y_train[:,0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Rz_x8S3UrlcF"
},
"source": [
"### Make model prediction"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "XR0gnt3MnuYS",
"outputId": "157e40ab-9a23-4b66-a885-0d52a24b2364"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1412, 1) (44, 1)\n"
]
}
],
"source": [
"# Making predictions\n",
"\n",
"y_train_pred = model.predict(x_train).reshape(-1,1)\n",
"y_test_pred = model.predict(x_test).reshape(-1,1)\n",
"\n",
"print(y_train_pred.shape, y_test_pred.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_2epncg-SGzr"
},
"source": [
"## Analyzing model performance"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1412 44\n"
]
}
],
"source": [
"# Scaling the predictions\n",
"\n",
"y_train_pred = scaler.inverse_transform(y_train_pred)\n",
"y_test_pred = scaler.inverse_transform(y_test_pred)\n",
"\n",
"print(len(y_train_pred), len(y_test_pred))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "xmm_YLXhq7gV",
"outputId": "18392f64-4029-49ac-c71a-a4e2411152a1"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1412 44\n"
]
}
],
"source": [
"# Scaling the original values\n",
"\n",
"y_train = scaler.inverse_transform(y_train)\n",
"y_test = scaler.inverse_transform(y_test)\n",
"\n",
"print(len(y_train), len(y_test))"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "u3LBj93coHEi",
"outputId": "d4fd49e8-8c6e-4bb0-8ef9-ca0b26d725b4"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1412 44\n"
]
}
],
"source": [
"# Extract the timesteps for x-axis\n",
"\n",
"train_timestamps = energy[(energy.index < test_start_dt) & (energy.index >= train_start_dt)].index[timesteps-1:]\n",
"test_timestamps = energy[test_start_dt:].index[timesteps-1:]\n",
"\n",
"print(len(train_timestamps), len(test_timestamps))"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABasAAAGDCAYAAAAlN0y0AAAAOXRFWHRTb2Z0d2FyZQBNYX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hiGYRiGYRiGYRiGYRiGebLDYvU50mq18NWvfhUf+tCHcOONNwIA0jTFr/7qr+K6667Ds5/9bLznPe/Bn/zJn+DkyZP43u/9Xnzv934vAKBarfbauemmm/CTP/mTAIB//Md/xHd+53fiec97Hl71qlfhzJkz5/19MQzDMAzDMAzDMAzDMAzDbAXOVh/ASLzpTZvT7vvfP/Aln/70p3HDDTfg6U9/OqampnDvvffi7rvvxuHDh3HffffBcRzMz89jcnIS/+f//B98+ctfxo4dO9Zt86UvfSnuuusuCCHwwQ9+EO985zvxR3/0R1TvimEYhmEYhmEYhmEYhmEY5oJle4vVW8jHPvYx/NIv/RIA4PWvfz0+9rGP4dChQ3jzm98Mx9Ef6+Tk5IbaPH78OH70R38Up06dQhzHuOqqq8iPm2EYhmEYhmEY5pyQErj9duA7vgMYYMRhGIZhGIY5F4YWq4UQNoB7AJxQSr1WCPFhAK8AsGRe8pNKqfuEEALAuwH8awAd8/g3TRs/AeC3zet/Tyn1VyMd/RAO6M1gfn4eX/rSl/DAAw9ACIE0TSGEwPXXXz/U7+uPSBOGYe/f/+W//Bf8yq/8Cl73utfhK1/5Ct761rdSHzrDMAzDMAzDMMy5cfvtwI03Ap/8JPCnf7rVR8MwDMMwzJOQjWRW/xKAR1Y89mtKqeea/+4zj/0/AJ5m/vtZAO8FACHEJIC3APhOAC8C8BYhxMQIx75l3HTTTfixH/sxHDlyBIcPH8axY8dw1VVX4TnPeQ7e//73Q0oJQIvaAFCr1dBsNnu/v3v
"text/plain": [
"<Figure size 1800x432 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"plt.figure(figsize=(25,6))\n",
"plt.plot(train_timestamps, y_train, color = 'red', linewidth=2.0, alpha = 0.6)\n",
"plt.plot(train_timestamps, y_train_pred, color = 'blue', linewidth=0.8)\n",
"plt.legend(['Actual','Predicted'])\n",
"plt.xlabel('Timestamp')\n",
"plt.title(\"Training data prediction\")\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "LnhzcnYtXHCm",
"outputId": "f5f0d711-f18b-4788-ad21-d4470ea2c02b"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"MAPE for training data: 1.7195710200875551 %\n"
]
}
],
"source": [
"print('MAPE for training data: ', mape(y_train_pred, y_train)*100, '%')"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 225
},
"id": "53Q02FoqQH4V",
"outputId": "53e2d59b-5075-4765-ad9e-aed56c966583"
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAmUAAADSCAYAAAAL37fDAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAABSYklEQVR4nO3dd3hU1dbA4d9Kofei0qSJ9B6KKIiIgKCoIIoFbAjYr+Varvez92vFhogNRJCqgCC9inRCB+kSpPciqfv7Y53AgAmkn0my3ueZh5kzM2f2HJKTdfZee21xzmGMMcYYY/wV4ncDjDHGGGOMBWXGGGOMMUHBgjJjjDHGmCBgQZkxxhhjTBCwoMwYY4wxJghYUGaMMcYYEwRSHJSJSKiILBOR8d7jb0Vki4hEercG3nYRkX4islFEVohIo4B93CUiG7zbXRn+bYwxxhhjsqmwVLz2MWAtUCRg27+dcyPPet21QDXv1gz4HGgmIiWAF4EIwAFLRGSsc+5gWhtvjDHGGJNTpCgoE5HyQCfgdeCJ87z8BmCQ06q080WkmIiUAVoDU5xzB7x9TgE6AEOT21GpUqVcpUqVUtJEY4wxxhhfLVmyZJ9zrnRa35/SnrIPgaeBwmdtf11EXgCmAc8656KBcsD2gNdEeduS256sSpUqsXjx4hQ20RhjjDHGPyKyLT3vP29OmYhcB+xxzi0566nngBpAE6AE8Ex6GhLwe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/q2QwIPCMuWgR162pHVmbIkwcefxze+b6srlie2GP
"text/plain": [
"<Figure size 720x216 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"plt.figure(figsize=(10,3))\n",
"plt.plot(test_timestamps, y_test, color = 'red', linewidth=2.0, alpha = 0.6)\n",
"plt.plot(test_timestamps, y_test_pred, color = 'blue', linewidth=0.8)\n",
"plt.legend(['Actual','Predicted'])\n",
"plt.xlabel('Timestamp')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "clOAUH-SXCJG",
"outputId": "a3aa85ff-126a-4a4a-cd9e-90b9cc465ef5"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"MAPE for testing data: 1.2623790187854018 %\n"
]
}
],
"source": [
"print('MAPE for testing data: ', mape(y_test_pred, y_test)*100, '%')"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "DHlKvVCId5ue"
},
"source": [
"## Full dataset prediction"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "cOFJ45vreO0N",
"outputId": "35628e33-ecf9-4966-8036-f7ea86db6f16"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tensor shape: (26300, 5)\n",
"X shape: (26300, 4) \n",
"Y shape: (26300, 1)\n"
]
}
],
"source": [
"# Extracting load values as numpy array\n",
"data = energy.copy().values\n",
"\n",
"# Scaling\n",
"data = scaler.transform(data)\n",
"\n",
"# Transforming to 2D tensor as per model input requirement\n",
"data_timesteps=np.array([[j for j in data[i:i+timesteps]] for i in range(0,len(data)-timesteps+1)])[:,:,0]\n",
"print(\"Tensor shape: \", data_timesteps.shape)\n",
"\n",
"# Selecting inputs and outputs from data\n",
"X, Y = data_timesteps[:,:timesteps-1],data_timesteps[:,[timesteps-1]]\n",
"print(\"X shape: \", X.shape,\"\\nY shape: \", Y.shape)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"id": "ESSAdQgwexIi"
},
"outputs": [],
"source": [
"# Make model predictions\n",
"Y_pred = model.predict(X).reshape(-1,1)\n",
"\n",
"# Inverse scale and reshape\n",
"Y_pred = scaler.inverse_transform(Y_pred)\n",
"Y = scaler.inverse_transform(Y)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 328
},
"id": "M_qhihN0RVVX",
"outputId": "a89cb23e-1d35-437f-9d63-8b8907e12f80"
},
"outputs": [
{
"data": {
"image/png": "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
"text/plain": [
"<Figure size 2160x576 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"plt.figure(figsize=(30,8))\n",
"plt.plot(Y, color = 'red', linewidth=2.0, alpha = 0.6)\n",
"plt.plot(Y_pred, color = 'blue', linewidth=1)\n",
"plt.legend(['Actual','Predicted'])\n",
"plt.xlabel('Timestamp')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "AcN7pMYXVGTK",
"outputId": "7e1c2161-47ce-496c-9d86-7ad9ae0df770"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"MAPE: 2.0572089029888656 %\n"
]
}
],
"source": [
"print('MAPE: ', mape(Y_pred, Y)*100, '%')"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"collapsed_sections": [],
"name": "Recurrent_Neural_Networks.ipynb",
"provenance": []
},
"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.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 1
}