Evaluating models: metrics that matter
Confusion matrices, precision and recall, F1, ROC and AUC, and regression errors.
Animated explanation
The idea
"95% accurate" can be meaningless. If 95% of transactions are legitimate, a model that always says "legitimate" is 95% accurate and catches zero fraud.
Good evaluation starts from the cost of each kind of mistake. Missing a tumour (false negative) is far worse than an extra scan (false positive); blocking a real customer may be worse than letting a small fraud through. The confusion matrix counts each kind of mistake, and metrics like precision, recall and AUC summarise it for the decision you actually face.
Classification: beyond accuracy
- Precision answers "when the model raises an alarm, how often is it right?"
- Recall (sensitivity, true positive rate) answers "of all real positives, how many did it catch?"
- Specificity is recall for the negative class; false positive rate = 1 − specificity.
- F1 combines precision and recall; use to weight recall β times more.
Most classifiers output a score; the threshold is a business decision. Pick it on validation data to meet a target (e.g. recall ≥ 95%) or to minimise expected cost.
Threshold-free views: ROC and PR
The ROC curve plots true positive rate against false positive rate as the threshold sweeps from high to low; its area (AUC) is the probability that a random positive gets a higher score than a random negative. With heavy class imbalance, the precision–recall curve (and average precision) is more informative, because it ignores the huge pool of easy negatives.
For multi-class problems, compute per-class metrics and average them: macro (each class equal) or weighted (by class size).
Regression and calibration
For numbers, report RMSE (same units as the target, punishes large errors), MAE (robust to outliers) and . For probabilities, check calibration: among cases given a 70% probability, about 70% should be positive. Log-loss and the Brier score reward calibrated probabilities.
The algorithm, step by step
- 1Fix the metric from the problem's costs before modelling.
- 2Compute scores on validation data; build the confusion matrix at candidate thresholds.
- 3Choose the threshold that meets the operating target (cost, recall or precision).
- 4Report the chosen metrics on the test set, with confidence intervals (bootstrap) where possible.
The maths
Precision, recall, F1
ROC axes
AUC as a ranking probability
Log-loss
Strengths and weaknesses
Strengths
- Precision/recall map directly onto real costs
- AUC compares rankers independent of threshold
- Confusion matrices make error types visible
Weaknesses
- Accuracy misleads on imbalanced data
- ROC can look optimistic when positives are rare
- A single number never tells the whole story
Reach for it when…
Check your understanding
Lambdian Lab
Metrics from scratch
Implement the core metrics for binary labels in {0, 1} and compare with scikit-learn.
- confusion(y, y_pred) → (tp, fp, fn, tn)
- precision_recall_f1(y, y_pred) → (p, r, f1); return 0.0 where a denominator is 0.
- roc_auc(y, scores): the rank formula (count pairs where a positive outscores a negative; ties count ½).
- best_threshold(y, scores, min_recall): the highest threshold among the scores whose recall is at least min_recall.