A metric turns a model’s predictions into a number you can compare, and the choice of metric quietly decides what “better” means. Accuracy can look excellent on a model that never finds the rare class you care about, and a high ROC-AUC can hide a model that is useless at the precision you need. I keep this page as a checklist for matching the metric to the cost of the mistakes.

Precision: of what you caught, how much was right. Recall: of what was there, how much you caught. Image: Pouria, CC BY-SA 4.0, via Wikimedia Commons.
Mental Model
- Start from the confusion matrix. Precision, recall, specificity and F1 are all ratios of its four cells.
- Threshold metrics (precision, recall, F1) describe one operating point. Ranking metrics (ROC-AUC, PR-AUC) describe the score ordering over all thresholds.
- Under heavy class imbalance, ROC-AUC is optimistic because true negatives dominate the false positive rate. PR curves track what a user of the predictions experiences.
- A model can rank well and still output badly calibrated probabilities. If you act on the probabilities, check calibration separately.
- The right metric follows from the cost of each error type, not from habit.
1. The Confusion Matrix
For a binary classifier with a positive class, the four outcomes are true positives (TP), false positives (FP), false negatives (FN) and true negatives (TN). The derived rates:
Precision answers “of what I flagged, how much was right”. Recall answers “of what was out there, how much did I find”. The F1 score is their harmonic mean, , which is low if either is low. The general weights recall times as much as precision.
Worked example
Screening 1000 cases, 60 of which are positive. The model finds 40 of them (TP , FN ) and wrongly flags 10 of the 940 negatives (FP , TN ). Accuracy . Precision . Recall . F1 . FPR . A model that predicts “negative” for everything has accuracy and recall 0, which is why 0.97 is less impressive than it sounds.
For more than two classes, compute per-class metrics and average them. Macro averaging weights every class equally and so exposes failures on rare classes. Micro averaging pools all decisions, so it follows the frequent classes (for single-label problems, micro-averaged F1 equals accuracy).
2. Thresholds, ROC and PR Curves
Most models output a score, and the class label comes from a threshold. Sweeping the threshold traces two curves. The ROC curve plots TPR against FPR, and the area under it (ROC-AUC) equals the probability that a random positive gets a higher score than a random negative. It is 0.5 for random scores and 1 for a perfect ranking. The PR curve plots precision against recall, and its area is estimated as average precision (AP). The baseline for PR is the positive rate, not 0.5.
Why They Disagree Under Imbalance
FPR divides false positives by the number of negatives. When negatives outnumber positives 99 to 1, a thousand false positives can still be a tiny FPR, so the ROC curve barely moves. Precision divides by the number of flagged items, so the same thousand false positives swamp the true positives and precision collapses.
The experiment below uses the same score model both times (positives drawn from , negatives from ), a threshold of 1.25, 10 000 samples, and changes only the class balance.
import numpy as np
def roc_auc(y, s): # rank-based (Mann-Whitney)
r = np.argsort(np.argsort(s)) + 1
n1 = y.sum(); n0 = len(y) - n1
return (r[y == 1].sum() - n1 * (n1 + 1) / 2) / (n1 * n0)
def avg_precision(y, s):
o = np.argsort(-s); y = y[o]
tp = np.cumsum(y); prec = tp / np.arange(1, len(y) + 1)
return (prec * y).sum() / y.sum()
rng = np.random.default_rng(0)
for n_pos in [5000, 100]:
n_neg = 10000 - n_pos
y = np.r_[np.ones(n_pos), np.zeros(n_neg)]
s = np.r_[rng.normal(1.5, 1, n_pos), rng.normal(0, 1, n_neg)] # same score model both times
t = 1.5 / 2 + 0.5 # a fixed threshold
pred = s > t
tp = (pred & (y == 1)).sum(); fp = (pred & (y == 0)).sum()
print(f"pos={n_pos:5d} ROC-AUC={roc_auc(y, s):.3f} AP={avg_precision(y, s):.3f} "
f"TP={tp} FP={fp} precision={tp/(tp+fp):.3f} recall={tp/n_pos:.3f}")Running it prints:
| Positives | ROC-AUC | AP | TP | FP | Precision | Recall |
|---|---|---|---|---|---|---|
| 5000 (50%) | 0.853 | 0.852 | 2937 | 512 | 0.852 | 0.587 |
| 100 (1%) | 0.841 | 0.089 | 57 | 1027 | 0.053 | 0.570 |
ROC-AUC moves from 0.853 to 0.841, barely changed, because the score distributions are identical. AP falls from 0.852 to 0.089, and at the fixed threshold the precision drops from 0.85 to 0.05: roughly 19 of every 20 flagged cases are false alarms. The model did not get worse; the base rate changed. Analytic check: with these Gaussians TPR at 1.25 is and FPR is , which at 1% prevalence gives precision near .
So ROC-AUC is a fair summary of ranking quality independent of prevalence, and PR-AUC is the better view of usefulness when positives are rare and you care about the positive class.
3. Calibration
A model is calibrated if, among predictions of “0.7”, about 70% are actually positive. Ranking metrics ignore this, since any monotone transform of the score leaves AUC unchanged. If you use probabilities for decisions (expected cost, risk thresholds, combining models), you need calibration.
A reliability diagram bins predictions and plots the observed positive rate in each bin against the mean predicted probability, with the diagonal being perfect. Expected calibration error (ECE) summarizes the gap as a weighted average over bins. The Brier score, the mean of , is a proper scoring rule that rewards both calibration and sharpness. Log-loss is another. Fixes use a held-out set: Platt scaling (a logistic fit on the scores), isotonic regression, or temperature scaling for neural networks (Guo and colleagues, 2017, found modern deep nets are often overconfident, and a single temperature parameter repairs much of it). Logistic regression is often reasonably calibrated out of the box (Linear and Logistic Regression). Random forests and boosted trees often are not.
4. Regression Metrics
For targets and predictions :
MAE is in the target’s units, less sensitive to outliers, and corresponds to predicting the median. RMSE is also in target units but squares errors first, so large misses count much more, and it corresponds to predicting the mean. compares against the trivial model that always predicts : 1 is perfect, 0 matches the mean, and it can go negative on held-out data.
Worked example
Targets , predictions . Errors: . MAE . MSE , RMSE . The mean target is 4.5, so the total sum of squares is and the residual sum is , giving . One miss of 2 contributes 76% of the squared error but only 57% of the absolute error, which shows how RMSE punishes outliers.
Percentage errors (MAPE) explode near zero targets. For skewed positive targets, consider errors on the log scale.
5. Ranking Metrics
When the output is an ordered list (search, recommendation), the unit of evaluation is the list. Precision@ and recall@ look at the top . Mean reciprocal rank (MRR) averages of the first relevant item. Mean average precision averages AP across queries. NDCG handles graded relevance by discounting gains with a logarithm of the position, , and normalizing by the best possible ordering so scores lie in .
6. Choosing a Metric
| Problem | Reasonable primary metric | Watch out for |
|---|---|---|
| Balanced multi-class | accuracy or macro-F1 | classes that are balanced only on paper |
| Rare event, a human reviews alerts | PR-AUC, precision at a fixed recall | ROC-AUC looking great |
| Missing a positive is costly (screening) | recall at an acceptable precision, | threshold chosen on the test set |
| Probabilities drive decisions | log-loss, Brier, ECE | good AUC with poor calibration |
| Regression, outliers matter | RMSE | one bad point dominating |
| Regression, outlier-tolerant summary | MAE or median absolute error | hiding rare big misses |
| Search and recommendation | NDCG, MRR, recall@k | metric on a biased click log |
| Clustering with no labels | silhouette, stability | scores that favor the algorithm’s own bias |
Pick the metric before training, compute it on held-out data (Generalization and Regularization), and report a spread (cross-validation or bootstrap), not a single decimal.
Related
- Linear and Logistic Regression: the probabilities and thresholds being evaluated
- Generalization and Regularization: how to hold out data so the metric is honest
- Decision Trees and Ensembles: models whose probabilities often need calibration
- Clustering (k-means, GMM, DBSCAN): silhouette and other unsupervised scores
- The PyTorch Training Loop: computing validation metrics inside the loop
- Foundation Models L02: ML Basics: cross-entropy as both a loss and a metric