5.6 KiB
5.6 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.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
In [ ]:
raw_df = pd.read_csv(DATA_PATH)
raw_df.columns = raw_df.columns.str.strip()
clean_df = raw_df.drop_duplicates().copy()
clean_df = clean_df.replace([np.inf, -np.inf], np.nan)
print("Raw rows:", len(raw_df))
print("Clean rows:", len(clean_df))
print("Missing numeric values:", int(clean_df.select_dtypes(include="number").isna().sum().sum()))
In [ ]:
X = clean_df.drop(columns="Label").select_dtypes(include="number")
y = clean_df["Label"].astype(str).str.strip().str.upper().ne("BENIGN").astype(int)
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=RANDOM_STATE,
stratify=y,
)
print("Train shape:", X_train.shape)
print("Test shape:", X_test.shape)
print("Train attack rate:", round(y_train.mean(), 3))
print("Test attack rate:", round(y_test.mean(), 3))
In [ ]:
imputer = SimpleImputer(strategy="median")
scaler = StandardScaler()
X_train_imputed = imputer.fit_transform(X_train)
X_test_imputed = imputer.transform(X_test)
X_train_scaled = scaler.fit_transform(X_train_imputed)
X_test_scaled = scaler.transform(X_test_imputed)
print("Mean of scaled training columns:", np.round(X_train_scaled.mean(axis=0), 3))
print("Mean of scaled test columns:", np.round(X_test_scaled.mean(axis=0), 3))
In [ ]:
assert len(clean_df) <= len(raw_df)
assert np.isfinite(X_train_scaled).all()
assert np.isfinite(X_test_scaled).all()
assert abs(y_train.mean() - y_test.mean()) < 0.02
assert np.allclose(X_train_scaled.mean(axis=0), 0.0, atol=1e-10)
assert X_train_scaled.shape[1] == X.shape[1]
print("✓ Lab 2 completed")