Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!pip install adversarial-robustness-toolbox
- import pandas as pd
- import time
- import psutil
- import os
- from sklearn.model_selection import StratifiedKFold
- from sklearn.tree import DecisionTreeClassifier
- from sklearn.metrics import classification_report
- import joblib
- from sklearn.preprocessing import LabelEncoder
- import numpy as np
- # --- NEW IMPORTS for ART adversarial testing ---
- from art.estimators.classification.scikitlearn import ScikitlearnDecisionTreeClassifier
- from art.attacks.evasion import DecisionTreeAttack # :contentReference[oaicite:0]{index=0}
- # Step 1: Load the data
- file_path = "m3.csv"
- df = pd.read_csv(file_path)
- # Preprocessing
- label_encoder = LabelEncoder()
- X = df.drop(columns=['label'])
- y = df['label']
- X = X.apply(lambda col: label_encoder.fit_transform(col.astype(str)), axis=0, result_type='expand')
- # Step 2: Initialize the Decision Tree model
- clf = DecisionTreeClassifier(random_state=42)
- # Step 3: Set up 2-Fold Cross-Validation
- skf = StratifiedKFold(n_splits=2, shuffle=True, random_state=42)
- fold = 1
- for train_index, test_index in skf.split(X, y):
- print(f"\n=== Fold {fold} ===")
- X_train, X_test = X.iloc[train_index], X.iloc[test_index]
- y_train, y_test = y.iloc[train_index], y.iloc[test_index]
- # Train
- start = time.time()
- clf.fit(X_train, y_train)
- train_time = time.time() - start
- print(f"Training time: {train_time:.4f}s")
- # Predict (clean)
- start = time.time()
- preds = clf.predict(X_test)
- pred_time = time.time() - start
- print(f"Prediction time: {pred_time:.4f}s")
- # Memory/CPU during prediction
- proc = psutil.Process(os.getpid())
- mem_before = proc.memory_info().rss / (1024**2)
- cpu_before = psutil.cpu_percent(interval=1)
- clf.predict(X_test)
- mem_after = proc.memory_info().rss / (1024**2)
- cpu_after = psutil.cpu_percent(interval=1)
- print(f"Memory delta: {mem_after - mem_before:.4f} MB")
- print(f"CPU delta: {cpu_after - cpu_before:.4f}%")
- # Clean classification report
- print("Clean Classification Report:")
- print(classification_report(y_test, preds))
- # Save model size
- model_path = f"decision_tree_model_fold{fold}.pkl"
- joblib.dump(clf, model_path)
- size_mb = os.path.getsize(model_path) / (1024**2)
- print(f"Model size: {size_mb:.4f} MB")
- # --- ADVERSARIAL TESTING BLOCK ---
- # 1. Wrap trained model for ART
- clip_min, clip_max = X_train.min().min(), X_train.max().max()
- art_classifier = ScikitlearnDecisionTreeClassifier(
- model=clf,
- clip_values=(clip_min, clip_max)
- )
- # 2. Set up the Decision Tree attack
- attack = DecisionTreeAttack(
- classifier=art_classifier,
- offset=0.001,
- verbose=False
- )
- # 3. Generate adversarial examples
- x_test_np = X_test.to_numpy()
- y_test_np = y_test.to_numpy()
- x_test_adv = attack.generate(x=x_test_np, y=y_test_np)
- # 4. Evaluate robust accuracy
- adv_preds = art_classifier.predict(x_test_adv).argmax(axis=1)
- robust_acc = (adv_preds == y_test_np).mean() * 100
- print(f"Robust accuracy under DecisionTreeAttack: {robust_acc:.2f}%")
- fold+=1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement