{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a11bab8a-922b-4ce7-b8b8-3c40a45d2a97",
   "metadata": {},
   "source": [
    "# Import Library"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c7b08ce1-1cab-400b-8035-e7266fe04586",
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6b9f0d06-4a2a-4eea-a2de-275643bd9072",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import classification_report,confusion_matrix,accuracy_score, precision_score, recall_score, f1_score\n",
    "from sklearn.metrics import classification_report,confusion_matrix,accuracy_score,precision_recall_fscore_support\n",
    "import lightgbm as lgb\n",
    "import catboost as cbt\n",
    "import xgboost as xgb\n",
    "import time\n",
    "from river import stream\n",
    "from statistics import mode\n",
    "from statistics import mode\n",
    "from sklearn.ensemble import RandomForestClassifier,ExtraTreesClassifier\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "import xgboost as xgb\n",
    "from xgboost import plot_importance\n",
    "from sklearn.linear_model import LogisticRegression, RidgeClassifier\n",
    "from sklearn.svm import SVC\n",
    "from statistics import mode\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split, GridSearchCV, KFold\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.linear_model import LassoCV\n",
    "from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier\n",
    "from sklearn.linear_model import LogisticRegression, RidgeClassifier\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.neural_network import MLPClassifier\n",
    "from xgboost import XGBClassifier\n",
    "from sklearn.metrics import accuracy_score, recall_score, f1_score, roc_curve, auc, log_loss\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import time\n",
    "import joblib\n",
    "from joblib import load\n",
    "import os\n",
    "from sklearn.preprocessing import PowerTransformer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a500ef73-6ed4-4e58-8d03-a67f0800e45b",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(\"./data/CICIOV_2024.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a7566206-5ccf-41d8-b621-25dc04643086",
   "metadata": {},
   "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>ID</th>\n",
       "      <th>DATA_0</th>\n",
       "      <th>DATA_1</th>\n",
       "      <th>DATA_2</th>\n",
       "      <th>DATA_3</th>\n",
       "      <th>DATA_4</th>\n",
       "      <th>DATA_5</th>\n",
       "      <th>DATA_6</th>\n",
       "      <th>DATA_7</th>\n",
       "      <th>Label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>936</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>39</td>\n",
       "      <td>16</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>359</td>\n",
       "      <td>0</td>\n",
       "      <td>128</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>227</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>344</td>\n",
       "      <td>6</td>\n",
       "      <td>20</td>\n",
       "      <td>6</td>\n",
       "      <td>57</td>\n",
       "      <td>6</td>\n",
       "      <td>33</td>\n",
       "      <td>2</td>\n",
       "      <td>17</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>516</td>\n",
       "      <td>192</td>\n",
       "      <td>0</td>\n",
       "      <td>125</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>531</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>8</td>\n",
       "      <td>127</td>\n",
       "      <td>47</td>\n",
       "      <td>7</td>\n",
       "      <td>255</td>\n",
       "      <td>80</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99983</th>\n",
       "      <td>532</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>255</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99984</th>\n",
       "      <td>513</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>64</td>\n",
       "      <td>156</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99985</th>\n",
       "      <td>514</td>\n",
       "      <td>28</td>\n",
       "      <td>239</td>\n",
       "      <td>112</td>\n",
       "      <td>0</td>\n",
       "      <td>64</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99986</th>\n",
       "      <td>128</td>\n",
       "      <td>132</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>35</td>\n",
       "      <td>24</td>\n",
       "      <td>5</td>\n",
       "      <td>138</td>\n",
       "      <td>34</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99987</th>\n",
       "      <td>531</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>8</td>\n",
       "      <td>127</td>\n",
       "      <td>47</td>\n",
       "      <td>7</td>\n",
       "      <td>255</td>\n",
       "      <td>80</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>99988 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        ID  DATA_0  DATA_1  DATA_2  DATA_3  DATA_4  DATA_5  DATA_6  DATA_7  \\\n",
       "0      936       1       0      39      16       0       0       0       0   \n",
       "1      359       0     128       0       0       0       1     227       0   \n",
       "2      344       6      20       6      57       6      33       2      17   \n",
       "3      516     192       0     125       0       0       0       0       0   \n",
       "4      531     255     255       8     127      47       7     255      80   \n",
       "...    ...     ...     ...     ...     ...     ...     ...     ...     ...   \n",
       "99983  532       0       0       0       0       0       0     255       0   \n",
       "99984  513       0       0       0       0       0       0      64     156   \n",
       "99985  514      28     239     112       0      64       0       0       0   \n",
       "99986  128     132       3       2      35      24       5     138      34   \n",
       "99987  531     255     255       8     127      47       7     255      80   \n",
       "\n",
       "       Label  \n",
       "0          0  \n",
       "1          0  \n",
       "2          4  \n",
       "3          0  \n",
       "4          0  \n",
       "...      ...  \n",
       "99983      0  \n",
       "99984      2  \n",
       "99985      0  \n",
       "99986      5  \n",
       "99987      0  \n",
       "\n",
       "[99988 rows x 10 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e46a8255-c352-453e-9417-9c76edf37fb9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Label\n",
       "0    50000\n",
       "1    10000\n",
       "3    10000\n",
       "4     9999\n",
       "5     9998\n",
       "2     9991\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.Label.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "dae95f06-e65e-4e27-a9c5-93967c08169a",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = df.drop(['Label'],axis=1)\n",
    "y = df['Label']\n",
    "X_train, X_test, y_train, y_test = train_test_split(X,y, train_size = 0.8, test_size = 0.2, random_state = 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "24786bdd-8b24-46ca-8c2b-76b77efd70ba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Label\n",
       "0    40026\n",
       "5     8023\n",
       "3     8019\n",
       "2     7980\n",
       "1     7975\n",
       "4     7967\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Series(y_train).value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "783d53b4-5c72-430f-ac36-1e79bd16954b",
   "metadata": {},
   "source": [
    "0 BENIGN \\\n",
    "1 DOS\\\n",
    "2 Spoofing_GAS\\\n",
    "3 Spoofing_RPM\\\n",
    "4 Spoofing_Speed\\\n",
    "5 Spoofing_STEERING_WHEEL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f10573c7-c986-43b9-b1f4-db3e77bb2ddd",
   "metadata": {},
   "outputs": [],
   "source": [
    "from imblearn.over_sampling import SMOTE\n",
    "smote=SMOTE(n_jobs=-1,sampling_strategy={1:10000,2:10000,3:10000,4:10000,5:10000})\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d209155a-1dfb-4d61-8c44-8ff2515e7868",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, y_train = smote.fit_resample(X_train, y_train.astype('int'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "693d9ba1-b782-4502-b59d-3ae25ec1faa2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Label\n",
       "0    40026\n",
       "3    10000\n",
       "1    10000\n",
       "2    10000\n",
       "5    10000\n",
       "4    10000\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Series(y_train).value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b41d2827-bccd-441a-aaa1-74c4ca49ad51",
   "metadata": {},
   "source": [
    "# Model Training"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f387ea0-4c78-45dd-8cd3-f8f635368e2a",
   "metadata": {},
   "source": [
    "## AdaBoost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "80ce2ab4-0319-440e-8462-fb3963eda48b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.67      0.81      0.73      9974\n",
      "           1       0.00      0.00      0.00      2025\n",
      "           2       1.00      1.00      1.00      2011\n",
      "           3       0.34      1.00      0.50      1981\n",
      "           4       0.00      0.00      0.00      2032\n",
      "           5       0.00      0.00      0.00      1975\n",
      "\n",
      "    accuracy                           0.60     19998\n",
      "   macro avg       0.33      0.47      0.37     19998\n",
      "weighted avg       0.47      0.60      0.52     19998\n",
      "\n",
      "Accuracy of AdaBoost: 0.6041104110411041\n",
      "Precision of AdaBoost: 0.4675558168943889\n",
      "Recall of AdaBoost: 0.6041104110411041\n",
      "Average F1 of AdaBoost: 0.5160881398451854\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 31768 characters]",
      "text/plain": [
       "<Figure size 500x500 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 3.48 s\n",
      "Wall time: 3.53 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "from sklearn.ensemble import AdaBoostClassifier\n",
    "from sklearn.metrics import classification_report, accuracy_score, precision_score, recall_score, f1_score\n",
    "from sklearn.metrics import confusion_matrix\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "\n",
    "ad = AdaBoostClassifier()\n",
    "X_train_x = X_train.values\n",
    "X_test_x = X_test.values\n",
    "ad.fit(X_train_x, y_train)\n",
    "y_pred = ad.predict(X_test_x)\n",
    "print(classification_report(y_test, y_pred))\n",
    "print(\"Accuracy of AdaBoost: \" + str(accuracy_score(y_test, y_pred)))\n",
    "print(\"Precision of AdaBoost: \" + str(precision_score(y_test, y_pred, average='weighted')))\n",
    "print(\"Recall of AdaBoost: \" + str(recall_score(y_test, y_pred, average='weighted')))\n",
    "print(\"Average F1 of AdaBoost: \" + str(f1_score(y_test, y_pred, average='weighted')))\n",
    "ad_f1 = f1_score(y_test, y_pred, average=None)\n",
    "cm = confusion_matrix(y_test, y_pred)\n",
    "f, ax = plt.subplots(figsize=(5, 5))\n",
    "sns.heatmap(cm, annot=True, linewidth=0.5, linecolor=\"red\", fmt=\".0f\", ax=ax)\n",
    "plt.xlabel(\"y_pred\")\n",
    "plt.ylabel(\"y_true\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b31334a9-3a9f-4665-8baa-f008346ea98f",
   "metadata": {},
   "source": [
    "## Logistic Regression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d4ea670e-d55b-4638-a7ee-2fb44dd2ab2f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.75      0.94      0.83      9974\n",
      "           1       0.00      0.00      0.00      2025\n",
      "           2       1.00      1.00      1.00      2011\n",
      "           3       0.83      0.26      0.40      1981\n",
      "           4       0.71      1.00      0.83      2032\n",
      "           5       1.00      1.00      1.00      1975\n",
      "\n",
      "    accuracy                           0.80     19998\n",
      "   macro avg       0.71      0.70      0.68     19998\n",
      "weighted avg       0.73      0.80      0.74     19998\n",
      "\n",
      "Accuracy of Logic Regression: 0.7952795279527953\n",
      "Precision of Logic Regression: 0.726701326849849\n",
      "Recall of Logic Regression: 0.7952795279527953\n",
      "Average F1 of Logic Regression: 0.7386205259712273\n",
      "F1 of Logic Regression for each type of attack: [0.83243676 0.         1.         0.40122511 0.83040458 1.        ]\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 29692 characters]",
      "text/plain": [
       "<Figure size 500x500 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 15.5 s\n",
      "Wall time: 2.06 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "lR = LogisticRegression()\n",
    "lR.fit(X_train, y_train)\n",
    "\n",
    "y_pred = lR.predict(X_test)\n",
    "\n",
    "print(classification_report(y_test,y_pred))\n",
    "\n",
    "print(\"Accuracy of Logic Regression: \"+ str(accuracy_score(y_test, y_pred)))\n",
    "print(\"Precision of Logic Regression: \"+ str(precision_score(y_test, y_pred, average='weighted')))\n",
    "print(\"Recall of Logic Regression: \"+ str(recall_score(y_test, y_pred, average='weighted')))\n",
    "print(\"Average F1 of Logic Regression: \"+ str(f1_score(y_test, y_pred, average='weighted')))\n",
    "print(\"F1 of Logic Regression for each type of attack: \"+ str(f1_score(y_test, y_pred, average=None)))\n",
    "\n",
    "lR_f1=f1_score(y_test, y_pred, average=None)\n",
    "\n",
    "# Plot the confusion matrix\n",
    "cm=confusion_matrix(y_test,y_pred)\n",
    "\n",
    "f,ax=plt.subplots(figsize=(5,5))\n",
    "sns.heatmap(cm,annot=True,linewidth=0.5,linecolor=\"red\",fmt=\".0f\",ax=ax)\n",
    "plt.xlabel(\"y_pred\")\n",
    "plt.ylabel(\"y_true\")\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "258e90c6-822a-43ed-bd9c-ce67da977ecb",
   "metadata": {},
   "source": [
    "## Ridge"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a2d13451-fa7e-43de-89b5-3f31b6ac7f72",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.81      0.88      0.84      9974\n",
      "           1       1.00      1.00      1.00      2025\n",
      "           2       0.85      1.00      0.92      2011\n",
      "           3       0.78      0.17      0.28      1981\n",
      "           4       0.83      0.81      0.82      2032\n",
      "           5       0.82      1.00      0.90      1975\n",
      "\n",
      "    accuracy                           0.84     19998\n",
      "   macro avg       0.85      0.81      0.79     19998\n",
      "weighted avg       0.83      0.84      0.81     19998\n",
      "\n",
      "Accuracy of Ridge: 0.8367836783678367\n",
      "Precision of Ridge: 0.8334045105309642\n",
      "Recall of Ridge: 0.8367836783678367\n",
      "Average F1 of Ridge: 0.8138175827964\n",
      "F1 of Ridge for each type of attack: [0.84257741 1.         0.91700866 0.28193468 0.81797864 0.90182648]\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 34444 characters]",
      "text/plain": [
       "<Figure size 500x500 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 2.5 s\n",
      "Wall time: 462 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "rid = RidgeClassifier()\n",
    "\n",
    "rid.fit(X_train, y_train)\n",
    "\n",
    "y_pred = rid.predict(X_test)\n",
    "# \n",
    "print(classification_report(y_test,y_pred))\n",
    "# \n",
    "print(\"Accuracy of Ridge: \"+ str(accuracy_score(y_test, y_pred)))\n",
    "print(\"Precision of Ridge: \"+ str(precision_score(y_test, y_pred, average='weighted')))\n",
    "print(\"Recall of Ridge: \"+ str(recall_score(y_test, y_pred, average='weighted')))\n",
    "print(\"Average F1 of Ridge: \"+ str(f1_score(y_test, y_pred, average='weighted')))\n",
    "print(\"F1 of Ridge for each type of attack: \"+ str(f1_score(y_test, y_pred, average=None)))\n",
    "# \n",
    "rid_f1=f1_score(y_test, y_pred, average=None)\n",
    "\n",
    "# Plot the confusion matrix\n",
    "cm=confusion_matrix(y_test,y_pred)\n",
    "\n",
    "f,ax=plt.subplots(figsize=(5,5))\n",
    "sns.heatmap(cm,annot=True,linewidth=0.5,linecolor=\"red\",fmt=\".0f\",ax=ax)\n",
    "plt.xlabel(\"y_pred\")\n",
    "plt.ylabel(\"y_true\")\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0fc36a94-f2c2-4318-a486-91a65b5f51c7",
   "metadata": {},
   "source": [
    "##  Gaussian Naive Bayes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b777cc01-d0c3-4880-a7fa-d0c5cc334171",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      0.90      0.95      9974\n",
      "           1       1.00      1.00      1.00      2025\n",
      "           2       1.00      1.00      1.00      2011\n",
      "           3       0.64      0.90      0.75      1981\n",
      "           4       0.91      1.00      0.95      2032\n",
      "           5       1.00      1.00      1.00      1975\n",
      "\n",
      "    accuracy                           0.94     19998\n",
      "   macro avg       0.93      0.97      0.94     19998\n",
      "weighted avg       0.96      0.94      0.94     19998\n",
      "\n",
      "Accuracy of Naive Bayes: 0.9396439643964396\n",
      "Precision of Naive Bayes: 0.9552575919436319\n",
      "Recall of Naive Bayes: 0.9396439643964396\n",
      "Average F1 of Naive Bayes: 0.9436696362609218\n",
      "F1 of Naive Bayes for each type of attack: [0.94655682 1.         0.99950298 0.7474367  0.95466291 1.        ]\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 32572 characters]",
      "text/plain": [
       "<Figure size 500x500 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 312 ms\n",
      "Wall time: 311 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "# Train the Naive Bayes algorithm\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "from sklearn.metrics import classification_report, accuracy_score, precision_score, recall_score, f1_score, confusion_matrix\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# \n",
    "nb = GaussianNB()\n",
    "nb.fit(X_train, y_train)\n",
    "\n",
    "# \n",
    "y_pred = nb.predict(X_test)\n",
    "\n",
    "# \n",
    "print(classification_report(y_test, y_pred))\n",
    "\n",
    "# \n",
    "print(\"Accuracy of Naive Bayes: \" + str(accuracy_score(y_test, y_pred)))\n",
    "print(\"Precision of Naive Bayes: \" + str(precision_score(y_test, y_pred, average='weighted')))\n",
    "print(\"Recall of Naive Bayes: \" + str(recall_score(y_test, y_pred, average='weighted')))\n",
    "print(\"Average F1 of Naive Bayes: \" + str(f1_score(y_test, y_pred, average='weighted')))\n",
    "print(\"F1 of Naive Bayes for each type of attack: \" + str(f1_score(y_test, y_pred, average=None)))\n",
    "\n",
    "# \n",
    "nb_f1 = f1_score(y_test, y_pred, average=None)\n",
    "\n",
    "# \n",
    "cm = confusion_matrix(y_test, y_pred)\n",
    "\n",
    "# \n",
    "f, ax = plt.subplots(figsize=(5, 5))\n",
    "sns.heatmap(cm, annot=True, linewidth=0.5, linecolor=\"red\", fmt=\".0f\", ax=ax)\n",
    "plt.xlabel(\"y_pred\")\n",
    "plt.ylabel(\"y_true\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb5def85-3aeb-4067-b65e-6fe83221dea0",
   "metadata": {},
   "source": [
    "## MultinomialNB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "315edf63-c429-4cc8-9ff7-e57a8952c26b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.89      0.43      0.58      9974\n",
      "           1       0.37      1.00      0.54      2025\n",
      "           2       0.84      1.00      0.91      2011\n",
      "           3       0.34      0.36      0.35      1981\n",
      "           4       0.60      0.81      0.69      2032\n",
      "           5       0.82      1.00      0.90      1975\n",
      "\n",
      "    accuracy                           0.64     19998\n",
      "   macro avg       0.64      0.77      0.66     19998\n",
      "weighted avg       0.74      0.64      0.63     19998\n",
      "\n",
      "Accuracy of Multinomial Naive Bayes: 0.6358635863586358\n",
      "Precision of Multinomial Naive Bayes: 0.7418768549955412\n",
      "Recall of Multinomial Naive Bayes: 0.6358635863586358\n",
      "Average F1 of Multinomial Naive Bayes: 0.6320272276820695\n",
      "F1 of Multinomial Naive Bayes for each type of attack: [0.58424242 0.53856383 0.91326067 0.35363458 0.69056604 0.90182648]\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 36880 characters]",
      "text/plain": [
       "<Figure size 500x500 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 1.69 s\n",
      "Wall time: 301 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "# Train the Multinomial Naive Bayes algorithm\n",
    "from sklearn.naive_bayes import MultinomialNB\n",
    "from sklearn.metrics import classification_report, accuracy_score, precision_score, recall_score, f1_score, confusion_matrix\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# \n",
    "mnb = MultinomialNB()\n",
    "mnb.fit(X_train, y_train)\n",
    "\n",
    "# \n",
    "y_pred = mnb.predict(X_test)\n",
    "\n",
    "# \n",
    "print(classification_report(y_test, y_pred))\n",
    "\n",
    "# \n",
    "print(\"Accuracy of Multinomial Naive Bayes: \" + str(accuracy_score(y_test, y_pred)))\n",
    "print(\"Precision of Multinomial Naive Bayes: \" + str(precision_score(y_test, y_pred, average='weighted')))\n",
    "print(\"Recall of Multinomial Naive Bayes: \" + str(recall_score(y_test, y_pred, average='weighted')))\n",
    "print(\"Average F1 of Multinomial Naive Bayes: \" + str(f1_score(y_test, y_pred, average='weighted')))\n",
    "print(\"F1 of Multinomial Naive Bayes for each type of attack: \" + str(f1_score(y_test, y_pred, average=None)))\n",
    "\n",
    "# \n",
    "mnb_f1 = f1_score(y_test, y_pred, average=None)\n",
    "\n",
    "# \n",
    "cm = confusion_matrix(y_test, y_pred)\n",
    "\n",
    "# \n",
    "f, ax = plt.subplots(figsize=(5, 5))\n",
    "sns.heatmap(cm, annot=True, linewidth=0.5, linecolor=\"red\", fmt=\".0f\", ax=ax)\n",
    "plt.xlabel(\"y_pred\")\n",
    "plt.ylabel(\"y_true\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "507ee242-2342-49cf-a9a6-e1cbb75a10ee",
   "metadata": {},
   "source": [
    "## Bernoulli Naive Bayes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "82788f14-257a-4238-85bf-1f5830b9d76e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.95      0.67      0.79      9974\n",
      "           1       1.00      0.26      0.41      2025\n",
      "           2       0.85      1.00      0.92      2011\n",
      "           3       0.56      0.63      0.59      1981\n",
      "           4       0.00      0.00      0.00      2032\n",
      "           5       0.28      1.00      0.44      1975\n",
      "\n",
      "    accuracy                           0.62     19998\n",
      "   macro avg       0.61      0.59      0.52     19998\n",
      "weighted avg       0.74      0.62      0.63     19998\n",
      "\n",
      "Accuracy of Bernoulli Naive Bayes: 0.6227122712271227\n",
      "Precision of Bernoulli Naive Bayes: 0.7418358336684087\n",
      "Recall of Bernoulli Naive Bayes: 0.6227122712271227\n",
      "Average F1 of Bernoulli Naive Bayes: 0.6279242393217781\n",
      "F1 of Bernoulli Naive Bayes for each type of attack: [0.78548822 0.40739284 0.91952446 0.59231134 0.         0.44317289]\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 34488 characters]",
      "text/plain": [
       "<Figure size 500x500 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 1.75 s\n",
      "Wall time: 300 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "# Train the Bernoulli Naive Bayes algorithm\n",
    "from sklearn.naive_bayes import BernoulliNB\n",
    "from sklearn.metrics import classification_report, accuracy_score, precision_score, recall_score, f1_score, confusion_matrix\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# \n",
    "bnb = BernoulliNB()\n",
    "bnb.fit(X_train, y_train)\n",
    "\n",
    "# \n",
    "y_pred = bnb.predict(X_test)\n",
    "\n",
    "# \n",
    "print(classification_report(y_test, y_pred))\n",
    "\n",
    "# \n",
    "print(\"Accuracy of Bernoulli Naive Bayes: \" + str(accuracy_score(y_test, y_pred)))\n",
    "print(\"Precision of Bernoulli Naive Bayes: \" + str(precision_score(y_test, y_pred, average='weighted')))\n",
    "print(\"Recall of Bernoulli Naive Bayes: \" + str(recall_score(y_test, y_pred, average='weighted')))\n",
    "print(\"Average F1 of Bernoulli Naive Bayes: \" + str(f1_score(y_test, y_pred, average='weighted')))\n",
    "print(\"F1 of Bernoulli Naive Bayes for each type of attack: \" + str(f1_score(y_test, y_pred, average=None)))\n",
    "\n",
    "# \n",
    "bnb_f1 = f1_score(y_test, y_pred, average=None)\n",
    "\n",
    "# \n",
    "cm = confusion_matrix(y_test, y_pred)\n",
    "\n",
    "# \n",
    "f, ax = plt.subplots(figsize=(5, 5))\n",
    "sns.heatmap(cm, annot=True, linewidth=0.5, linecolor=\"red\", fmt=\".0f\", ax=ax)\n",
    "plt.xlabel(\"y_pred\")\n",
    "plt.ylabel(\"y_true\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1444a82e-1a34-4698-ad06-4427b4a35562",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.8.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
