{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "CAR_IDS_LOGISTIC_SVM (1).ipynb", "provenance": [], "collapsed_sections": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "id": "lGiyP2dR6Jw-" }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "from sklearn.linear_model import LogisticRegression\n", "from sklearn.ensemble import RandomForestClassifier" ] }, { "cell_type": "code", "source": [ "def changecolumn(dataset, AttackType):\n", " df = pd.read_csv(dataset).sample(frac = 0.1, random_state = 20, replace = False).reset_index(drop=True)\n", " df.columns = [\"Timestamp\", \"CAN ID\", \"Byte\", \"DATA[0]\",\"DATA[1]\",\"DATA[2]\",\"DATA[3]\",\"DATA[4]\",\"DATA[5]\",\"DATA[6]\",\"DATA[7]\",\"AttackType\"]\n", " df['AttackType'] = np.where(df['AttackType'] == 'T',AttackType, 'Normal Message')\n", " df.dropna()\n", " return df\n", "\n", "dfDos = changecolumn('DoS_dataset.csv','DoS Attack')\n", "dfFuzzy = changecolumn('Fuzzy_dataset.csv','Fuzzy Attack')\n", "dfGear = changecolumn('gear_dataset.csv','Gear Spooing Attack')\n", "dfRPM = changecolumn('RPM_dataset.csv','RPM Spoofing Attack')\n", "frames = [dfDos, dfFuzzy, dfGear, dfRPM]\n", "df = pd.concat(frames)\n", "print(df.head(10))\n", "print(df.shape)\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "K4qXdKi-756E", "outputId": "fcf9fee4-8ccb-48a1-99bc-401628828b0f" }, "execution_count": 2, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ " Timestamp CAN ID Byte DATA[0] DATA[1] DATA[2] DATA[3] DATA[4] DATA[5] \\\n", "0 1.478200e+09 0000 8 00 00 00 00 00 00 \n", "1 1.478201e+09 0131 8 1b 80 00 00 3f 7f \n", "2 1.478199e+09 00a1 8 80 89 00 00 24 00 \n", "3 1.478200e+09 0260 8 18 21 22 30 08 8f \n", "4 1.478201e+09 02c0 8 14 00 00 00 00 00 \n", "5 1.478200e+09 0130 8 0b 80 00 ff 08 80 \n", "6 1.478200e+09 0370 8 00 20 00 00 00 00 \n", "7 1.478199e+09 04f0 8 00 00 00 80 00 69 \n", "8 1.478199e+09 0130 8 05 80 00 ff 0b 80 \n", "9 1.478198e+09 0131 8 f7 7f 00 00 4c 7f \n", "\n", " DATA[6] DATA[7] AttackType \n", "0 00 00 DoS Attack \n", "1 0e a6 Normal Message \n", "2 00 00 Normal Message \n", "3 70 05 Normal Message \n", "4 00 00 Normal Message \n", "5 04 88 Normal Message \n", "6 00 00 Normal Message \n", "7 d1 13 Normal Message \n", "8 0c ed Normal Message \n", "9 0d e7 Normal Message \n", "(1656947, 12)\n" ] } ] }, { "cell_type": "code", "source": [ "print(df.dtypes)\n", "df = df.dropna()\n", "def changecolumntype(df):\n", " for column in df[['CAN ID', 'DATA[0]', 'DATA[1]', 'DATA[2]', 'DATA[3]', 'DATA[4]', 'DATA[5]', 'DATA[6]', 'DATA[7]']]:\n", " df[column] = df[column].apply(lambda x: int(str(x), base=16))\n", " return df\n", "\n", "df = changecolumntype(df)\n", "print(df.dtypes)\n", "df.head(10)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 1000 }, "id": "XZTb7XOpJhQw", "outputId": "2755474a-cd7b-4575-cca0-7b7017da8297" }, "execution_count": 3, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Timestamp float64\n", "CAN ID object\n", "Byte int64\n", "DATA[0] object\n", "DATA[1] object\n", "DATA[2] object\n", "DATA[3] object\n", "DATA[4] object\n", "DATA[5] object\n", "DATA[6] object\n", "DATA[7] object\n", "AttackType object\n", "dtype: object\n", "Timestamp float64\n", "CAN ID int64\n", "Byte int64\n", "DATA[0] int64\n", "DATA[1] int64\n", "DATA[2] int64\n", "DATA[3] int64\n", "DATA[4] int64\n", "DATA[5] int64\n", "DATA[6] int64\n", "DATA[7] int64\n", "AttackType object\n", "dtype: object\n" ] }, { "output_type": "execute_result", "data": { "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", "
TimestampCAN IDByteDATA[0]DATA[1]DATA[2]DATA[3]DATA[4]DATA[5]DATA[6]DATA[7]AttackType
01.478200e+090800000000DoS Attack
11.478201e+09305827128006312714166Normal Message
21.478199e+0916181281370036000Normal Message
31.478200e+0960882433344881431125Normal Message
41.478201e+097048200000000Normal Message
51.478200e+09304811128025581284136Normal Message
61.478200e+098808032000000Normal Message
71.478199e+0912648000128010520919Normal Message
81.478199e+093048512802551112812237Normal Message
91.478198e+093058247127007612713231Normal Message
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ], "text/plain": [ " Timestamp CAN ID Byte DATA[0] DATA[1] DATA[2] DATA[3] DATA[4] \\\n", "0 1.478200e+09 0 8 0 0 0 0 0 \n", "1 1.478201e+09 305 8 27 128 0 0 63 \n", "2 1.478199e+09 161 8 128 137 0 0 36 \n", "3 1.478200e+09 608 8 24 33 34 48 8 \n", "4 1.478201e+09 704 8 20 0 0 0 0 \n", "5 1.478200e+09 304 8 11 128 0 255 8 \n", "6 1.478200e+09 880 8 0 32 0 0 0 \n", "7 1.478199e+09 1264 8 0 0 0 128 0 \n", "8 1.478199e+09 304 8 5 128 0 255 11 \n", "9 1.478198e+09 305 8 247 127 0 0 76 \n", "\n", " DATA[5] DATA[6] DATA[7] AttackType \n", "0 0 0 0 DoS Attack \n", "1 127 14 166 Normal Message \n", "2 0 0 0 Normal Message \n", "3 143 112 5 Normal Message \n", "4 0 0 0 Normal Message \n", "5 128 4 136 Normal Message \n", "6 0 0 0 Normal Message \n", "7 105 209 19 Normal Message \n", "8 128 12 237 Normal Message \n", "9 127 13 231 Normal Message " ] }, "metadata": {}, "execution_count": 3 } ] }, { "cell_type": "code", "source": [ "df['Message'] = df.iloc[:,3:11].apply(lambda x: ''.join(x.astype(str)), axis = 1)\n", "df.head(10)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 601 }, "id": "ym4-oGjemqFD", "outputId": "2dc335e5-788b-41ff-d48c-1a2602f67391" }, "execution_count": 4, "outputs": [ { "output_type": "execute_result", "data": { "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", "
TimestampCAN IDByteDATA[0]DATA[1]DATA[2]DATA[3]DATA[4]DATA[5]DATA[6]DATA[7]AttackTypeMessage
01.478200e+090800000000DoS Attack00000000
11.478201e+09305827128006312714166Normal Message27128006312714166
21.478199e+0916181281370036000Normal Message1281370036000
31.478200e+0960882433344881431125Normal Message2433344881431125
41.478201e+097048200000000Normal Message200000000
51.478200e+09304811128025581284136Normal Message11128025581284136
61.478200e+098808032000000Normal Message032000000
71.478199e+0912648000128010520919Normal Message000128010520919
81.478199e+093048512802551112812237Normal Message512802551112812237
91.478198e+093058247127007612713231Normal Message247127007612713231
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ], "text/plain": [ " Timestamp CAN ID Byte DATA[0] DATA[1] DATA[2] DATA[3] DATA[4] \\\n", "0 1.478200e+09 0 8 0 0 0 0 0 \n", "1 1.478201e+09 305 8 27 128 0 0 63 \n", "2 1.478199e+09 161 8 128 137 0 0 36 \n", "3 1.478200e+09 608 8 24 33 34 48 8 \n", "4 1.478201e+09 704 8 20 0 0 0 0 \n", "5 1.478200e+09 304 8 11 128 0 255 8 \n", "6 1.478200e+09 880 8 0 32 0 0 0 \n", "7 1.478199e+09 1264 8 0 0 0 128 0 \n", "8 1.478199e+09 304 8 5 128 0 255 11 \n", "9 1.478198e+09 305 8 247 127 0 0 76 \n", "\n", " DATA[5] DATA[6] DATA[7] AttackType Message \n", "0 0 0 0 DoS Attack 00000000 \n", "1 127 14 166 Normal Message 27128006312714166 \n", "2 0 0 0 Normal Message 1281370036000 \n", "3 143 112 5 Normal Message 2433344881431125 \n", "4 0 0 0 Normal Message 200000000 \n", "5 128 4 136 Normal Message 11128025581284136 \n", "6 0 0 0 Normal Message 032000000 \n", "7 105 209 19 Normal Message 000128010520919 \n", "8 128 12 237 Normal Message 512802551112812237 \n", "9 127 13 231 Normal Message 247127007612713231 " ] }, "metadata": {}, "execution_count": 4 } ] }, { "cell_type": "code", "source": [ "#df['Message'] = df['Message'].map(lambda x: int(x))\n", "df['Message'] = df['Message'].astype(float)\n", "df.head(10)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 601 }, "id": "fUPHSmKPAP6_", "outputId": "4d196f89-62c4-4c91-eb66-cb3a8172af9f" }, "execution_count": 5, "outputs": [ { "output_type": "execute_result", "data": { "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", "
TimestampCAN IDByteDATA[0]DATA[1]DATA[2]DATA[3]DATA[4]DATA[5]DATA[6]DATA[7]AttackTypeMessage
01.478200e+090800000000DoS Attack0.000000e+00
11.478201e+09305827128006312714166Normal Message2.712801e+16
21.478199e+0916181281370036000Normal Message1.281370e+12
31.478200e+0960882433344881431125Normal Message2.433345e+15
41.478201e+097048200000000Normal Message2.000000e+08
51.478200e+09304811128025581284136Normal Message1.112803e+16
61.478200e+098808032000000Normal Message3.200000e+07
71.478199e+0912648000128010520919Normal Message1.280105e+11
81.478199e+093048512802551112812237Normal Message5.128026e+17
91.478198e+093058247127007612713231Normal Message2.471270e+17
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ], "text/plain": [ " Timestamp CAN ID Byte DATA[0] DATA[1] DATA[2] DATA[3] DATA[4] \\\n", "0 1.478200e+09 0 8 0 0 0 0 0 \n", "1 1.478201e+09 305 8 27 128 0 0 63 \n", "2 1.478199e+09 161 8 128 137 0 0 36 \n", "3 1.478200e+09 608 8 24 33 34 48 8 \n", "4 1.478201e+09 704 8 20 0 0 0 0 \n", "5 1.478200e+09 304 8 11 128 0 255 8 \n", "6 1.478200e+09 880 8 0 32 0 0 0 \n", "7 1.478199e+09 1264 8 0 0 0 128 0 \n", "8 1.478199e+09 304 8 5 128 0 255 11 \n", "9 1.478198e+09 305 8 247 127 0 0 76 \n", "\n", " DATA[5] DATA[6] DATA[7] AttackType Message \n", "0 0 0 0 DoS Attack 0.000000e+00 \n", "1 127 14 166 Normal Message 2.712801e+16 \n", "2 0 0 0 Normal Message 1.281370e+12 \n", "3 143 112 5 Normal Message 2.433345e+15 \n", "4 0 0 0 Normal Message 2.000000e+08 \n", "5 128 4 136 Normal Message 1.112803e+16 \n", "6 0 0 0 Normal Message 3.200000e+07 \n", "7 105 209 19 Normal Message 1.280105e+11 \n", "8 128 12 237 Normal Message 5.128026e+17 \n", "9 127 13 231 Normal Message 2.471270e+17 " ] }, "metadata": {}, "execution_count": 5 } ] }, { "cell_type": "code", "source": [ "import datetime\n", "newdf = df.copy(deep = True)\n", "dateformat = \"%Y-%m-%d %H:%M:%S.%f\"\n", "df['Timestamp'] = df['Timestamp'].apply(lambda x: datetime.datetime.fromtimestamp(float(x)).strftime(dateformat))\n", "print(df.dtypes)\n", "df.head(100)" ], "metadata": { "id": "DMt9KCx9ql_W", "colab": { "base_uri": "https://localhost:8080/", "height": 921 }, "outputId": "68acd251-e138-489f-8e43-ae6b6632c6af" }, "execution_count": 6, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Timestamp object\n", "CAN ID int64\n", "Byte int64\n", "DATA[0] int64\n", "DATA[1] int64\n", "DATA[2] int64\n", "DATA[3] int64\n", "DATA[4] int64\n", "DATA[5] int64\n", "DATA[6] int64\n", "DATA[7] int64\n", "AttackType object\n", "Message float64\n", "dtype: object\n" ] }, { "output_type": "execute_result", "data": { "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", "
TimestampCAN IDByteDATA[0]DATA[1]DATA[2]DATA[3]DATA[4]DATA[5]DATA[6]DATA[7]AttackTypeMessage
02016-11-03 19:08:43.0441570800000000DoS Attack0.000000e+00
12016-11-03 19:24:35.989254305827128006312714166Normal Message2.712801e+16
22016-11-03 18:54:13.78868116181281370036000Normal Message1.281370e+12
32016-11-03 19:06:50.28611960882433344881431125Normal Message2.433345e+15
42016-11-03 19:26:04.1397147048200000000Normal Message2.000000e+08
..........................................
952016-11-03 19:05:13.3464160800000000DoS Attack0.000000e+00
972016-11-03 19:15:01.1463057048200000000Normal Message2.000000e+08
982016-11-03 18:56:54.7611378098220190127201732020Normal Message2.201901e+17
992016-11-03 18:52:14.511839497880000000Normal Message8.000000e+07
1002016-11-03 19:16:38.7902567048200000000Normal Message2.000000e+08
\n", "

100 rows × 13 columns

\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ], "text/plain": [ " Timestamp CAN ID Byte DATA[0] DATA[1] DATA[2] \\\n", "0 2016-11-03 19:08:43.044157 0 8 0 0 0 \n", "1 2016-11-03 19:24:35.989254 305 8 27 128 0 \n", "2 2016-11-03 18:54:13.788681 161 8 128 137 0 \n", "3 2016-11-03 19:06:50.286119 608 8 24 33 34 \n", "4 2016-11-03 19:26:04.139714 704 8 20 0 0 \n", ".. ... ... ... ... ... ... \n", "95 2016-11-03 19:05:13.346416 0 8 0 0 0 \n", "97 2016-11-03 19:15:01.146305 704 8 20 0 0 \n", "98 2016-11-03 18:56:54.761137 809 8 220 190 127 \n", "99 2016-11-03 18:52:14.511839 497 8 8 0 0 \n", "100 2016-11-03 19:16:38.790256 704 8 20 0 0 \n", "\n", " DATA[3] DATA[4] DATA[5] DATA[6] DATA[7] AttackType Message \n", "0 0 0 0 0 0 DoS Attack 0.000000e+00 \n", "1 0 63 127 14 166 Normal Message 2.712801e+16 \n", "2 0 36 0 0 0 Normal Message 1.281370e+12 \n", "3 48 8 143 112 5 Normal Message 2.433345e+15 \n", "4 0 0 0 0 0 Normal Message 2.000000e+08 \n", ".. ... ... ... ... ... ... ... \n", "95 0 0 0 0 0 DoS Attack 0.000000e+00 \n", "97 0 0 0 0 0 Normal Message 2.000000e+08 \n", "98 20 17 32 0 20 Normal Message 2.201901e+17 \n", "99 0 0 0 0 0 Normal Message 8.000000e+07 \n", "100 0 0 0 0 0 Normal Message 2.000000e+08 \n", "\n", "[100 rows x 13 columns]" ] }, "metadata": {}, "execution_count": 6 } ] }, { "cell_type": "code", "source": [ "#df = newdf.copy(deep = True)\n", "from sklearn import preprocessing\n", "#print(df['AttackType'].unique())\n", "#print(df['AttackType'].value_counts())\n", "encoder = preprocessing.LabelEncoder()\n", "#df1 = df[['AttackType']].copy()\n", "df['AttackType']= encoder.fit_transform(df['AttackType'].values)\n", "# df = df.drop(['AttackType'], axis = 1)\n", "# df1\n", "#df = pd.concat([df.iloc[:,0:11],df1, df.iloc[:, 11:]], axis=1)\n", "#df = pd.get_dummies(df, columns =['AttackType'], prefix = '', prefix_sep = '')\n", "print(df.head(10))\n", "# print(df['AttackType Encode'])\n", "print(df['AttackType'])\n", "print(df.shape)\n", "#print(df.shape)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "vEUXBabOBtpQ", "outputId": "076885d8-3a39-446d-96b6-0816f653f48a" }, "execution_count": 7, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ " Timestamp CAN ID Byte DATA[0] DATA[1] DATA[2] \\\n", "0 2016-11-03 19:08:43.044157 0 8 0 0 0 \n", "1 2016-11-03 19:24:35.989254 305 8 27 128 0 \n", "2 2016-11-03 18:54:13.788681 161 8 128 137 0 \n", "3 2016-11-03 19:06:50.286119 608 8 24 33 34 \n", "4 2016-11-03 19:26:04.139714 704 8 20 0 0 \n", "5 2016-11-03 19:03:07.624543 304 8 11 128 0 \n", "6 2016-11-03 19:06:31.658461 880 8 0 32 0 \n", "7 2016-11-03 18:55:47.812754 1264 8 0 0 0 \n", "8 2016-11-03 18:46:48.226079 304 8 5 128 0 \n", "9 2016-11-03 18:40:52.891089 305 8 247 127 0 \n", "\n", " DATA[3] DATA[4] DATA[5] DATA[6] DATA[7] AttackType Message \n", "0 0 0 0 0 0 0 0.000000e+00 \n", "1 0 63 127 14 166 3 2.712801e+16 \n", "2 0 36 0 0 0 3 1.281370e+12 \n", "3 48 8 143 112 5 3 2.433345e+15 \n", "4 0 0 0 0 0 3 2.000000e+08 \n", "5 255 8 128 4 136 3 1.112803e+16 \n", "6 0 0 0 0 0 3 3.200000e+07 \n", "7 128 0 105 209 19 3 1.280105e+11 \n", "8 255 11 128 12 237 3 5.128026e+17 \n", "9 0 76 127 13 231 3 2.471270e+17 \n", "0 0\n", "1 3\n", "2 3\n", "3 3\n", "4 3\n", " ..\n", "462165 3\n", "462166 3\n", "462167 3\n", "462168 3\n", "462169 4\n", "Name: AttackType, Length: 1636855, dtype: int64\n", "(1636855, 13)\n" ] } ] }, { "cell_type": "code", "source": [ "df.columns" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "jPQUX0V2PPkm", "outputId": "45225c4a-9642-444e-bb72-1ff1c5f3e0cc" }, "execution_count": 8, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "Index(['Timestamp', 'CAN ID', 'Byte', 'DATA[0]', 'DATA[1]', 'DATA[2]',\n", " 'DATA[3]', 'DATA[4]', 'DATA[5]', 'DATA[6]', 'DATA[7]', 'AttackType',\n", " 'Message'],\n", " dtype='object')" ] }, "metadata": {}, "execution_count": 8 } ] }, { "cell_type": "code", "source": [ "X = df.iloc[:, np.r_[:,1,3:11]]\n", "#X = df[['CAN ID', 'DATA[0]', 'DATA[1]', 'DATA[2]', 'DATA[3]', 'DATA[4]', 'DATA[5]', 'DATA[6]', 'DATA[7]']]\n", "Y = df[['AttackType']]\n", "X,Y" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "-E_zgBKtbX4C", "outputId": "fe0d954e-9b94-46d9-f3a1-3e8503d67c3d" }, "execution_count": 9, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "( CAN ID DATA[0] DATA[1] DATA[2] DATA[3] DATA[4] DATA[5] DATA[6] \\\n", " 0 0 0 0 0 0 0 0 0 \n", " 1 305 27 128 0 0 63 127 14 \n", " 2 161 128 137 0 0 36 0 0 \n", " 3 608 24 33 34 48 8 143 112 \n", " 4 704 20 0 0 0 0 0 0 \n", " ... ... ... ... ... ... ... ... ... \n", " 462165 809 220 183 126 20 17 32 0 \n", " 462166 305 242 127 0 0 58 127 12 \n", " 462167 305 242 127 0 0 64 127 6 \n", " 462168 704 21 0 0 0 0 0 0 \n", " 462169 790 69 41 36 255 41 36 0 \n", " \n", " DATA[7] \n", " 0 0 \n", " 1 166 \n", " 2 0 \n", " 3 5 \n", " 4 0 \n", " ... ... \n", " 462165 20 \n", " 462166 131 \n", " 462167 22 \n", " 462168 0 \n", " 462169 255 \n", " \n", " [1636855 rows x 9 columns], AttackType\n", " 0 0\n", " 1 3\n", " 2 3\n", " 3 3\n", " 4 3\n", " ... ...\n", " 462165 3\n", " 462166 3\n", " 462167 3\n", " 462168 3\n", " 462169 4\n", " \n", " [1636855 rows x 1 columns])" ] }, "metadata": {}, "execution_count": 9 } ] }, { "cell_type": "code", "source": [ "from sklearn.model_selection import train_test_split\n", "from sklearn.svm import SVC\n", "from sklearn.pipeline import Pipeline\n", "X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.25, random_state = 20)" ], "metadata": { "id": "7_qOxOWjbeX8" }, "execution_count": 10, "outputs": [] }, { "cell_type": "code", "source": [ "val = encoder.inverse_transform(df['AttackType'])\n", "(unique, counts) = np.unique(val, return_counts=True)\n", "frequencies = np.asarray((unique, counts)).T\n", "print(frequencies)\n", "print(df['AttackType'].value_counts())" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "oUcMhUpnh4Tq", "outputId": "6fe79717-8b48-42ea-b5a8-11fe00f2d568" }, "execution_count": 23, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[['DoS Attack' 58469]\n", " ['Fuzzy Attack' 49258]\n", " ['Gear Spooing Attack' 60016]\n", " ['Normal Message' 1403673]\n", " ['RPM Spoofing Attack' 65439]]\n", "3 1403673\n", "4 65439\n", "2 60016\n", "0 58469\n", "1 49258\n", "Name: AttackType, dtype: int64\n" ] } ] }, { "cell_type": "code", "source": [ "model = Pipeline([\n", " ('svc', SVC(random_state=20))\n", " ])\n", "model.fit(X_train, Y_train)\n", "pred = model.predict(X_test)\n", "pred" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "NkzbFP_GbjSe", "outputId": "bf0d413b-a09f-49d3-82fb-87ce53b04aae" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "/usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py:993: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n", " y = column_or_1d(y, warn=True)\n" ] } ] }, { "cell_type": "code", "source": [ "from sklearn.metrics import confusion_matrix\n", "from sklearn.metrics import classification_report\n", "from sklearn.metrics import accuracy_score\n", "accuracy = accuracy_score(Y_test, pred)\n", "print('accuracy : \\n', accuracy)\n", "matrix = confusion_matrix(Y_test,pred)\n", "print('Confusion matrix : \\n',matrix)\n", "matrix = classification_report(Y_test,pred)\n", "print('Classification report : \\n',matrix)" ], "metadata": { "id": "zCFivudpci-x" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "dfC = df['AttackType'].value_counts()\n", "dfcount = pd.DataFrame(dfC, index = ['Normal Message','RPM Spoofing Attack','Gear Spooing Attack','DoS Attack','Fuzzy Attack'])\n", "#dfcount_reset = dfcount.reset_index()\n", "dfcount.columns = ['Injected Messages']\n", "#dfcount_reset.set_index('Attack Type')\n", "#dfcount_reset.dropna()\n", "print(\"\\n\",dfcount)\n", "\n", "#index = ['Normal Message','RPM Spoofing Attack','Gear Spooing Attack','DoS Attack','Fuzzy Attack']\n", "# df2frame = dfDos['AttackType'].value_counts()\n", "# df2frame_count = pd.DataFrame(df2frame)\n", "# df2frame_count_reset = df2frame_count.reset_index()\n", "# df2frame_count_reset.columns = ['No Of Normal Message','No Of Injected Messages']\n", "# print(\"\\n\",df2frame_count_reset)" ], "metadata": { "id": "z-aD9lxNQ2hX" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import matplotlib.pyplot as plt\n", "# dffinal = pd.DataFrame({'mass': [0.330, 4.87 , 5.97],\n", "# 'radius': [2439.7, 6051.8, 6378.1]},\n", "# index=['Mercury', 'Venus', 'Earth'])\n", "#plts = dfcount.plot.bar(x='Attack Type', y='Injected Messages', rot=0, figsize=(12, 8))\n", "plot = dfcount.plot.pie(y='Injected Messages', figsize=(12, 8))" ], "metadata": { "id": "7Am7edMcou4Y" }, "execution_count": null, "outputs": [] } ] }