Datasets:
File size: 10,631 Bytes
86830e3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | {
"cells": [
{
"cell_type": "code",
"id": "initial_id",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2026-06-30T14:55:38.259769Z",
"start_time": "2026-06-30T14:55:37.724976Z"
}
},
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sns"
],
"outputs": [],
"execution_count": 1
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2026-06-30T14:57:07.241706Z",
"start_time": "2026-06-30T14:57:02.508449Z"
}
},
"cell_type": "code",
"source": "data = pd.read_csv('../Cleaned_data.csv')",
"id": "a3d6404b70c8c5a1",
"outputs": [],
"execution_count": 7
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2026-06-30T14:57:07.621114Z",
"start_time": "2026-06-30T14:57:07.601409Z"
}
},
"cell_type": "code",
"source": "print('Data Shape::',data.shape )",
"id": "fd40b42eaa1ccce1",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Data Shape:: (2520798, 79)\n"
]
}
],
"execution_count": 8
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2026-06-30T14:57:16.859802Z",
"start_time": "2026-06-30T14:57:16.794596Z"
}
},
"cell_type": "code",
"source": [
"## we have to encode those categories like BENING to 0 and others to 1...2...etc\n",
"print('Labels :',data['Label'].unique())"
],
"id": "4c2b7cb5ac89a68d",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Labels : <StringArray>\n",
"[ 'BENIGN', 'FTP-Patator',\n",
" 'SSH-Patator', 'DoS slowloris',\n",
" 'DoS Slowhttptest', 'DoS Hulk',\n",
" 'DoS GoldenEye', 'Heartbleed',\n",
" 'Web Attack � Brute Force', 'Web Attack � XSS',\n",
" 'Web Attack � Sql Injection', 'Infiltration',\n",
" 'Bot', 'PortScan',\n",
" 'DDoS']\n",
"Length: 15, dtype: str\n"
]
}
],
"execution_count": 9
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2026-06-30T14:57:20.699603Z",
"start_time": "2026-06-30T14:57:20.375465Z"
}
},
"cell_type": "code",
"source": [
"from sklearn.preprocessing import LabelEncoder,StandardScaler\n",
"le = LabelEncoder()\n",
"data['New_Label']=le.fit_transform(data['Label'])\n",
"print(le.classes_)"
],
"id": "b1c0c404b464024a",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['BENIGN' 'Bot' 'DDoS' 'DoS GoldenEye' 'DoS Hulk' 'DoS Slowhttptest'\n",
" 'DoS slowloris' 'FTP-Patator' 'Heartbleed' 'Infiltration' 'PortScan'\n",
" 'SSH-Patator' 'Web Attack � Brute Force' 'Web Attack � Sql Injection'\n",
" 'Web Attack � XSS']\n"
]
}
],
"execution_count": 10
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2026-06-30T14:57:22.949246Z",
"start_time": "2026-06-30T14:57:22.922049Z"
}
},
"cell_type": "code",
"source": [
"print(f\"Total columns: {len(data.columns)}\")\n",
"print(data.columns.tolist())"
],
"id": "4f9c54f4f889e992",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total columns: 80\n",
"['Destination Port', 'Flow Duration', 'Total Fwd Packets', 'Total Backward Packets', 'Total Length of Fwd Packets', 'Total Length of Bwd Packets', 'Fwd Packet Length Max', 'Fwd Packet Length Min', 'Fwd Packet Length Mean', 'Fwd Packet Length Std', 'Bwd Packet Length Max', 'Bwd Packet Length Min', 'Bwd Packet Length Mean', 'Bwd Packet Length Std', 'Flow Bytes/s', 'Flow Packets/s', 'Flow IAT Mean', 'Flow IAT Std', 'Flow IAT Max', 'Flow IAT Min', 'Fwd IAT Total', 'Fwd IAT Mean', 'Fwd IAT Std', 'Fwd IAT Max', 'Fwd IAT Min', 'Bwd IAT Total', 'Bwd IAT Mean', 'Bwd IAT Std', 'Bwd IAT Max', 'Bwd IAT Min', 'Fwd PSH Flags', 'Bwd PSH Flags', 'Fwd URG Flags', 'Bwd URG Flags', 'Fwd Header Length', 'Bwd Header Length', 'Fwd Packets/s', 'Bwd Packets/s', 'Min Packet Length', 'Max Packet Length', 'Packet Length Mean', 'Packet Length Std', 'Packet Length Variance', 'FIN Flag Count', 'SYN Flag Count', 'RST Flag Count', 'PSH Flag Count', 'ACK Flag Count', 'URG Flag Count', 'CWE Flag Count', 'ECE Flag Count', 'Down/Up Ratio', 'Average Packet Size', 'Avg Fwd Segment Size', 'Avg Bwd Segment Size', 'Fwd Header Length.1', 'Fwd Avg Bytes/Bulk', 'Fwd Avg Packets/Bulk', 'Fwd Avg Bulk Rate', 'Bwd Avg Bytes/Bulk', 'Bwd Avg Packets/Bulk', 'Bwd Avg Bulk Rate', 'Subflow Fwd Packets', 'Subflow Fwd Bytes', 'Subflow Bwd Packets', 'Subflow Bwd Bytes', 'Init_Win_bytes_forward', 'Init_Win_bytes_backward', 'act_data_pkt_fwd', 'min_seg_size_forward', 'Active Mean', 'Active Std', 'Active Max', 'Active Min', 'Idle Mean', 'Idle Std', 'Idle Max', 'Idle Min', 'Label', 'New_Label']\n"
]
}
],
"execution_count": 11
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2026-06-30T14:57:25.425183Z",
"start_time": "2026-06-30T14:57:25.394293Z"
}
},
"cell_type": "code",
"source": [
"data_to_dl = data.drop(columns=['Label'])\n",
"print('Remaining columns:',len(data_to_dl.columns))"
],
"id": "f748fed425b4474d",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Remaining columns: 79\n"
]
}
],
"execution_count": 12
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2026-06-30T14:57:28.166228Z",
"start_time": "2026-06-30T14:57:28.149963Z"
}
},
"cell_type": "code",
"source": [
"X = data_to_dl.drop(columns=['New_Label'])\n",
"Y = data_to_dl['New_Label']\n",
"\n",
"print(f\"Features shape (X): {X.shape}\")\n",
"print(f\"Target shape (Y): {Y.shape}\")"
],
"id": "26c68ae50c492420",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Features shape (X): (2520798, 78)\n",
"Target shape (Y): (2520798,)\n"
]
}
],
"execution_count": 13
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2026-06-30T14:59:57.553115Z",
"start_time": "2026-06-30T14:59:52.475959Z"
}
},
"cell_type": "code",
"source": [
"## HERE WE HAVE TO APPLY S.M.O.T.E but only to TRAIN SPLIT\n",
"\n",
"from sklearn.model_selection import train_test_split\n",
"X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, random_state = 42)\n",
"\n",
"print(f'X_train shape: {X_train.shape}')\n",
"print(f'X_test shape: {X_test.shape}')\n",
"print(f'Y_train distribution: {Y_train.value_counts()}')"
],
"id": "757fac208c382bb0",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"X_train shape: (2016638, 78)\n",
"X_test shape: (504160, 78)\n",
"Y_train distribution: New_Label\n",
"0 1675805\n",
"4 138511\n",
"2 102513\n",
"10 72507\n",
"3 8207\n",
"7 4709\n",
"6 4334\n",
"5 4176\n",
"11 2553\n",
"1 1560\n",
"12 1175\n",
"14 536\n",
"9 27\n",
"13 16\n",
"8 9\n",
"Name: count, dtype: int64\n"
]
}
],
"execution_count": 14
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2026-06-30T15:01:52.339866Z",
"start_time": "2026-06-30T15:01:51.036901Z"
}
},
"cell_type": "code",
"source": [
"#Now we have to encode using StandardScaler\n",
"from sklearn.preprocessing import StandardScaler\n",
"scaler = StandardScaler()\n",
"X_train_scaled = scaler.fit_transform(X_train)\n",
"X_test_scaled = scaler.transform(X_test)\n",
"print(f'X_train scaled shape: {X_train_scaled.shape}')\n",
"print(f\"Mean of first feature (should be ~0): {X_train_scaled[:, 0].mean():.4f}\")\n",
"print(f\"Std of first feature (should be ~1): {X_train_scaled[:, 0].std():.4f}\")"
],
"id": "2d6b079049a36bfa",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"X_train scaled shape: (2016638, 78)\n",
"Mean of first feature (should be ~0): -0.0000\n",
"Std of first feature (should be ~1): 1.0000\n"
]
}
],
"execution_count": 15
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2026-06-30T15:15:02.117853Z",
"start_time": "2026-06-30T15:12:53.513901Z"
}
},
"cell_type": "code",
"source": [
"# SMOTE --> Synthetic Minority Oversampling Technique\n",
"from imblearn.over_sampling import SMOTE\n",
"smote = SMOTE(random_state=42)\n",
"X_train_smote, Y_train_smote = smote.fit_resample(X_train_scaled, Y_train)\n",
"print(f'BEFORE SMOTE SHAPE: {X_train_scaled.shape}')\n",
"print(f'AFTER SMOTE SHAPE: {X_train_smote.shape}')\n",
"print(f\"\\nClass distribution after SMOTE:\")\n",
"print(pd.Series(Y_train_smote).value_counts())"
],
"id": "d951396573721ff",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"BEFORE SMOTE SHAPE: (2016638, 78)\n",
"AFTER SMOTE SHAPE: (25137075, 78)\n",
"\n",
"Class distribution after SMOTE:\n",
"New_Label\n",
"0 1675805\n",
"2 1675805\n",
"4 1675805\n",
"10 1675805\n",
"14 1675805\n",
"5 1675805\n",
"7 1675805\n",
"12 1675805\n",
"3 1675805\n",
"1 1675805\n",
"11 1675805\n",
"6 1675805\n",
"13 1675805\n",
"8 1675805\n",
"9 1675805\n",
"Name: count, dtype: int64\n"
]
}
],
"execution_count": 18
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": "",
"id": "38c105a1f64a18ae"
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|