Skip to content
ml-recall

Topic notes

Evaluation Metrics

How to score models — confusion-matrix metrics, thresholds, ROC/AUC and PR curves, multiclass averaging, regression errors and R², ranking metrics and calibration.

5-min read AI-checked

Practise 69 questions Take a test

Key ideas

Confusion matrix (also called an error matrix). For a binary classifier it counts true positives (TP), false positives (FP: an actual negative predicted positive, a false alarm), false negatives (FN: a missed positive) and true negatives (TN). "True/false" says whether the prediction was right; "positive/negative" names the class that was predicted1. Check which axis holds the true class — below, rows are true classes.

Binary metrics1

Thresholds. A classifier outputs a score; a threshold turns it into a label. Raising the threshold labels fewer examples positive, so TP and FP both fall while TN and FN both rise: recall can only fall or stay, precision usually (not always) rises1. The threshold changes decisions, never the scores.

ROC and AUC2. The ROC curve plots TPR against FPR over all thresholds, from (0,0)(0, 0) (nothing labelled positive) to (1,1)(1, 1) (everything positive). Random guessing gives the diagonal, AUC 0.5; a perfect ranking gives 1.0. AUC is the probability that a random positive gets a higher score than a random negative, so it depends only on the order of the scores: a strictly increasing transform such as log⁡\log leaves it unchanged, and so does any later choice of threshold. An AUC below 0.5 means the ranking is reversed — flipping the predictions turns AUC into 1−AUC1 - \text{AUC}; a constant predictor is only at chance. To choose an operating point, pick a low FPR when false positives are costly and a high TPR when misses are costly2.

Rare positives. On imbalanced data, precision–recall curves often compare models more clearly than ROC curves2. A random ranker has precision equal to the positive rate at every recall, so its average precision (area under the PR curve) is about that rate — 0.02 for 2% fraud — while its ROC AUC stays at 0.53.

Multiclass3. With rows as true classes, precision of class ii divides the diagonal entry by its column sum (everything predicted ii); recall divides it by its row sum (everything truly ii). Macro averaging takes the plain mean of the per-class scores, so a rare class weighs as much as a frequent one; weighted averaging weights by class size; micro averaging pools the counts, and for single-label multiclass data micro precision, recall and F1 all equal accuracy3. Macro-F1 is the mean of the per-class F1 scores, not the F1 of the mean precision and recall.

Matthews correlation (MCC) uses all four cells and ranges from −1-1 through 0 (random) to +1+1 (perfect), which keeps it informative under imbalance3.

Regression metrics3

Squared-error metrics are dominated by the examples with large values, so a model can have a high R² and a poor MAPE at the same time.

Ranking4. Precision@kk = relevant items in the top kk divided by kk; recall@kk divides by the number of all relevant items. Average precision (AP) averages precision@kk over the ranks of the relevant items, divided by the total number relevant; MAP is AP averaged over queries. NDCG divides the discounted cumulative gain by that of the ideal order.

Calibration5. A classifier is well calibrated if, among the cases it gives probability about 0.8, about 80% are positive. It is checked on held-out data with a calibration curve (reliability diagram) that bins the predictions. Ranking well and being calibrated are different properties.

Formulas

precision=TPTP+FP,recall=TPTP+FN,F1=2PRP+R=2 TP2 TP+FP+FN\text{precision} = \frac{TP}{TP + FP}, \qquad \text{recall} = \frac{TP}{TP + FN}, \qquad F_1 = \frac{2PR}{P + R} = \frac{2\,TP}{2\,TP + FP + FN} MCC=TP⋅TN−FP⋅FN(TP+FP)(TP+FN)(TN+FP)(TN+FN)\mathrm{MCC} = \frac{TP \cdot TN - FP \cdot FN}{\sqrt{(TP + FP)(TP + FN)(TN + FP)(TN + FN)}} R2=1−∑i(yi−y^i)2∑i(yi−yˉ)2,DCG=∑irelilog⁡2(i+1)R^2 = 1 - \frac{\sum_i (y_i - \hat y_i)^2}{\sum_i (y_i - \bar y)^2}, \qquad \mathrm{DCG} = \sum_{i} \frac{\mathrm{rel}_i}{\log_2(i + 1)}

ROC AUC = share of (positive, negative) pairs in which the positive has the higher score, ties counting one half.

Worked example

Classification. 1000 items, 20 of them positive; the model gets TP = 15, FN = 5, FP = 25, TN = 955.

AUC by counting pairs. Positive scores 0.8, 0.4; negative scores 0.6, 0.3, 0.2. The 0.8 beats all 3 negatives and the 0.4 beats 2, so AUC =5/6≈0.833= 5/6 \approx 0.833. Taking the log of every score keeps 0.833; replacing each score ss by 1−s1 - s gives 1/61/6.

Regression. y=(3,5,8,4)y = (3, 5, 8, 4), y^=(4,5,6,4)\hat y = (4, 5, 6, 4): errors y^−y=1,0,−2,0\hat y - y = 1, 0, -2, 0, so MAE =0.75= 0.75, MSE =1.25= 1.25, RMSE ≈1.118\approx 1.118. With yˉ=5\bar y = 5, the total sum of squares is 14 and the residual sum 5, so R2=1−5/14≈0.643R^2 = 1 - 5/14 \approx 0.643. Predicting 6 for every case gives R2≈−0.286R^2 \approx -0.286.

In code

from sklearn.metrics import (confusion_matrix, f1_score, precision_score,
                             r2_score, recall_score, roc_auc_score)

y_true = [1, 1, 0, 0, 0, 1, 0, 0]
score = [0.9, 0.4, 0.35, 0.8, 0.1, 0.7, 0.2, 0.3]     # predicted P(y = 1)
y_pred = [int(s >= 0.5) for s in score]               # threshold 0.5
print(confusion_matrix(y_true, y_pred).ravel())       # [4 1 1 2] = tn, fp, fn, tp
p, r = precision_score(y_true, y_pred), recall_score(y_true, y_pred)
f1, auc = f1_score(y_true, y_pred), roc_auc_score(y_true, score)
print(round(p, 3), round(r, 3), round(f1, 3), round(auc, 3))  # 0.667 0.667 0.667 0.867
print(round(r2_score([3, 5, 8, 4], [4, 5, 6, 4]), 3),          # 0.643
      round(r2_score([3, 5, 8, 4], [6, 6, 6, 6]), 3))          # -0.286

Common pitfalls

References

  1. Machine Learning Crash Course — Classification: Accuracy, recall, precision, and related metrics (incl. Choice of metric and tradeoffs; F1 score)
  2. Machine Learning Crash Course — Classification: ROC and AUC (incl. Precision-recall curve; AUC and ROC for choosing model and threshold)
  3. scikit-learn User Guide — Sec. 3.4.4 Classification metrics (3.4.4.9 Precision, recall and F-measures; 3.4.4.13 Matthews correlation coefficient) and Sec. 3.4.6 Regression metrics (3.4.6.1 R² score; 3.4.6.5 Mean absolute percentage error)
  4. Wikipedia: Evaluation measures (information retrieval) — Offline metrics (Precision at k, Average precision, Mean average precision, Discounted cumulative gain)
  5. scikit-learn User Guide — Sec. 1.16 Probability calibration (1.16.1 Calibration curves)