Lesson 8 of 9
Mini Project: Comprehensive Model Evaluation Dashboard & Performance Auditor
Construct an automated model evaluation and audit dashboard. The tool ingests test ground truth labels and model prediction probabilities, computing a multi-dimensional performance report: Confusion matrix metrics, ROC-AUC, PR-AUC, Brier score calibration, and a cost-sensitive threshold optimization audit.
The Production Model Performance Auditor Implementation
import numpy as np
import pandas as pd
from sklearn.metrics import (
confusion_matrix, roc_auc_score, average_precision_score,
brier_score_loss, log_loss, f1_score
)
class ModelPerformanceAuditor:
def __init__(self, y_true: np.ndarray, y_prob: np.ndarray, threshold: float = 0.5):
self.y_true = np.asarray(y_true)
self.y_prob = np.asarray(y_prob)
self.threshold = threshold
self.y_pred = (self.y_prob >= self.threshold).astype(int)
def generate_executive_audit(self) -> pd.DataFrame:
# Atomic counts
cm = confusion_matrix(self.y_true, self.y_pred)
tn, fp, fn, tp = cm.ravel()
# Performance metrics
prec = tp / (tp + fp) if (tp + fp) > 0 else 0.0
rec = tp / (tp + fn) if (tp + fn) > 0 else 0.0
f1 = f1_score(self.y_true, self.y_pred)
roc_auc = roc_auc_score(self.y_true, self.y_prob)
pr_auc = average_precision_score(self.y_true, self.y_prob)
brier = brier_score_loss(self.y_true, self.y_prob)
loss = log_loss(self.y_true, self.y_prob)
metrics = [
{'Category': 'Threshold Decision', 'Metric': 'Operational Threshold', 'Value': f"{self.threshold:.2f}"},
{'Category': 'Threshold Decision', 'Metric': 'True Positives (Caught)', 'Value': f"{tp:,}"},
{'Category': 'Threshold Decision', 'Metric': 'False Positives (Alarms)', 'Value': f"{fp:,}"},
{'Category': 'Threshold Decision', 'Metric': 'False Negatives (Missed)', 'Value': f"{fn:,}"},
{'Category': 'Classification', 'Metric': 'Precision', 'Value': f"{prec*100:.2f}%"},
{'Category': 'Classification', 'Metric': 'Recall (Sensitivity)', 'Value': f"{rec*100:.2f}%"},
{'Category': 'Classification', 'Metric': 'F1-Score', 'Value': f"{f1:.4f}"},
{'Category': 'Ranking & Discrimination', 'Metric': 'ROC-AUC', 'Value': f"{roc_auc:.4f}"},
{'Category': 'Ranking & Discrimination', 'Metric': 'PR-AUC (Avg Precision)', 'Value': f"{pr_auc:.4f}"},
{'Category': 'Probability Calibration', 'Metric': 'Brier Score Loss', 'Value': f"{brier:.4f}"},
{'Category': 'Probability Calibration', 'Metric': 'Log-Loss', 'Value': f"{loss:.4f}"},
]
return pd.DataFrame(metrics)
# Run auditor on synthetic fraud test set
np.random.seed(42)
y_test_true = np.random.choice([0, 1], size=2000, p=[0.95, 0.05])
y_test_prob = np.where(y_test_true == 1, np.random.beta(4, 2, 2000), np.random.beta(1, 10, 2000))
auditor = ModelPerformanceAuditor(y_test_true, y_test_prob, threshold=0.30)
audit_df = auditor.generate_executive_audit()
print("=== PRODUCTION MODEL PERFORMANCE AUDIT REPORT ===")
print(audit_df.to_string(index=False))Pro Tip: Exporting Model Cards from Evaluation Audits
Use the output of your evaluation auditor to populate an automated Model Card (Mitchell et al.). Storing the Model Card alongside the model artifact in your registry provides compliance auditors with permanent proof of model performance, calibration, and limitation boundaries.
MlEvaluationDashboardProject Visual Representation
Hands-On Challenge: Build an Automated Disparate Impact Metric
Extend the `ModelPerformanceAuditor` to accept a `protected_group` array, computing selection rates and adverse impact ratios alongside performance metrics.
Quick Quiz & Key Concept Checks
Common Pitfall: Celebrating ROC-AUC on Rare Events Without Checking PR-AUC
An ROC-AUC of 0.96 sounds impressive. However, if true event prevalence is 0.05% and PR-AUC is 0.08, every positive alert will produce 12 false alarms in production. Always audit PR-AUC on imbalanced sets.