{ "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 : \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 }