{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "Epileptic Seizure Recognition.ipynb", "provenance": [], "collapsed_sections": [], "authorship_tag": "ABX9TyOkmT2KGZjwqmWC6jYfLxc4", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "source": [ "# Epileptic Seizure Recognition" ], "metadata": { "id": "VNtL5AMo02-f" } }, { "cell_type": "markdown", "source": [ "### Mounting Gdrive" ], "metadata": { "id": "R5Jjxx4d0-Zv" } }, { "cell_type": "code", "source": [ "from google.colab import drive\n", "drive.mount('/content/drive')" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "OGu62BQOwbpf", "outputId": "4b78eef8-4751-4bef-998f-855c01b338c8" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n" ] } ] }, { "cell_type": "markdown", "source": [ "###Import Required Libraries" ], "metadata": { "id": "D6MjSbhg1DPj" } }, { "cell_type": "code", "source": [ "!pip install lime" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Q50Ya7P2UkhV", "outputId": "7d671de6-b966-431a-8173-becb494142ce" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Requirement already satisfied: lime in /usr/local/lib/python3.7/dist-packages (0.2.0.1)\n", "Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from lime) (1.21.5)\n", "Requirement already satisfied: tqdm in /usr/local/lib/python3.7/dist-packages (from lime) (4.63.0)\n", "Requirement already satisfied: matplotlib in /usr/local/lib/python3.7/dist-packages (from lime) (3.2.2)\n", "Requirement already satisfied: scikit-learn>=0.18 in /usr/local/lib/python3.7/dist-packages (from lime) (1.0.2)\n", "Requirement already satisfied: scipy in /usr/local/lib/python3.7/dist-packages (from lime) (1.4.1)\n", "Requirement already satisfied: scikit-image>=0.12 in /usr/local/lib/python3.7/dist-packages (from lime) (0.18.3)\n", "Requirement already satisfied: tifffile>=2019.7.26 in /usr/local/lib/python3.7/dist-packages (from scikit-image>=0.12->lime) (2021.11.2)\n", "Requirement already satisfied: networkx>=2.0 in /usr/local/lib/python3.7/dist-packages (from scikit-image>=0.12->lime) (2.6.3)\n", "Requirement already satisfied: PyWavelets>=1.1.1 in /usr/local/lib/python3.7/dist-packages (from scikit-image>=0.12->lime) (1.3.0)\n", "Requirement already satisfied: pillow!=7.1.0,!=7.1.1,>=4.3.0 in /usr/local/lib/python3.7/dist-packages (from scikit-image>=0.12->lime) (7.1.2)\n", "Requirement already satisfied: imageio>=2.3.0 in /usr/local/lib/python3.7/dist-packages (from scikit-image>=0.12->lime) (2.4.1)\n", "Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->lime) (2.8.2)\n", "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib->lime) (0.11.0)\n", "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->lime) (1.4.0)\n", "Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->lime) (3.0.7)\n", "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from kiwisolver>=1.0.1->matplotlib->lime) (3.10.0.2)\n", "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=2.1->matplotlib->lime) (1.15.0)\n", "Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.7/dist-packages (from scikit-learn>=0.18->lime) (3.1.0)\n", "Requirement already satisfied: joblib>=0.11 in /usr/local/lib/python3.7/dist-packages (from scikit-learn>=0.18->lime) (1.1.0)\n" ] } ] }, { "cell_type": "code", "source": [ "import pandas as pd\n", "import numpy as np\n", "import tensorflow as tf\n", "from tensorflow.keras.utils import to_categorical\n", "from sklearn.preprocessing import StandardScaler\n", "from sklearn.model_selection import train_test_split, cross_val_score\n", "from tensorflow.keras import Sequential\n", "from tensorflow.keras.layers import Dense, Dropout\n", "from tensorflow.keras.layers import Embedding\n", "from tensorflow.keras.layers import LSTM\n", "from sklearn.linear_model import LogisticRegression\n", "from sklearn.svm import SVC\n", "from sklearn.neighbors import KNeighborsClassifier\n", "from sklearn.svm import LinearSVC\n", "import warnings\n", "from sklearn.metrics import accuracy_score\n", "from sklearn.metrics import classification_report\n", "import lime\n", "from lime import lime_tabular\n", "warnings.filterwarnings('ignore')" ], "metadata": { "id": "asRxxUrTzjIr" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "### Dataframe Enquiry\n" ], "metadata": { "id": "Zq7h6i0S1UoC" } }, { "cell_type": "code", "source": [ "df = pd.read_csv(\"/content/drive/MyDrive/Colab Notebooks/Dataset/Epileptic Seizure Recognition.csv\")\n", "df" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 467 }, "id": "XPNuTLB8zx-M", "outputId": "81029a18-e79a-494e-fe90-e5224369592d" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " Unnamed X1 X2 X3 X4 X5 X6 X7 X8 X9 ... X170 \\\n", "0 X21.V1.791 135 190 229 223 192 125 55 -9 -33 ... -17 \n", "1 X15.V1.924 386 382 356 331 320 315 307 272 244 ... 164 \n", "2 X8.V1.1 -32 -39 -47 -37 -32 -36 -57 -73 -85 ... 57 \n", "3 X16.V1.60 -105 -101 -96 -92 -89 -95 -102 -100 -87 ... -82 \n", "4 X20.V1.54 -9 -65 -98 -102 -78 -48 -16 0 -21 ... 4 \n", "... ... ... ... ... ... ... ... ... ... ... ... ... \n", "11495 X22.V1.114 -22 -22 -23 -26 -36 -42 -45 -42 -45 ... 15 \n", "11496 X19.V1.354 -47 -11 28 77 141 211 246 240 193 ... -65 \n", "11497 X8.V1.28 14 6 -13 -16 10 26 27 -9 4 ... -65 \n", "11498 X10.V1.932 -40 -25 -9 -12 -2 12 7 19 22 ... 121 \n", "11499 X16.V1.210 29 41 57 72 74 62 54 43 31 ... -59 \n", "\n", " X171 X172 X173 X174 X175 X176 X177 X178 y \n", "0 -15 -31 -77 -103 -127 -116 -83 -51 4 \n", "1 150 146 152 157 156 154 143 129 1 \n", "2 64 48 19 -12 -30 -35 -35 -36 5 \n", "3 -81 -80 -77 -85 -77 -72 -69 -65 5 \n", "4 2 -12 -32 -41 -65 -83 -89 -73 5 \n", "... ... ... ... ... ... ... ... ... .. \n", "11495 16 12 5 -1 -18 -37 -47 -48 2 \n", "11496 -33 -7 14 27 48 77 117 170 1 \n", "11497 -48 -61 -62 -67 -30 -2 -1 -8 5 \n", "11498 135 148 143 116 86 68 59 55 3 \n", "11499 -25 -4 2 5 4 -2 2 20 4 \n", "\n", "[11500 rows x 180 columns]" ], "text/html": [ "\n", "
\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
UnnamedX1X2X3X4X5X6X7X8X9...X170X171X172X173X174X175X176X177X178y
0X21.V1.79113519022922319212555-9-33...-17-15-31-77-103-127-116-83-514
1X15.V1.924386382356331320315307272244...1641501461521571561541431291
2X8.V1.1-32-39-47-37-32-36-57-73-85...57644819-12-30-35-35-365
3X16.V1.60-105-101-96-92-89-95-102-100-87...-82-81-80-77-85-77-72-69-655
4X20.V1.54-9-65-98-102-78-48-160-21...42-12-32-41-65-83-89-735
..................................................................
11495X22.V1.114-22-22-23-26-36-42-45-42-45...1516125-1-18-37-47-482
11496X19.V1.354-47-112877141211246240193...-65-33-7142748771171701
11497X8.V1.28146-13-16102627-94...-65-48-61-62-67-30-2-1-85
11498X10.V1.932-40-25-9-12-21271922...121135148143116866859553
11499X16.V1.210294157727462544331...-59-25-4254-22204
\n", "

11500 rows × 180 columns

\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 44 } ] }, { "cell_type": "markdown", "source": [ "### Feature Label extraction" ], "metadata": { "id": "K_XZO5QQ3Jsu" } }, { "cell_type": "code", "source": [ "X = df.iloc[:,1:-1]\n", "y = df.iloc[:,-1:]" ], "metadata": { "id": "X0XkQefh13Ri" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "Feature" ], "metadata": { "id": "apBxECNK3OGk" } }, { "cell_type": "code", "source": [ "X" ], "metadata": { "id": "NH3iED4k3DcC", "colab": { "base_uri": "https://localhost:8080/", "height": 423 }, "outputId": "694e72f4-3eaa-457c-c3bd-d62da27d28d4" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 ... X169 X170 \\\n", "0 135 190 229 223 192 125 55 -9 -33 -38 ... 8 -17 \n", "1 386 382 356 331 320 315 307 272 244 232 ... 168 164 \n", "2 -32 -39 -47 -37 -32 -36 -57 -73 -85 -94 ... 29 57 \n", "3 -105 -101 -96 -92 -89 -95 -102 -100 -87 -79 ... -80 -82 \n", "4 -9 -65 -98 -102 -78 -48 -16 0 -21 -59 ... 10 4 \n", "... ... ... ... ... ... ... ... ... ... ... ... ... ... \n", "11495 -22 -22 -23 -26 -36 -42 -45 -42 -45 -49 ... 20 15 \n", "11496 -47 -11 28 77 141 211 246 240 193 136 ... -94 -65 \n", "11497 14 6 -13 -16 10 26 27 -9 4 14 ... -42 -65 \n", "11498 -40 -25 -9 -12 -2 12 7 19 22 29 ... 114 121 \n", "11499 29 41 57 72 74 62 54 43 31 23 ... -94 -59 \n", "\n", " X171 X172 X173 X174 X175 X176 X177 X178 \n", "0 -15 -31 -77 -103 -127 -116 -83 -51 \n", "1 150 146 152 157 156 154 143 129 \n", "2 64 48 19 -12 -30 -35 -35 -36 \n", "3 -81 -80 -77 -85 -77 -72 -69 -65 \n", "4 2 -12 -32 -41 -65 -83 -89 -73 \n", "... ... ... ... ... ... ... ... ... \n", "11495 16 12 5 -1 -18 -37 -47 -48 \n", "11496 -33 -7 14 27 48 77 117 170 \n", "11497 -48 -61 -62 -67 -30 -2 -1 -8 \n", "11498 135 148 143 116 86 68 59 55 \n", "11499 -25 -4 2 5 4 -2 2 20 \n", "\n", "[11500 rows x 178 columns]" ], "text/html": [ "\n", "
\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
X1X2X3X4X5X6X7X8X9X10...X169X170X171X172X173X174X175X176X177X178
013519022922319212555-9-33-38...8-17-15-31-77-103-127-116-83-51
1386382356331320315307272244232...168164150146152157156154143129
2-32-39-47-37-32-36-57-73-85-94...2957644819-12-30-35-35-36
3-105-101-96-92-89-95-102-100-87-79...-80-82-81-80-77-85-77-72-69-65
4-9-65-98-102-78-48-160-21-59...1042-12-32-41-65-83-89-73
..................................................................
11495-22-22-23-26-36-42-45-42-45-49...201516125-1-18-37-47-48
11496-47-112877141211246240193136...-94-65-33-714274877117170
11497146-13-16102627-9414...-42-65-48-61-62-67-30-2-1-8
11498-40-25-9-12-2127192229...11412113514814311686685955
1149929415772746254433123...-94-59-25-4254-2220
\n", "

11500 rows × 178 columns

\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 46 } ] }, { "cell_type": "markdown", "source": [ "Label\n" ], "metadata": { "id": "GGy_lI3b3RPZ" } }, { "cell_type": "code", "source": [ "y" ], "metadata": { "id": "1DfH6WnK3SjB", "colab": { "base_uri": "https://localhost:8080/", "height": 423 }, "outputId": "e0aa5d4f-15a0-4b50-8e17-5cfe6d6db7d6" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " y\n", "0 4\n", "1 1\n", "2 5\n", "3 5\n", "4 5\n", "... ..\n", "11495 2\n", "11496 1\n", "11497 5\n", "11498 3\n", "11499 4\n", "\n", "[11500 rows x 1 columns]" ], "text/html": [ "\n", "
\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
y
04
11
25
35
45
......
114952
114961
114975
114983
114994
\n", "

11500 rows × 1 columns

\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 47 } ] }, { "cell_type": "markdown", "source": [ "### Feature Engineering" ], "metadata": { "id": "iebCP7AR3vYC" } }, { "cell_type": "markdown", "source": [ "#### Multi label to binary label conversion" ], "metadata": { "id": "4rh7-z-m4EnZ" } }, { "cell_type": "code", "source": [ "def toBinary(x):\n", " if x != 1: return 0;\n", " else: return 1;" ], "metadata": { "id": "GWe5qER_0pIN" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "y = y['y'].apply(toBinary)\n", "y = pd.DataFrame(data=y)\n", "y" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 423 }, "id": "4HXmsAwD1a90", "outputId": "694feb36-d060-492a-93a1-d3d1b9371884" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " y\n", "0 0\n", "1 1\n", "2 0\n", "3 0\n", "4 0\n", "... ..\n", "11495 0\n", "11496 1\n", "11497 0\n", "11498 0\n", "11499 0\n", "\n", "[11500 rows x 1 columns]" ], "text/html": [ "\n", "
\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
y
00
11
20
30
40
......
114950
114961
114970
114980
114990
\n", "

11500 rows × 1 columns

\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 49 } ] }, { "cell_type": "markdown", "source": [ "#### Feature Scaling" ], "metadata": { "id": "HjXaPE6m4X-R" } }, { "cell_type": "code", "source": [ "# scaler = StandardScaler()\n", "# X = scaler.fit_transform(X)" ], "metadata": { "id": "r3qlclsD3xSo" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "X" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 423 }, "id": "wRHvTMhS5Not", "outputId": "a0127ca2-8719-4840-a2ae-80029f8c2e81" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 ... X169 X170 \\\n", "0 135 190 229 223 192 125 55 -9 -33 -38 ... 8 -17 \n", "1 386 382 356 331 320 315 307 272 244 232 ... 168 164 \n", "2 -32 -39 -47 -37 -32 -36 -57 -73 -85 -94 ... 29 57 \n", "3 -105 -101 -96 -92 -89 -95 -102 -100 -87 -79 ... -80 -82 \n", "4 -9 -65 -98 -102 -78 -48 -16 0 -21 -59 ... 10 4 \n", "... ... ... ... ... ... ... ... ... ... ... ... ... ... \n", "11495 -22 -22 -23 -26 -36 -42 -45 -42 -45 -49 ... 20 15 \n", "11496 -47 -11 28 77 141 211 246 240 193 136 ... -94 -65 \n", "11497 14 6 -13 -16 10 26 27 -9 4 14 ... -42 -65 \n", "11498 -40 -25 -9 -12 -2 12 7 19 22 29 ... 114 121 \n", "11499 29 41 57 72 74 62 54 43 31 23 ... -94 -59 \n", "\n", " X171 X172 X173 X174 X175 X176 X177 X178 \n", "0 -15 -31 -77 -103 -127 -116 -83 -51 \n", "1 150 146 152 157 156 154 143 129 \n", "2 64 48 19 -12 -30 -35 -35 -36 \n", "3 -81 -80 -77 -85 -77 -72 -69 -65 \n", "4 2 -12 -32 -41 -65 -83 -89 -73 \n", "... ... ... ... ... ... ... ... ... \n", "11495 16 12 5 -1 -18 -37 -47 -48 \n", "11496 -33 -7 14 27 48 77 117 170 \n", "11497 -48 -61 -62 -67 -30 -2 -1 -8 \n", "11498 135 148 143 116 86 68 59 55 \n", "11499 -25 -4 2 5 4 -2 2 20 \n", "\n", "[11500 rows x 178 columns]" ], "text/html": [ "\n", "
\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
X1X2X3X4X5X6X7X8X9X10...X169X170X171X172X173X174X175X176X177X178
013519022922319212555-9-33-38...8-17-15-31-77-103-127-116-83-51
1386382356331320315307272244232...168164150146152157156154143129
2-32-39-47-37-32-36-57-73-85-94...2957644819-12-30-35-35-36
3-105-101-96-92-89-95-102-100-87-79...-80-82-81-80-77-85-77-72-69-65
4-9-65-98-102-78-48-160-21-59...1042-12-32-41-65-83-89-73
..................................................................
11495-22-22-23-26-36-42-45-42-45-49...201516125-1-18-37-47-48
11496-47-112877141211246240193136...-94-65-33-714274877117170
11497146-13-16102627-9414...-42-65-48-61-62-67-30-2-1-8
11498-40-25-9-12-2127192229...11412113514814311686685955
1149929415772746254433123...-94-59-25-4254-2220
\n", "

11500 rows × 178 columns

\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 51 } ] }, { "cell_type": "code", "source": [ "y" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 423 }, "id": "nMwtOiCh5jkd", "outputId": "07375ec2-d854-4b3a-8b4a-090ab9a643de" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " y\n", "0 0\n", "1 1\n", "2 0\n", "3 0\n", "4 0\n", "... ..\n", "11495 0\n", "11496 1\n", "11497 0\n", "11498 0\n", "11499 0\n", "\n", "[11500 rows x 1 columns]" ], "text/html": [ "\n", "
\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
y
00
11
20
30
40
......
114950
114961
114970
114980
114990
\n", "

11500 rows × 1 columns

\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 52 } ] }, { "cell_type": "markdown", "source": [ "### Splitting Data (Train-Test)" ], "metadata": { "id": "nuMY_vws4gzy" } }, { "cell_type": "code", "source": [ "x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.3)" ], "metadata": { "id": "bkEFX9kK4hL4" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "x_test.iloc[1]" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "xCJ-bFv9XAbj", "outputId": "f6c8bc38-1b38-4128-df1e-c2220b922374" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "X1 -101\n", "X2 -121\n", "X3 -134\n", "X4 -148\n", "X5 -152\n", " ... \n", "X174 -16\n", "X175 -4\n", "X176 9\n", "X177 16\n", "X178 19\n", "Name: 4458, Length: 178, dtype: int64" ] }, "metadata": {}, "execution_count": 54 } ] }, { "cell_type": "markdown", "source": [ "## Building Models (classical ML)" ], "metadata": { "id": "-Aj7lISc2R-X" } }, { "cell_type": "markdown", "source": [ "### Logistic Regression" ], "metadata": { "id": "9o8MoRr22YQj" } }, { "cell_type": "markdown", "source": [ "**Training data accuracy evaluation**" ], "metadata": { "id": "W05ryyZX4437" } }, { "cell_type": "code", "source": [ "clf = LogisticRegression() #initializing logistic regression\n", "clf.fit(x_train, y_train) #training the model with train data(input, output)\n", "acc_log_reg = clf.score(x_train, y_train) * 100\n", "print(round(acc_log_reg,2), \"%\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "tygNnA9w2X4e", "outputId": "71bc4e2f-b6bf-4a77-d008-1d9bde47fc73" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "67.45 %\n" ] } ] }, { "cell_type": "markdown", "source": [ "**Test Data accuracy evaluation**" ], "metadata": { "id": "T87vtDes5BIm" } }, { "cell_type": "code", "source": [ "y_pred_log_reg = clf.predict(x_test)\n", "acc_log_reg2 = round(clf.score(x_test, y_test) * 100, 2)\n", "print(acc_log_reg2, \"%\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "-6XEP0Xm43v3", "outputId": "ad4c7c89-8e72-462f-fc89-453cedb0ec34" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "64.17 %\n" ] } ] }, { "cell_type": "markdown", "source": [ "***Model Report***\n" ], "metadata": { "id": "WdCf2gb-PtEd" } }, { "cell_type": "code", "source": [ "predictions = clf.predict(x_test)\n", "print(classification_report(y_test, predictions))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "AJbbEySSPtbw", "outputId": "cd0b558f-23f0-4e0f-a483-3a4237f074ac" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ " precision recall f1-score support\n", "\n", " 0 0.82 0.70 0.76 2740\n", " 1 0.26 0.41 0.32 710\n", "\n", " accuracy 0.64 3450\n", " macro avg 0.54 0.56 0.54 3450\n", "weighted avg 0.71 0.64 0.67 3450\n", "\n" ] } ] }, { "cell_type": "markdown", "source": [ "***Model interpretation (LIME)***" ], "metadata": { "id": "HEHjWmqbiDDA" } }, { "cell_type": "code", "source": [ "explainer = lime_tabular.LimeTabularExplainer(\n", " training_data=np.array(x_train),\n", " feature_names=x_train.columns,\n", " class_names=[0, 1],\n", " mode='classification'\n", ")" ], "metadata": { "id": "aet_btfPkzSS" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "exp = explainer.explain_instance(\n", " data_row=x_test.iloc[0], \n", " predict_fn=clf.predict_proba\n", ")\n", "\n", "exp.show_in_notebook(show_table=True)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 331 }, "outputId": "e6719d56-6489-431d-ad90-d9fab9400599", "id": "5lmbGXf3iDDL" }, "execution_count": null, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "" ], "text/html": [ "\n", " \n", " \n", "
\n", " \n", " \n", " " ] }, "metadata": {} } ] }, { "cell_type": "markdown", "source": [ "### SVM" ], "metadata": { "id": "W50yLMiJ68qe" } }, { "cell_type": "markdown", "source": [ "**Training data accuracy evaluation**" ], "metadata": { "id": "Py0B_fMR-ak4" } }, { "cell_type": "code", "source": [ "clf = SVC(probability=True) #initializing svm classifier\n", "clf.fit(x_train, y_train) #training the model with train data(input, output)\n", "acc_svc1 = clf.score(x_train, y_train) * 100\n", "print(round(acc_svc1,2), '%')" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "C_tLS9J569Fl", "outputId": "b9a92991-8c58-4522-aa9b-62adb2cc5735" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "98.07 %\n" ] } ] }, { "cell_type": "markdown", "source": [ "**Test Data accuracy evaluation**" ], "metadata": { "id": "vAh1nvp_-nJj" } }, { "cell_type": "code", "source": [ "y_pred_svc = clf.predict(x_test)\n", "acc_svc2 = round(clf.score(x_test, y_test) * 100, 2)\n", "print(acc_svc2, \"%\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "b4b9fa30-076a-4579-b0ef-d5475df4acb7", "id": "Lj2qa-Vv-v6z" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "97.3 %\n" ] } ] }, { "cell_type": "markdown", "source": [ "***Model Report***\n" ], "metadata": { "id": "IG3c4bptSlcS" } }, { "cell_type": "code", "source": [ "predictions = clf.predict(x_test)\n", "print(classification_report(y_test, predictions))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "a0e6c6fd-3940-43da-90d2-a26cc608a41a", "id": "KnY5PZzESlcT" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ " precision recall f1-score support\n", "\n", " 0 0.98 0.99 0.98 2740\n", " 1 0.96 0.90 0.93 710\n", "\n", " accuracy 0.97 3450\n", " macro avg 0.97 0.95 0.96 3450\n", "weighted avg 0.97 0.97 0.97 3450\n", "\n" ] } ] }, { "cell_type": "markdown", "source": [ "***Model interpretation (LIME)***" ], "metadata": { "id": "POnWq7PpWd2a" } }, { "cell_type": "code", "source": [ "exp = explainer.explain_instance(\n", " data_row=x_test.iloc[2222], \n", " predict_fn=clf.predict_proba\n", ")\n", "\n", "exp.show_in_notebook(show_table=True)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 331 }, "id": "75WL4Zk8WeVw", "outputId": "53363c04-64b0-4c99-8eb8-7698b41c770a" }, "execution_count": null, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "" ], "text/html": [ "\n", " \n", " \n", "
\n", " \n", " \n", " " ] }, "metadata": {} } ] }, { "cell_type": "markdown", "source": [ "### Linear SVM" ], "metadata": { "id": "opVRLfML9Iym" } }, { "cell_type": "markdown", "source": [ "**Train Data accuracy evaluation**" ], "metadata": { "id": "sLwq23WI_Fww" } }, { "cell_type": "code", "source": [ "clf = LinearSVC() #initializing svm classifier\n", "# clf = SVC(kernel='linear',probability=True)\n", "clf.fit(x_train, y_train) #training the model with train data(input, output)\n", "acc_linear_svc1 = clf.score(x_train, y_train) * 100\n", "print(round(acc_linear_svc1,2), '%')" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "64diTNFE9JMM", "outputId": "f9c95f63-2b18-4732-813e-1b8bfd751680" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "85.3 %\n" ] } ] }, { "cell_type": "markdown", "source": [ "**Test Data accuracy evaluation**" ], "metadata": { "id": "p7XtFEsd_Dti" } }, { "cell_type": "code", "source": [ "y_pred_linear_svc = clf.predict(x_test)\n", "acc_linear_svc2 = round(clf.score(x_test, y_test) * 100, 2)\n", "print(acc_linear_svc2, \"%\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "56056972-6fbc-4cf4-c4d0-d84028271a4c", "id": "bDKYT6wJ_Dtj" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "84.55 %\n" ] } ] }, { "cell_type": "markdown", "source": [ "***Model Report***\n" ], "metadata": { "id": "bBmTOT_BTIJa" } }, { "cell_type": "code", "source": [ "predictions = clf.predict(x_test)\n", "print(classification_report(y_test, predictions))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "480ff75a-71dc-4510-818d-6a3904b391f4", "id": "EZdbWQPsTIJa" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ " precision recall f1-score support\n", "\n", " 0 0.84 1.00 0.91 2740\n", " 1 0.94 0.27 0.41 710\n", "\n", " accuracy 0.85 3450\n", " macro avg 0.89 0.63 0.66 3450\n", "weighted avg 0.86 0.85 0.81 3450\n", "\n" ] } ] }, { "cell_type": "markdown", "source": [ "### KNN" ], "metadata": { "id": "_anjVb808NzB" } }, { "cell_type": "markdown", "source": [ "**Train Data accuracy evaluation**" ], "metadata": { "id": "SXfdDfW5_d7_" } }, { "cell_type": "code", "source": [ "clf = KNeighborsClassifier() #initializing svm classifier\n", "clf.fit(x_train, y_train) #training the model with train data(input, output)\n", "acc_knn1 = clf.score(x_train, y_train) * 100\n", "print(round(acc_knn1,2), '%')" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "xsPHCC3j8Oiz", "outputId": "179c989a-823b-43cd-b403-45984ad02961" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "93.7 %\n" ] } ] }, { "cell_type": "markdown", "source": [ "**Test Data accuracy evaluation**" ], "metadata": { "id": "3R80tO4O_aCJ" } }, { "cell_type": "code", "source": [ "y_pred_knn = clf.predict(x_test)\n", "acc_knn2 = round(clf.score(x_test, y_test) * 100, 2)\n", "print(acc_knn2, \"%\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "a7924b98-3b97-47b5-faba-9f285e31d1ac", "id": "i04OzWhD_aCM" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "91.8 %\n" ] } ] }, { "cell_type": "markdown", "source": [ "***Model Report***\n" ], "metadata": { "id": "Pp1tU6W9TJd9" } }, { "cell_type": "code", "source": [ "predictions = clf.predict(x_test)\n", "print(classification_report(y_test, predictions))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "e7105f5c-4253-43b5-a875-0738e25de7cf", "id": "O-AC0KmPTJd9" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ " precision recall f1-score support\n", "\n", " 0 0.91 1.00 0.95 2740\n", " 1 0.99 0.61 0.75 710\n", "\n", " accuracy 0.92 3450\n", " macro avg 0.95 0.80 0.85 3450\n", "weighted avg 0.92 0.92 0.91 3450\n", "\n" ] } ] }, { "cell_type": "markdown", "source": [ "***Model interpretation (LIME)***" ], "metadata": { "id": "EQVYvSSUlMr4" } }, { "cell_type": "code", "source": [ "exp = explainer.explain_instance(\n", " data_row=x_test.iloc[2222], \n", " predict_fn=clf.predict_proba\n", ")\n", "\n", "exp.show_in_notebook(show_table=True)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 331 }, "outputId": "145e9f94-e2e9-4221-89c0-c2cca2737d4d", "id": "AVRVNEX3lMr4" }, "execution_count": null, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "" ], "text/html": [ "\n", " \n", " \n", "
\n", " \n", " \n", " " ] }, "metadata": {} } ] }, { "cell_type": "markdown", "source": [ "## RNN (Neural Network)" ], "metadata": { "id": "WX82zuu1A-5d" } }, { "cell_type": "markdown", "source": [ "### feature engineering for RNN" ], "metadata": { "id": "5SCMo2Zu9o-q" } }, { "cell_type": "code", "source": [ "y = to_categorical(y)\n", "x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)\n", "X_train = x_train\n", "X_test = x_test\n", "\n", "scaler = StandardScaler()\n", "\n", "x_train = scaler.fit_transform(x_train)\n", "x_train = np.reshape(x_train, (x_train.shape[0],1,X.shape[1]))\n", "\n", "x_test = scaler.fit_transform(x_test)\n", "x_test = np.reshape(x_test, (x_test.shape[0],1,X.shape[1]))" ], "metadata": { "id": "u76-M9hFpZpr" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "print(type(x_train))\n", "print(type(x_test))\n", "print(type(X_train))\n", "print(type(X_test))\n", "print(type(y_train))\n", "print(type(y_test))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "jjxGycsxduxE", "outputId": "86149d7e-d2c5-4316-ec69-f675857bb554" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\n", "\n", "\n", "\n", "\n", "\n" ] } ] }, { "cell_type": "markdown", "source": [ "### LSTM / BiLSTM" ], "metadata": { "id": "ZQX1XBJc4htc" } }, { "cell_type": "code", "source": [ "tf.keras.backend.clear_session()\n", "\n", "model = Sequential()\n", "model.add(LSTM(64, input_shape=(1,178),activation=\"relu\",return_sequences=True))\n", "model.add(LSTM(32,activation=\"sigmoid\"))\n", "model.add(Dense(2, activation='softmax'))\n", "model.compile(loss = 'categorical_crossentropy', optimizer = \"adam\", metrics = ['accuracy'])\n", "model.summary()" ], "metadata": { "id": "1bMOc7Ac4iHt", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "bc418925-26c6-4e31-93dd-64eb7a91c2e4" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Model: \"sequential\"\n", "_________________________________________________________________\n", " Layer (type) Output Shape Param # \n", "=================================================================\n", " lstm (LSTM) (None, 1, 64) 62208 \n", " \n", " lstm_1 (LSTM) (None, 32) 12416 \n", " \n", " dense (Dense) (None, 2) 66 \n", " \n", "=================================================================\n", "Total params: 74,690\n", "Trainable params: 74,690\n", "Non-trainable params: 0\n", "_________________________________________________________________\n" ] } ] }, { "cell_type": "markdown", "source": [ "### Fitting Model" ], "metadata": { "id": "1ZCeoHST4inc" } }, { "cell_type": "code", "source": [ "history = model.fit(x_train, y_train, epochs = 10)" ], "metadata": { "id": "qZYHIlFi4jBX", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "1ffbd8d0-c21b-4be2-ccab-e886d8fe628d" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Epoch 1/10\n", "288/288 [==============================] - 3s 4ms/step - loss: 0.3133 - accuracy: 0.8862\n", "Epoch 2/10\n", "288/288 [==============================] - 1s 4ms/step - loss: 0.1058 - accuracy: 0.9708\n", "Epoch 3/10\n", "288/288 [==============================] - 1s 4ms/step - loss: 0.0699 - accuracy: 0.9808\n", "Epoch 4/10\n", "288/288 [==============================] - 1s 4ms/step - loss: 0.0518 - accuracy: 0.9849\n", "Epoch 5/10\n", "288/288 [==============================] - 1s 4ms/step - loss: 0.0414 - accuracy: 0.9883\n", "Epoch 6/10\n", "288/288 [==============================] - 1s 4ms/step - loss: 0.0328 - accuracy: 0.9905\n", "Epoch 7/10\n", "288/288 [==============================] - 1s 4ms/step - loss: 0.0248 - accuracy: 0.9930\n", "Epoch 8/10\n", "288/288 [==============================] - 1s 4ms/step - loss: 0.0187 - accuracy: 0.9948\n", "Epoch 9/10\n", "288/288 [==============================] - 1s 4ms/step - loss: 0.0139 - accuracy: 0.9968\n", "Epoch 10/10\n", "288/288 [==============================] - 1s 4ms/step - loss: 0.0112 - accuracy: 0.9970\n" ] } ] }, { "cell_type": "markdown", "source": [ "### Accuracy Evaluation" ], "metadata": { "id": "zOcixyVH4jka" } }, { "cell_type": "markdown", "source": [ "Train data\n" ], "metadata": { "id": "On1LrUhEAYOw" } }, { "cell_type": "code", "source": [ "scoreTrain, accTrain = model.evaluate(x_train, y_train)\n", "print(round(accTrain*100, 2), '%')" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "7a4dda27-06ea-40f6-863a-fe4e92b25652", "id": "z-LAzi0eAXfj" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "288/288 [==============================] - 1s 2ms/step - loss: 0.0098 - accuracy: 0.9973\n", "99.73 %\n" ] } ] }, { "cell_type": "markdown", "source": [ "Test Data" ], "metadata": { "id": "azGu4-3ZAaTH" } }, { "cell_type": "code", "source": [ "scoreTest, accTest = model.evaluate(x_test, y_test)\n", "print(round(accTest*100, 2), '%')" ], "metadata": { "id": "ez6CVqLS4kb8", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "e7190177-7c7c-4e48-f851-88b4f325f1c6" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "72/72 [==============================] - 0s 2ms/step - loss: 0.0906 - accuracy: 0.9743\n", "97.43 %\n" ] } ] }, { "cell_type": "markdown", "source": [ "**Individual check**" ], "metadata": { "id": "G5vKlM9ACXAs" } }, { "cell_type": "code", "source": [ "print(x_test[22,:])\n", "print(y_test[22,:])" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "M9L7FX9VBgFV", "outputId": "64c3fe06-797d-4463-af84-26185f7539be" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[[-0.27394654 -0.32804627 -0.42904765 -0.48753605 -0.54755226 -0.58983591\n", " -0.58537214 -0.55949834 -0.43968008 -0.37293693 -0.34310263 -0.31993666\n", " -0.27848473 -0.2196931 -0.17781191 -0.12804085 -0.14068347 -0.07507794\n", " -0.00790428 0.04495067 0.02981974 0.02817117 0.06060217 0.05034591\n", " 0.08619392 0.10422928 0.07772134 0.08832882 0.03174449 -0.01619464\n", " -0.09960484 -0.13096325 -0.14681821 -0.1034075 -0.03104909 0.02851506\n", " 0.08955337 0.0473092 -0.04005323 -0.09391386 -0.18231731 -0.2486911\n", " -0.23296083 -0.16163196 -0.06551701 -0.01223946 -0.00294793 -0.05713513\n", " -0.09766821 -0.15842429 -0.19149529 -0.28554208 -0.3881192 -0.44921689\n", " -0.51665123 -0.5920847 -0.63233076 -0.6463007 -0.60515044 -0.50495606\n", " -0.40408235 -0.34114291 -0.30242893 -0.27671821 -0.24965612 -0.17529294\n", " -0.11588091 -0.0099983 0.0346715 0.06507569 0.0726249 0.06318569\n", " 0.13827268 0.18620243 0.29304884 0.29172423 0.32491785 0.34922817\n", " 0.3499471 0.30923951 0.33703713 0.36986291 0.37389442 0.40242202\n", " 0.36420472 0.32141731 0.23435624 0.17261021 0.14177181 0.08849791\n", " 0.08802865 0.07502712 0.03273347 0.02470467 0.06094804 0.07442878\n", " 0.09438576 0.12220139 0.14663054 0.18559306 0.2373233 0.31841494\n", " 0.3954984 0.38146073 0.31815601 0.23530614 0.16327116 0.10122072\n", " 0.08959236 0.00218855 -0.04991878 -0.11931541 -0.16827824 -0.20825556\n", " -0.27909706 -0.31712507 -0.41086265 -0.45586123 -0.45727768 -0.41683662\n", " -0.3312998 -0.20453536 -0.10251652 0.01370532 0.08119537 0.13717106\n", " 0.15654614 0.11208445 0.04614337 -0.03447503 -0.07907902 -0.12033642\n", " -0.18931062 -0.24672544 -0.32293709 -0.37215495 -0.39881472 -0.37276288\n", " -0.33425137 -0.27855264 -0.27556763 -0.25519566 -0.20283903 -0.16155683\n", " -0.118237 -0.13050738 -0.16296491 -0.16747638 -0.20614067 -0.25770189\n", " -0.25909891 -0.22674276 -0.26381383 -0.2440509 -0.25590711 -0.22570517\n", " -0.19770466 -0.15983697 -0.14697078 -0.10003764 -0.03386954 -0.04347877\n", " -0.05469603 -0.12379429 -0.16004472 -0.22487 -0.2955943 -0.329563\n", " -0.36461738 -0.36340312 -0.3303952 -0.27704948 -0.27589093 -0.2938621\n", " -0.30298402 -0.30966416 -0.29840958 -0.30337485]]\n", "[1. 0.]\n" ] } ] }, { "cell_type": "code", "source": [ "scoreTest, accTest = model.evaluate(x_test[[44],:], y_test[[44],:])\n", "print(round(accTest*100, 2), '%')" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "g_Bj2DOACd-a", "outputId": "66f54491-c6d1-4dfb-f27b-cb4e88941a4f" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1/1 [==============================] - 0s 21ms/step - loss: 1.0610e-05 - accuracy: 1.0000\n", "100.0 %\n" ] } ] }, { "cell_type": "code", "source": [ "print(model.predict(x_test[[44],:]))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "QpsN8THxE20h", "outputId": "bad0a602-06fd-43b5-b90b-e3484258d370" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[[9.9998939e-01 1.0614717e-05]]\n" ] } ] }, { "cell_type": "markdown", "source": [ "***Model interpretation (LIME)***" ], "metadata": { "id": "ji8BKGcGQ3Ag" } }, { "cell_type": "code", "source": [ "explainer = lime_tabular.RecurrentTabularExplainer(training_data = x_train, \n", " feature_names = X_train.columns,\n", " class_names = [0, 1],\n", " mode='classification'\n", " )\n", "\n", "exp = explainer.explain_instance(np.array(X_test.iloc[123]), model.predict)\n", "exp.show_in_notebook(show_table=True)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 331 }, "outputId": "07b10e95-ef13-480f-a29a-082365574328", "id": "WUZvD1smQ3Ag" }, "execution_count": null, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "" ], "text/html": [ "\n", " \n", " \n", "
\n", " \n", " \n", " " ] }, "metadata": {} } ] } ] }