Model Evaluation: Confusion Matrix, ROC-AUC & Business KPIs

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.

The 4 Quadrants of the Confusion Matrix

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.

Evaluating ROC-AUC & Threshold Tuning in Python

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})")

University Viva & Exam Checklist

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).

Frequently Asked Questions (FAQ) - Student & Interview Review

Q1. Why is Accuracy a misleading metric on imbalanced datasets?

If a fraud detection dataset has 99% legitimate transactions and 1% fraud, a trivial dummy model that predicts 'Legitimate' for every record achieves 99% accuracy while failing to identify a single instance of fraud.

Q2. When should I prioritize Precision over Recall?

Prioritize Precision when the cost of a False Positive is high (e.g., spam filtering, where marking an important business contract as spam is unacceptable). Prioritize Recall when the cost of a False Negative is catastrophic (e.g., cancer diagnosis, fraud detection, mechanical failure).

Q3. When should I use a Precision-Recall (PR) curve instead of ROC-AUC?

On heavily imbalanced datasets (e.g., < 5% positive class), ROC-AUC can present an overly optimistic assessment because the large number of True Negatives inflates the False Positive Rate denominator. PR curves focus exclusively on the minority positive class.

Q4. What does an ROC-AUC score of 0.85 mean intuitively?

An ROC-AUC of 0.85 means there is an 85% probability that the model will assign a higher predicted risk score to a randomly chosen positive instance than to a randomly chosen negative instance.

Q5. What is a Profit Curve in machine learning?

A profit curve maps model prediction threshold choices against explicit financial costs and revenues (Cost of Intervention, Value of Saved Customer, Loss from Missed Churn) to identify the exact decision threshold that maximizes net dollar profits.

Explore Related Data Science Tutorials & Next Steps

Broaden your statistical analysis, Python data wrangling, and production engineering expertise:

Customer Churn Prediction Modeling

Tune probability thresholds based on false positive vs false negative costs.

Regression Modeling & Evaluation

Evaluate continuous models using MAE, RMSE, and explained variance.

Explainable AI: SHAP & LIME

Verify model fairness and debug erroneous predictions.

130+ Data Science Interview Q&A

Review precision vs recall trade-off questions asked in FAANG interviews.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!