{
 "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={2:4000,3:4000,4:4000})\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.00      0.00      0.00      1981\n",
      "           4       0.34      1.00      0.51      2032\n",
      "           5       0.00      0.00      0.00      1975\n",
      "\n",
      "    accuracy                           0.61     19998\n",
      "   macro avg       0.34      0.47      0.37     19998\n",
      "weighted avg       0.47      0.61      0.52     19998\n",
      "\n",
      "Accuracy of AdaBoost: 0.6066606660666066\n",
      "Precision of AdaBoost: 0.46929101075965535\n",
      "Recall of AdaBoost: 0.6066606660666066\n",
      "Average F1 of AdaBoost: 0.5183489080818232\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 31648 characters]",
      "text/plain": [
       "<Figure size 500x500 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 3.41 s\n",
      "Wall time: 3.4 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": "6bc21949-ae59-406e-93f6-c9acb6e2c4d4",
   "metadata": {},
   "source": [
    "## MLP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "178c44a7-c3d2-4ed2-9621-1c566f459aeb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00      9974\n",
      "           1       1.00      1.00      1.00      2025\n",
      "           2       1.00      1.00      1.00      2011\n",
      "           3       1.00      1.00      1.00      1981\n",
      "           4       1.00      1.00      1.00      2032\n",
      "           5       1.00      1.00      1.00      1975\n",
      "\n",
      "    accuracy                           1.00     19998\n",
      "   macro avg       1.00      1.00      1.00     19998\n",
      "weighted avg       1.00      1.00      1.00     19998\n",
      "\n",
      "Accuracy of MLP: 0.9998999899989999\n",
      "Precision of MLP: 0.9999000100491203\n",
      "Recall of MLP: 0.9998999899989999\n",
      "Average F1 of MLP: 0.9998999697569818\n",
      "F1 of MLP for each type of attack: [0.99989975 1.         1.         0.99949495 1.         1.        ]\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 26112 characters]",
      "text/plain": [
       "<Figure size 500x500 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 38.1 s\n",
      "Wall time: 4.85 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "mlp = MLPClassifier(max_iter=200, early_stopping=True)\n",
    "\n",
    "mlp.fit(X_train, y_train)\n",
    "\n",
    "y_pred = mlp.predict(X_test)\n",
    "print(classification_report(y_test,y_pred))\n",
    "print(\"Accuracy of MLP: \"+ str(accuracy_score(y_test, y_pred)))\n",
    "print(\"Precision of MLP: \"+ str(precision_score(y_test, y_pred, average='weighted')))\n",
    "print(\"Recall of MLP: \"+ str(recall_score(y_test, y_pred, average='weighted')))\n",
    "print(\"Average F1 of MLP: \"+ str(f1_score(y_test, y_pred, average='weighted')))\n",
    "print(\"F1 of MLP for each type of attack: \"+ str(f1_score(y_test, y_pred, average=None)))\n",
    "mlp_f1=f1_score(y_test, y_pred, average=None)\n",
    "\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": "b31334a9-3a9f-4665-8baa-f008346ea98f",
   "metadata": {},
   "source": [
    "## Logic Regression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "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.7951295129512951\n",
      "Precision of Logic Regression: 0.7262819270141483\n",
      "Recall of Logic Regression: 0.7951295129512951\n",
      "Average F1 of Logic Regression: 0.7384972515838806\n",
      "F1 of Logic Regression for each type of attack: [0.83228101 0.         1.         0.40076482 0.83040458 1.        ]\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 29612 characters]",
      "text/plain": [
       "<Figure size 500x500 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 14.2 s\n",
      "Wall time: 1.83 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "lR = LogisticRegression()\n",
    "lR.fit(X_train, y_train)\n",
    "\n",
    "y_pred = lR.predict(X_test)\n",
    "# 使用训练好的模型对测试数据集X_test进行预测，得到预测结果y_pred。\n",
    "print(classification_report(y_test,y_pred))\n",
    "# 打印分类报告，其中包括每个类别的精确率、召回率、F1分数和支持度。\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",
    "# 计算并保存F1分数\n",
    "# Plot the confusion matrix\n",
    "cm=confusion_matrix(y_test,y_pred)\n",
    "# 计算真实标签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()\n",
    "# 绘制混淆矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "258e90c6-822a-43ed-bd9c-ce67da977ecb",
   "metadata": {},
   "source": [
    "## Ridge"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "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: 1.69 s\n",
      "Wall time: 276 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",
    "# 使用训练好的模型对测试数据集X_test进行预测，得到预测结果y_pred。\n",
    "print(classification_report(y_test,y_pred))\n",
    "# 打印分类报告，其中包括每个类别的精确率、召回率、F1分数和支持度。\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",
    "# 计算并保存F1分数\n",
    "# Plot the confusion matrix\n",
    "cm=confusion_matrix(y_test,y_pred)\n",
    "# 计算真实标签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()\n",
    "# 绘制混淆矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b777cc01-d0c3-4880-a7fa-d0c5cc334171",
   "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
}
