{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from genetic import GeneticExtractor\n",
    "\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "np.random.seed(1337)  # Random seed for reproducibility\n",
    "\n",
    "from tslearn.datasets import UCR_UEA_datasets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load ItalyPowerDemand dataset\n",
    "X_train, y_train, X_test, y_test = UCR_UEA_datasets().load_dataset('ItalyPowerDemand')\n",
    "X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1]))\n",
    "X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 172064 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fbc0c2e32e8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 154268 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fbc0c2e32b0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Visualize the timeseries in the train and test set\n",
    "colors = ['r', 'b', 'g', 'y', 'c']\n",
    "\n",
    "plt.figure(figsize=(10, 5))\n",
    "for ts, label in zip(X_train, y_train):\n",
    "    plt.plot(range(len(ts)), ts, c=colors[label%len(colors)])\n",
    "plt.title('The timeseries in the train set')\n",
    "plt.show()\n",
    "\n",
    "plt.figure(figsize=(10, 5))\n",
    "for ts, label in zip(X_test, y_test):\n",
    "    plt.plot(range(len(ts)), ts, c=colors[label%len(colors)])\n",
    "plt.title('The timeseries in the test set')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "it\t\tavg\t\tstd\t\tmax\t\ttime\n",
      "1\t\t-0.3676\t\t0.08\t\t-0.207659\t1.7613\n",
      "2\t\t-0.3188\t\t0.078\t\t-0.192144\t1.7582\n",
      "3\t\t-0.2722\t\t0.07\t\t-0.190906\t2.0417\n",
      "4\t\t-0.2397\t\t0.053\t\t-0.190906\t1.9892\n",
      "5\t\t-0.2413\t\t0.067\t\t-0.190906\t1.4277\n",
      "6\t\t-0.1974\t\t0.02\t\t-0.177838\t1.2561\n",
      "7\t\t-0.1962\t\t0.021\t\t-0.177838\t0.8073\n",
      "8\t\t-0.1995\t\t0.039\t\t-0.177838\t0.9649\n",
      "9\t\t-0.1892\t\t0.01\t\t-0.177838\t0.8323\n",
      "10\t\t-0.1893\t\t0.011\t\t-0.176465\t0.9984\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 24672 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fbc2b74b6a0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 28240 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fbc03f6eb38>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Extract the shapelets using the train set. Enabled verbosity. \n",
    "# Plotting is possible too, by setting plot='notebook' in jupyter or True else\n",
    "genetic_extractor = GeneticExtractor(verbose=True, population_size=50, iterations=10, plot=None, location=True)\n",
    "genetic_extractor.fit(X_train, y_train)\n",
    "\n",
    "plt.figure()\n",
    "for shap in genetic_extractor.shapelets:\n",
    "    plt.plot(shap)\n",
    "plt.title('The extracted shapelets')\n",
    "plt.show()\n",
    "\n",
    "x, y, y_err, y_max = [], [], [], []\n",
    "for it, stat in genetic_extractor.history:\n",
    "    x.append(it)\n",
    "    y.append(stat['avg'])\n",
    "    y_err.append(stat['std'] ** 1.25)\n",
    "    y_max.append(stat['max'])\n",
    "x, y, y_err, y_max = np.array(x), np.array(y), np.array(y_err), np.array(y_max)\n",
    "\n",
    "plt.plot(x, y, 'k-', label='Average')\n",
    "plt.fill_between(x, y-y_err, y+y_err, label='Variance')\n",
    "plt.plot(x, y_max, 'k--', label='Maximum')\n",
    "plt.title('The fitness (negative logloss) in function of the number of generations')\n",
    "plt.xlabel('Generation')\n",
    "plt.ylabel('Fitness')\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy LR = 0.9329446064139941\n"
     ]
    }
   ],
   "source": [
    "# Calculate distances from timeseries to extracted shapelets and fit a Logistic Regression model\n",
    "distances_train = genetic_extractor.transform(X_train)\n",
    "distances_test = genetic_extractor.transform(X_test)\n",
    "\n",
    "lr = LogisticRegression(random_state=2020)\n",
    "lr.fit(distances_train, y_train)\n",
    "\n",
    "# Print the accuracy score on the test set\n",
    "print('Accuracy LR = {}'.format(accuracy_score(y_test, lr.predict(distances_test))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
