How do you know if your model is genuinely ready for production? In this class, we deconstruct the Confusion Matrix, master ROC-AUC and Precision-Recall Curves, and learn how to construct a Business Profit Curve.
| Metric | Mathematical Formula | Business Focus |
|---|---|---|
| Precision | \(\frac{\text{TP}}{\text{TP} + \text{FP}}\) | "When the model predicts positive, how often is it right?" (Minimizes False Alarms). |
| Recall (Sensitivity) | \(\frac{\text{TP}}{\text{TP} + \text{FN}}\) | "Out of all actual positive cases, how many did we catch?" (Minimizes Misses). |
| F1-Score | \(2 \times \frac{\text{Precision} \times \text{Recall}}{\text{Precision} + \text{Recall}}\) | Harmonic mean balancing precision and recall. |
| Specificity | \(\frac{\text{TN}}{\text{TN} + \text{FP}}\) | True Negative Rate — ability to correctly identify negative cases. |
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve, auc, precision_recall_curve
# Simulated ground truth and model predicted probabilities
y_true = np.array([0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0])
y_scores = np.array([0.15, 0.22, 0.85, 0.31, 0.74, 0.90, 0.42, 0.65, 0.12, 0.78, 0.25, 0.38, 0.88, 0.55, 0.19])
# 1. Compute ROC Curve
fpr, tpr, roc_thresholds = roc_curve(y_true, y_scores)
roc_auc = auc(fpr, tpr)
print(f"ROC-AUC Area: {roc_auc:.4f}")
# 2. Compute Precision-Recall Curve
precision, recall, pr_thresholds = precision_recall_curve(y_true, y_scores)
# 3. Simulate Financial Profit Curve
# Profit per True Positive (Saved Customer): $200
# Cost of False Positive (Unnecessary Discount): -$20
profits = []
for t in np.linspace(0.1, 0.9, 50):
preds = (y_scores >= t).astype(int)
tp = np.sum((preds == 1) & (y_true == 1))
fp = np.sum((preds == 1) & (y_true == 0))
net_profit = (tp * 200) - (fp * 20)
profits.append((t, net_profit))
best_threshold, max_profit = max(profits, key=lambda x: x[1])
print(f"Profit-Maximizing Threshold: {best_threshold:.2f} (Net Profit: $ {max_profit})")
What is an ROC curve and what do the X and Y axes represent? (Answer: The Receiver Operating Characteristic curve plots True Positive Rate (Sensitivity) on the Y-axis against False Positive Rate (1 - Specificity) on the X-axis across all classification thresholds).
Broaden your statistical analysis, Python data wrangling, and production engineering expertise:
Tune probability thresholds based on false positive vs false negative costs.
Evaluate continuous models using MAE, RMSE, and explained variance.
Verify model fairness and debug erroneous predictions.
Review precision vs recall trade-off questions asked in FAANG interviews.