{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":27989,"databundleVersionId":2281517,"isSourceIdPinned":false},{"sourceType":"datasetVersion","sourceId":2215934,"datasetId":1330735,"databundleVersionId":2257579}],"dockerImageVersionId":31328,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\nimport pickle\nimport matplotlib.pyplot as plt\nfrom xgboost import plot_importance\nfrom numpy import loadtxt\nfrom xgboost import XGBClassifier\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.metrics import f1_score\nfrom sklearn.metrics import precision_score\nimport pickle\nfrom tqdm import tqdm\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.metrics import recall_score","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-04-27T19:55:39.410497Z","iopub.execute_input":"2026-04-27T19:55:39.410832Z","iopub.status.idle":"2026-04-27T19:55:41.085294Z","shell.execute_reply.started":"2026-04-27T19:55:39.410803Z","shell.execute_reply":"2026-04-27T19:55:41.084455Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/datasets/mdalamintalukder/cicids2017/MachineLearningCVE/Monday-WorkingHours.pcap_ISCX.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-27T20:21:38.293719Z","iopub.execute_input":"2026-04-27T20:21:38.294036Z","iopub.status.idle":"2026-04-27T20:21:41.800512Z","shell.execute_reply.started":"2026-04-27T20:21:38.294008Z","shell.execute_reply":"2026-04-27T20:21:41.799461Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"files_path = os.listdir('/kaggle/input/datasets/mdalamintalukder/cicids2017/MachineLearningCVE/')\nfor f in files_path:\n    if(f=='Monday-WorkingHours.pcap_ISCX.csv'):\n        continue\n    file = '/kaggle/input/datasets/mdalamintalukder/cicids2017/MachineLearningCVE/'+f\n    df2 = pd.read_csv(file)\n    df = pd.concat([df, df2])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-27T20:21:41.801987Z","iopub.execute_input":"2026-04-27T20:21:41.802238Z","iopub.status.idle":"2026-04-27T20:21:58.084245Z","shell.execute_reply.started":"2026-04-27T20:21:41.802214Z","shell.execute_reply":"2026-04-27T20:21:58.082973Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-27T20:03:18.760420Z","iopub.execute_input":"2026-04-27T20:03:18.761583Z","iopub.status.idle":"2026-04-27T20:03:18.767731Z","shell.execute_reply.started":"2026-04-27T20:03:18.761548Z","shell.execute_reply":"2026-04-27T20:03:18.766871Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df2.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-27T20:03:12.143357Z","iopub.execute_input":"2026-04-27T20:03:12.143698Z","iopub.status.idle":"2026-04-27T20:03:12.150597Z","shell.execute_reply.started":"2026-04-27T20:03:12.143670Z","shell.execute_reply":"2026-04-27T20:03:12.149679Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-27T20:19:17.014333Z","iopub.execute_input":"2026-04-27T20:19:17.015078Z","iopub.status.idle":"2026-04-27T20:19:17.021999Z","shell.execute_reply.started":"2026-04-27T20:19:17.015043Z","shell.execute_reply":"2026-04-27T20:19:17.020989Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn import preprocessing\nfor f in df.columns:\n    if df[f].dtype == 'object':\n        label = preprocessing.LabelEncoder()\n        label.fit(df[f].values)\n        df[f] = label.transform(df[f].values) \ndf.fillna(-999, inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-27T20:21:58.086342Z","iopub.execute_input":"2026-04-27T20:21:58.086730Z","iopub.status.idle":"2026-04-27T20:21:58.816300Z","shell.execute_reply.started":"2026-04-27T20:21:58.086698Z","shell.execute_reply":"2026-04-27T20:21:58.815212Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-27T20:25:20.490730Z","iopub.execute_input":"2026-04-27T20:25:20.491111Z","iopub.status.idle":"2026-04-27T20:25:20.504274Z","shell.execute_reply.started":"2026-04-27T20:25:20.491068Z","shell.execute_reply":"2026-04-27T20:25:20.503057Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.reset_index(drop=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-27T20:36:23.424022Z","iopub.execute_input":"2026-04-27T20:36:23.425806Z","iopub.status.idle":"2026-04-27T20:36:26.590281Z","shell.execute_reply.started":"2026-04-27T20:36:23.425752Z","shell.execute_reply":"2026-04-27T20:36:26.589497Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Y = df[' Label']\nX = df.drop(\" Label\",axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-27T20:52:46.767000Z","iopub.execute_input":"2026-04-27T20:52:46.767404Z","iopub.status.idle":"2026-04-27T20:52:47.712707Z","shell.execute_reply.started":"2026-04-27T20:52:46.767356Z","shell.execute_reply":"2026-04-27T20:52:47.711606Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-27T20:52:47.714264Z","iopub.execute_input":"2026-04-27T20:52:47.714673Z","iopub.status.idle":"2026-04-27T20:52:51.288514Z","shell.execute_reply.started":"2026-04-27T20:52:47.714630Z","shell.execute_reply":"2026-04-27T20:52:51.287674Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nfrom sklearn.impute import SimpleImputer\n\n# 1. Replace inf with NaN in BOTH sets\nX_train = X_train.replace([np.inf, -np.inf], np.nan) if hasattr(X_train, 'replace') else np.where(np.isinf(X_train), np.nan, X_train)\nX_test  = X_test.replace([np.inf, -np.inf], np.nan)  if hasattr(X_test, 'replace')  else np.where(np.isinf(X_test), np.nan, X_test)\n\n# 2. Impute missing/inf values (use same strategy for train & test)\nimputer = SimpleImputer(strategy='median')  # or 'most_frequent' for categorical\nX_train = imputer.fit_transform(X_train)\nX_test  = imputer.transform(X_test)\n\n# 3. Train & Predict (now guaranteed finite)\nmodel.fit(X_train, y_train)\ny_pred = model.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-27T20:52:51.459820Z","iopub.execute_input":"2026-04-27T20:52:51.460188Z","iopub.status.idle":"2026-04-27T20:59:23.520514Z","shell.execute_reply.started":"2026-04-27T20:52:51.460159Z","shell.execute_reply":"2026-04-27T20:59:23.518735Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import classification_report, confusion_matrix, roc_auc_score, roc_curve, recall_score\nprint(classification_report(y_test,y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-27T20:59:44.003335Z","iopub.execute_input":"2026-04-27T20:59:44.004710Z","iopub.status.idle":"2026-04-27T20:59:44.137127Z","shell.execute_reply.started":"2026-04-27T20:59:44.004661Z","shell.execute_reply":"2026-04-27T20:59:44.136199Z"}},"outputs":[],"execution_count":null}]}