Model Evaluation, Selection & Interpretability
P5.ml-methods.06 · Audience: Guest, IT/ML learner, Language professional · Prerequisites: Time Series & Data Issues
Where next?
Later in ML Methods
This module unlocks
Go up a level
P5.ml-methods.06 · Audience: Guest, IT/ML learner, Language professional · Prerequisites: Time Series & Data Issues
Later in ML Methods
This module unlocks
Go up a level
It is alarmingly easy to build a model that looks excellent and is useless — a fraud detector that never flags anything is 99 percent accurate when fraud touches one transaction in a hundred. Evaluating models honestly is therefore a discipline in its own right, and this module builds it piece by piece, from choosing scores an imbalanced dataset cannot flatter, through engineering features without letting the answer seep into them, to comparing models without quietly tuning on the data you report. It ends with two ideas interviews increasingly reach for, namely why very large models can generalise even after memorising their training data, and how to explain what a fitted model actually learned. A short calculation you can check by hand pins down every step.
Framing. Almost every evaluation mistake is a leak or a metric that flatters: the wrong curve under imbalance, a transform fit before the split, or tuning on the folds you report. Each step makes the failure concrete.
Accuracy misleads under imbalance. ROC-AUC (TPR vs FPR) normalises by the large negative count, so it is base-rate-insensitive and looks optimistic; PR-AUC (precision vs recall) targets the minority class and is preferred under heavy imbalance. Both are rank metrics and blind to calibration.
Worked example — precision and recall from a confusion matrix. A fraud model on a batch produces this confusion matrix:
predicted + predicted - actual + TP=8 FN=4 actual - FP=2 TN=986 precision = TP/(TP+FP) = 8/10 = 0.80 recall = TP/(TP+FN) = 8/12 = 0.67
Accuracy here is 994/1000 = 99.4% — flattering — while recall is only 0.67: it misses a third of the fraud. Precision 0.80 vs recall 0.67 is the trade the threshold controls, and PR-AUC summarises it across thresholds far more honestly than accuracy or ROC-AUC.
Want to move the operating point yourself? The interactive ROC / threshold lab lives in P5.evaluation.01 — Evaluation Metrics for NLP.
Interview-depth model evaluation for the ml-engineer track: metric choice under imbalance and calibration, feature engineering and leakage, nested cross-validation, generalization and double descent, and interpretability — with the assumptions and the 'why it matters' framing.
Back to the foundations: evaluation statistics live in the P1 statistics-core track (P1.statistics-core.05 — Evaluation Statistics); the regularization and capacity-control story is covered by the AI/ML foundations pillar.
Accuracy misleads under imbalance. With 99% negatives, a model that always predicts the majority class scores 99% accuracy while catching zero positives — the metric rewards the base rate, not the skill.
ROC-AUC plots the true-positive rate against the false-positive rate across thresholds:
Because FPR normalises by the (large) negative count, ROC-AUC is insensitive to the base rate and can look optimistic when negatives dominate — a handful of false positives barely moves it.
PR-AUC plots precision against recall:
Precision does depend on the positive base rate, so PR-AUC focuses squarely on the positive/minority class and is preferred under heavy imbalance (fraud, disease, rare-event detection), where the cost of false positives on a small positive class is what actually matters.
Calibration matters whenever probabilities — not just rankings — drive decisions (expected value, thresholds, cost-sensitive actions). A calibration (reliability) diagram bins predicted probabilities and plots them against observed frequencies; the expected calibration error (ECE) summarises the gap. Post-hoc fixes: Platt scaling (a logistic fit on the scores) and isotonic regression (a non-parametric monotone map). ROC-AUC and PR-AUC are rank metrics and are blind to miscalibration — a model can rank perfectly yet output badly-scaled probabilities.
Feature engineering — encodings (one-hot, target/ordinal), interaction terms, non-linear transforms, scaling/normalisation — increases the signal a model can exploit. Feature selection prunes noise to reduce variance and overfitting:
The cardinal sin is data leakage — letting information that would be unavailable at prediction time seep into training. It comes in two flavours:
Data leakage inflates offline metrics and then fails in production because the leaked signal is not really there at inference. The fix: fit every transform inside the CV fold, never on held-out data — wire preprocessing and the estimator into a single pipeline so fit sees only the training partition of each fold.
Hyperparameters (regularisation strength, tree depth, learning rate, k) are not learned by fitting — they are searched:
The trap: tuning hyperparameters and estimating generalisation on the same cross-validation reuses the validation folds for two jobs, so the model selection leaks into the performance estimate and the reported score is optimistically biased.
Nested cross-validation separates the two:
Because selection happens strictly inside the outer training partition, nested cross-validation gives an approximately unbiased estimate of generalisation — at the cost of O(outer × inner) more fits.
The classical bias-variance story is a U-curve: as capacity grows, training error falls monotonically while test error first drops (bias down) then rises (variance up), with an optimal sweet spot in the middle. This underlies "don't over-parameterise."
Double descent revises that picture. Plot test error against capacity and continue past the interpolation threshold — the point where the model has just enough capacity to fit the training set exactly (zero training error):
This ties to VC dimension / effective capacity — raw parameter count overstates the effective degrees of freedom — and to implicit regularisation: gradient descent among the many zero-training-error solutions is biased toward low-norm / minimum-complexity interpolants that generalise. Double descent is why "bigger than needed to fit the data" is not automatically worse.
Interpretability answers two different questions:
Two model-agnostic local methods:
Trade-offs. SHAP has strong theoretical guarantees (local accuracy, consistency, missingness) but exact Shapley values are exponential — practical use relies on approximations (KernelSHAP, TreeSHAP). LIME is cheaper and intuitive but its surrogate can be unstable — small changes in perturbation sampling or the neighbourhood kernel shift the explanation, and local fidelity is only as good as the surrogate. The axes to weigh: fidelity, stability, and compute.
Rigorous references for model evaluation and interpretability.
| Type | Resource |
|---|---|
| Book | Hastie, Tibshirani & Friedman, ESL — model selection, cross-validation |
| Book | Molnar, Interpretable Machine Learning — PDP, LIME, SHAP |
| Paper | Belkin et al., Reconciling modern ML and the bias–variance trade-off |
| In-app | P5.ml-methods.05 — Time Series & Data Issues, for imbalance and leakage in practice |
| In-app | P5.evaluation.01 — Evaluation Metrics for NLP, for the interactive ROC / threshold lab |
Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.
Off by default. The mentor always gets your file; this only decides whether your own copy stays here. Copies live in this browser only - they do not follow you to another device, and clearing site data removes them.
A scratch console for this page's ideas — ungraded, nothing you run here is recorded.
Scratch console
A scratch console with the scientific stack (pandas, numpy, scikit-learn). Runs on the server — no network, resource-limited and measured.
Output appears here.
Loading your notes...