You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1790 lines
76 KiB
Plaintext

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{
"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",
" <div id=\"df-07fb84f4-1b56-4725-8c79-156e67941e09\">\n",
" <div class=\"colab-df-container\">\n",
" <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>Timestamp</th>\n",
" <th>CAN ID</th>\n",
" <th>Byte</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>AttackType</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1.478200e+09</td>\n",
" <td>0</td>\n",
" <td>8</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>0</td>\n",
" <td>0</td>\n",
" <td>DoS Attack</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1.478201e+09</td>\n",
" <td>305</td>\n",
" <td>8</td>\n",
" <td>27</td>\n",
" <td>128</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>63</td>\n",
" <td>127</td>\n",
" <td>14</td>\n",
" <td>166</td>\n",
" <td>Normal Message</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1.478199e+09</td>\n",
" <td>161</td>\n",
" <td>8</td>\n",
" <td>128</td>\n",
" <td>137</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>36</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>Normal Message</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1.478200e+09</td>\n",
" <td>608</td>\n",
" <td>8</td>\n",
" <td>24</td>\n",
" <td>33</td>\n",
" <td>34</td>\n",
" <td>48</td>\n",
" <td>8</td>\n",
" <td>143</td>\n",
" <td>112</td>\n",
" <td>5</td>\n",
" <td>Normal Message</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1.478201e+09</td>\n",
" <td>704</td>\n",
" <td>8</td>\n",
" <td>20</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>0</td>\n",
" <td>Normal Message</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>1.478200e+09</td>\n",
" <td>304</td>\n",
" <td>8</td>\n",
" <td>11</td>\n",
" <td>128</td>\n",
" <td>0</td>\n",
" <td>255</td>\n",
" <td>8</td>\n",
" <td>128</td>\n",
" <td>4</td>\n",
" <td>136</td>\n",
" <td>Normal Message</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>1.478200e+09</td>\n",
" <td>880</td>\n",
" <td>8</td>\n",
" <td>0</td>\n",
" <td>32</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>Normal Message</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>1.478199e+09</td>\n",
" <td>1264</td>\n",
" <td>8</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>128</td>\n",
" <td>0</td>\n",
" <td>105</td>\n",
" <td>209</td>\n",
" <td>19</td>\n",
" <td>Normal Message</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>1.478199e+09</td>\n",
" <td>304</td>\n",
" <td>8</td>\n",
" <td>5</td>\n",
" <td>128</td>\n",
" <td>0</td>\n",
" <td>255</td>\n",
" <td>11</td>\n",
" <td>128</td>\n",
" <td>12</td>\n",
" <td>237</td>\n",
" <td>Normal Message</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>1.478198e+09</td>\n",
" <td>305</td>\n",
" <td>8</td>\n",
" <td>247</td>\n",
" <td>127</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>76</td>\n",
" <td>127</td>\n",
" <td>13</td>\n",
" <td>231</td>\n",
" <td>Normal Message</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>\n",
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-07fb84f4-1b56-4725-8c79-156e67941e09')\"\n",
" title=\"Convert this dataframe to an interactive table.\"\n",
" style=\"display:none;\">\n",
" \n",
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
" width=\"24px\">\n",
" <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
" <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
" </svg>\n",
" </button>\n",
" \n",
" <style>\n",
" .colab-df-container {\n",
" display:flex;\n",
" flex-wrap:wrap;\n",
" gap: 12px;\n",
" }\n",
"\n",
" .colab-df-convert {\n",
" background-color: #E8F0FE;\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: #1967D2;\n",
" height: 32px;\n",
" padding: 0 0 0 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-convert:hover {\n",
" background-color: #E2EBFA;\n",
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: #174EA6;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert {\n",
" background-color: #3B4455;\n",
" fill: #D2E3FC;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert:hover {\n",
" background-color: #434B5C;\n",
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
" fill: #FFFFFF;\n",
" }\n",
" </style>\n",
"\n",
" <script>\n",
" const buttonEl =\n",
" document.querySelector('#df-07fb84f4-1b56-4725-8c79-156e67941e09 button.colab-df-convert');\n",
" buttonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
"\n",
" async function convertToInteractive(key) {\n",
" const element = document.querySelector('#df-07fb84f4-1b56-4725-8c79-156e67941e09');\n",
" const dataTable =\n",
" await google.colab.kernel.invokeFunction('convertToInteractive',\n",
" [key], {});\n",
" if (!dataTable) return;\n",
"\n",
" const docLinkHtml = 'Like what you see? Visit the ' +\n",
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
" + ' to learn more about interactive tables.';\n",
" element.innerHTML = '';\n",
" dataTable['output_type'] = 'display_data';\n",
" await google.colab.output.renderOutput(dataTable, element);\n",
" const docLink = document.createElement('div');\n",
" docLink.innerHTML = docLinkHtml;\n",
" element.appendChild(docLink);\n",
" }\n",
" </script>\n",
" </div>\n",
" </div>\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",
" <div id=\"df-52fd1560-0d33-48d5-9d39-54f9e4f8cfc2\">\n",
" <div class=\"colab-df-container\">\n",
" <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>Timestamp</th>\n",
" <th>CAN ID</th>\n",
" <th>Byte</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>AttackType</th>\n",
" <th>Message</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1.478200e+09</td>\n",
" <td>0</td>\n",
" <td>8</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>0</td>\n",
" <td>0</td>\n",
" <td>DoS Attack</td>\n",
" <td>00000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1.478201e+09</td>\n",
" <td>305</td>\n",
" <td>8</td>\n",
" <td>27</td>\n",
" <td>128</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>63</td>\n",
" <td>127</td>\n",
" <td>14</td>\n",
" <td>166</td>\n",
" <td>Normal Message</td>\n",
" <td>27128006312714166</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1.478199e+09</td>\n",
" <td>161</td>\n",
" <td>8</td>\n",
" <td>128</td>\n",
" <td>137</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>36</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>Normal Message</td>\n",
" <td>1281370036000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1.478200e+09</td>\n",
" <td>608</td>\n",
" <td>8</td>\n",
" <td>24</td>\n",
" <td>33</td>\n",
" <td>34</td>\n",
" <td>48</td>\n",
" <td>8</td>\n",
" <td>143</td>\n",
" <td>112</td>\n",
" <td>5</td>\n",
" <td>Normal Message</td>\n",
" <td>2433344881431125</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1.478201e+09</td>\n",
" <td>704</td>\n",
" <td>8</td>\n",
" <td>20</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>0</td>\n",
" <td>Normal Message</td>\n",
" <td>200000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>1.478200e+09</td>\n",
" <td>304</td>\n",
" <td>8</td>\n",
" <td>11</td>\n",
" <td>128</td>\n",
" <td>0</td>\n",
" <td>255</td>\n",
" <td>8</td>\n",
" <td>128</td>\n",
" <td>4</td>\n",
" <td>136</td>\n",
" <td>Normal Message</td>\n",
" <td>11128025581284136</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>1.478200e+09</td>\n",
" <td>880</td>\n",
" <td>8</td>\n",
" <td>0</td>\n",
" <td>32</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>Normal Message</td>\n",
" <td>032000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>1.478199e+09</td>\n",
" <td>1264</td>\n",
" <td>8</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>128</td>\n",
" <td>0</td>\n",
" <td>105</td>\n",
" <td>209</td>\n",
" <td>19</td>\n",
" <td>Normal Message</td>\n",
" <td>000128010520919</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>1.478199e+09</td>\n",
" <td>304</td>\n",
" <td>8</td>\n",
" <td>5</td>\n",
" <td>128</td>\n",
" <td>0</td>\n",
" <td>255</td>\n",
" <td>11</td>\n",
" <td>128</td>\n",
" <td>12</td>\n",
" <td>237</td>\n",
" <td>Normal Message</td>\n",
" <td>512802551112812237</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>1.478198e+09</td>\n",
" <td>305</td>\n",
" <td>8</td>\n",
" <td>247</td>\n",
" <td>127</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>76</td>\n",
" <td>127</td>\n",
" <td>13</td>\n",
" <td>231</td>\n",
" <td>Normal Message</td>\n",
" <td>247127007612713231</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>\n",
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-52fd1560-0d33-48d5-9d39-54f9e4f8cfc2')\"\n",
" title=\"Convert this dataframe to an interactive table.\"\n",
" style=\"display:none;\">\n",
" \n",
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
" width=\"24px\">\n",
" <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
" <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
" </svg>\n",
" </button>\n",
" \n",
" <style>\n",
" .colab-df-container {\n",
" display:flex;\n",
" flex-wrap:wrap;\n",
" gap: 12px;\n",
" }\n",
"\n",
" .colab-df-convert {\n",
" background-color: #E8F0FE;\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: #1967D2;\n",
" height: 32px;\n",
" padding: 0 0 0 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-convert:hover {\n",
" background-color: #E2EBFA;\n",
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: #174EA6;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert {\n",
" background-color: #3B4455;\n",
" fill: #D2E3FC;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert:hover {\n",
" background-color: #434B5C;\n",
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
" fill: #FFFFFF;\n",
" }\n",
" </style>\n",
"\n",
" <script>\n",
" const buttonEl =\n",
" document.querySelector('#df-52fd1560-0d33-48d5-9d39-54f9e4f8cfc2 button.colab-df-convert');\n",
" buttonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
"\n",
" async function convertToInteractive(key) {\n",
" const element = document.querySelector('#df-52fd1560-0d33-48d5-9d39-54f9e4f8cfc2');\n",
" const dataTable =\n",
" await google.colab.kernel.invokeFunction('convertToInteractive',\n",
" [key], {});\n",
" if (!dataTable) return;\n",
"\n",
" const docLinkHtml = 'Like what you see? Visit the ' +\n",
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
" + ' to learn more about interactive tables.';\n",
" element.innerHTML = '';\n",
" dataTable['output_type'] = 'display_data';\n",
" await google.colab.output.renderOutput(dataTable, element);\n",
" const docLink = document.createElement('div');\n",
" docLink.innerHTML = docLinkHtml;\n",
" element.appendChild(docLink);\n",
" }\n",
" </script>\n",
" </div>\n",
" </div>\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",
" <div id=\"df-f763f16d-b817-44a9-ad56-4034883016f0\">\n",
" <div class=\"colab-df-container\">\n",
" <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>Timestamp</th>\n",
" <th>CAN ID</th>\n",
" <th>Byte</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>AttackType</th>\n",
" <th>Message</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1.478200e+09</td>\n",
" <td>0</td>\n",
" <td>8</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>0</td>\n",
" <td>0</td>\n",
" <td>DoS Attack</td>\n",
" <td>0.000000e+00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1.478201e+09</td>\n",
" <td>305</td>\n",
" <td>8</td>\n",
" <td>27</td>\n",
" <td>128</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>63</td>\n",
" <td>127</td>\n",
" <td>14</td>\n",
" <td>166</td>\n",
" <td>Normal Message</td>\n",
" <td>2.712801e+16</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1.478199e+09</td>\n",
" <td>161</td>\n",
" <td>8</td>\n",
" <td>128</td>\n",
" <td>137</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>36</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>Normal Message</td>\n",
" <td>1.281370e+12</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1.478200e+09</td>\n",
" <td>608</td>\n",
" <td>8</td>\n",
" <td>24</td>\n",
" <td>33</td>\n",
" <td>34</td>\n",
" <td>48</td>\n",
" <td>8</td>\n",
" <td>143</td>\n",
" <td>112</td>\n",
" <td>5</td>\n",
" <td>Normal Message</td>\n",
" <td>2.433345e+15</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1.478201e+09</td>\n",
" <td>704</td>\n",
" <td>8</td>\n",
" <td>20</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>0</td>\n",
" <td>Normal Message</td>\n",
" <td>2.000000e+08</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>1.478200e+09</td>\n",
" <td>304</td>\n",
" <td>8</td>\n",
" <td>11</td>\n",
" <td>128</td>\n",
" <td>0</td>\n",
" <td>255</td>\n",
" <td>8</td>\n",
" <td>128</td>\n",
" <td>4</td>\n",
" <td>136</td>\n",
" <td>Normal Message</td>\n",
" <td>1.112803e+16</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>1.478200e+09</td>\n",
" <td>880</td>\n",
" <td>8</td>\n",
" <td>0</td>\n",
" <td>32</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>Normal Message</td>\n",
" <td>3.200000e+07</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>1.478199e+09</td>\n",
" <td>1264</td>\n",
" <td>8</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>128</td>\n",
" <td>0</td>\n",
" <td>105</td>\n",
" <td>209</td>\n",
" <td>19</td>\n",
" <td>Normal Message</td>\n",
" <td>1.280105e+11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>1.478199e+09</td>\n",
" <td>304</td>\n",
" <td>8</td>\n",
" <td>5</td>\n",
" <td>128</td>\n",
" <td>0</td>\n",
" <td>255</td>\n",
" <td>11</td>\n",
" <td>128</td>\n",
" <td>12</td>\n",
" <td>237</td>\n",
" <td>Normal Message</td>\n",
" <td>5.128026e+17</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>1.478198e+09</td>\n",
" <td>305</td>\n",
" <td>8</td>\n",
" <td>247</td>\n",
" <td>127</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>76</td>\n",
" <td>127</td>\n",
" <td>13</td>\n",
" <td>231</td>\n",
" <td>Normal Message</td>\n",
" <td>2.471270e+17</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>\n",
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-f763f16d-b817-44a9-ad56-4034883016f0')\"\n",
" title=\"Convert this dataframe to an interactive table.\"\n",
" style=\"display:none;\">\n",
" \n",
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
" width=\"24px\">\n",
" <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
" <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
" </svg>\n",
" </button>\n",
" \n",
" <style>\n",
" .colab-df-container {\n",
" display:flex;\n",
" flex-wrap:wrap;\n",
" gap: 12px;\n",
" }\n",
"\n",
" .colab-df-convert {\n",
" background-color: #E8F0FE;\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: #1967D2;\n",
" height: 32px;\n",
" padding: 0 0 0 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-convert:hover {\n",
" background-color: #E2EBFA;\n",
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: #174EA6;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert {\n",
" background-color: #3B4455;\n",
" fill: #D2E3FC;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert:hover {\n",
" background-color: #434B5C;\n",
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
" fill: #FFFFFF;\n",
" }\n",
" </style>\n",
"\n",
" <script>\n",
" const buttonEl =\n",
" document.querySelector('#df-f763f16d-b817-44a9-ad56-4034883016f0 button.colab-df-convert');\n",
" buttonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
"\n",
" async function convertToInteractive(key) {\n",
" const element = document.querySelector('#df-f763f16d-b817-44a9-ad56-4034883016f0');\n",
" const dataTable =\n",
" await google.colab.kernel.invokeFunction('convertToInteractive',\n",
" [key], {});\n",
" if (!dataTable) return;\n",
"\n",
" const docLinkHtml = 'Like what you see? Visit the ' +\n",
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
" + ' to learn more about interactive tables.';\n",
" element.innerHTML = '';\n",
" dataTable['output_type'] = 'display_data';\n",
" await google.colab.output.renderOutput(dataTable, element);\n",
" const docLink = document.createElement('div');\n",
" docLink.innerHTML = docLinkHtml;\n",
" element.appendChild(docLink);\n",
" }\n",
" </script>\n",
" </div>\n",
" </div>\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",
" <div id=\"df-e686d4e7-84f8-48f3-8422-816b98e5394b\">\n",
" <div class=\"colab-df-container\">\n",
" <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>Timestamp</th>\n",
" <th>CAN ID</th>\n",
" <th>Byte</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>AttackType</th>\n",
" <th>Message</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2016-11-03 19:08:43.044157</td>\n",
" <td>0</td>\n",
" <td>8</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>0</td>\n",
" <td>0</td>\n",
" <td>DoS Attack</td>\n",
" <td>0.000000e+00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2016-11-03 19:24:35.989254</td>\n",
" <td>305</td>\n",
" <td>8</td>\n",
" <td>27</td>\n",
" <td>128</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>63</td>\n",
" <td>127</td>\n",
" <td>14</td>\n",
" <td>166</td>\n",
" <td>Normal Message</td>\n",
" <td>2.712801e+16</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2016-11-03 18:54:13.788681</td>\n",
" <td>161</td>\n",
" <td>8</td>\n",
" <td>128</td>\n",
" <td>137</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>36</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>Normal Message</td>\n",
" <td>1.281370e+12</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2016-11-03 19:06:50.286119</td>\n",
" <td>608</td>\n",
" <td>8</td>\n",
" <td>24</td>\n",
" <td>33</td>\n",
" <td>34</td>\n",
" <td>48</td>\n",
" <td>8</td>\n",
" <td>143</td>\n",
" <td>112</td>\n",
" <td>5</td>\n",
" <td>Normal Message</td>\n",
" <td>2.433345e+15</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2016-11-03 19:26:04.139714</td>\n",
" <td>704</td>\n",
" <td>8</td>\n",
" <td>20</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>0</td>\n",
" <td>Normal Message</td>\n",
" <td>2.000000e+08</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",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>95</th>\n",
" <td>2016-11-03 19:05:13.346416</td>\n",
" <td>0</td>\n",
" <td>8</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>0</td>\n",
" <td>0</td>\n",
" <td>DoS Attack</td>\n",
" <td>0.000000e+00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>97</th>\n",
" <td>2016-11-03 19:15:01.146305</td>\n",
" <td>704</td>\n",
" <td>8</td>\n",
" <td>20</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>0</td>\n",
" <td>Normal Message</td>\n",
" <td>2.000000e+08</td>\n",
" </tr>\n",
" <tr>\n",
" <th>98</th>\n",
" <td>2016-11-03 18:56:54.761137</td>\n",
" <td>809</td>\n",
" <td>8</td>\n",
" <td>220</td>\n",
" <td>190</td>\n",
" <td>127</td>\n",
" <td>20</td>\n",
" <td>17</td>\n",
" <td>32</td>\n",
" <td>0</td>\n",
" <td>20</td>\n",
" <td>Normal Message</td>\n",
" <td>2.201901e+17</td>\n",
" </tr>\n",
" <tr>\n",
" <th>99</th>\n",
" <td>2016-11-03 18:52:14.511839</td>\n",
" <td>497</td>\n",
" <td>8</td>\n",
" <td>8</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>0</td>\n",
" <td>Normal Message</td>\n",
" <td>8.000000e+07</td>\n",
" </tr>\n",
" <tr>\n",
" <th>100</th>\n",
" <td>2016-11-03 19:16:38.790256</td>\n",
" <td>704</td>\n",
" <td>8</td>\n",
" <td>20</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>0</td>\n",
" <td>Normal Message</td>\n",
" <td>2.000000e+08</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>100 rows × 13 columns</p>\n",
"</div>\n",
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-e686d4e7-84f8-48f3-8422-816b98e5394b')\"\n",
" title=\"Convert this dataframe to an interactive table.\"\n",
" style=\"display:none;\">\n",
" \n",
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
" width=\"24px\">\n",
" <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
" <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
" </svg>\n",
" </button>\n",
" \n",
" <style>\n",
" .colab-df-container {\n",
" display:flex;\n",
" flex-wrap:wrap;\n",
" gap: 12px;\n",
" }\n",
"\n",
" .colab-df-convert {\n",
" background-color: #E8F0FE;\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: #1967D2;\n",
" height: 32px;\n",
" padding: 0 0 0 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-convert:hover {\n",
" background-color: #E2EBFA;\n",
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: #174EA6;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert {\n",
" background-color: #3B4455;\n",
" fill: #D2E3FC;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert:hover {\n",
" background-color: #434B5C;\n",
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
" fill: #FFFFFF;\n",
" }\n",
" </style>\n",
"\n",
" <script>\n",
" const buttonEl =\n",
" document.querySelector('#df-e686d4e7-84f8-48f3-8422-816b98e5394b button.colab-df-convert');\n",
" buttonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
"\n",
" async function convertToInteractive(key) {\n",
" const element = document.querySelector('#df-e686d4e7-84f8-48f3-8422-816b98e5394b');\n",
" const dataTable =\n",
" await google.colab.kernel.invokeFunction('convertToInteractive',\n",
" [key], {});\n",
" if (!dataTable) return;\n",
"\n",
" const docLinkHtml = 'Like what you see? Visit the ' +\n",
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
" + ' to learn more about interactive tables.';\n",
" element.innerHTML = '';\n",
" dataTable['output_type'] = 'display_data';\n",
" await google.colab.output.renderOutput(dataTable, element);\n",
" const docLink = document.createElement('div');\n",
" docLink.innerHTML = docLinkHtml;\n",
" element.appendChild(docLink);\n",
" }\n",
" </script>\n",
" </div>\n",
" </div>\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": []
}
]
}