5.5 KiB
5.5 KiB
In [ ]:
from pathlib import Path
import numpy as np
import pandas as pd
DATA_PATH = Path("data/cicids2017_binary_sample.csv")
RANDOM_STATE = 42
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.ensemble import RandomForestClassifier
In [ ]:
df = pd.read_csv(DATA_PATH)
df.columns = df.columns.str.strip()
df = df.drop_duplicates().replace([np.inf, -np.inf], np.nan)
# TODO 1: create numeric X and binary y
X = None
y = None
# TODO 2: create a stratified 80/20 split
X_train, X_test, y_train, y_test = None, None, None, None
In [ ]:
# TODO 3: create a Pipeline with:
# - SimpleImputer(strategy="median")
# - RandomForestClassifier with n_estimators=100, max_depth=12,
# random_state=42, and n_jobs=-1
pipeline = None
print(pipeline)
In [ ]:
# TODO 4: fit the complete pipeline on X_train and y_train
# TODO 5: predict labels for X_test and store them in y_pred
y_pred = None
print("Predictions:", None)
print("Number of test samples:", None)
In [ ]:
one_flow = X_test.iloc[[0]]
# TODO 6: predict one_flow and convert the result to int
one_prediction = None
print("Prediction:", one_prediction)
print("Meaning:", "ATTACK" if one_prediction == 1 else "BENIGN")
In [ ]:
assert list(pipeline.named_steps) == ["imputer", "model"]
assert pipeline.named_steps["model"].n_estimators == 100
assert len(y_pred) == len(y_test)
assert set(np.unique(y_pred)).issubset({0, 1})
assert one_prediction in {0, 1}
print("✓ Lab 3 completed")