Classification metrics

core 11 minmodel-evaluationmetrics

Builds on:Logistic regression

The idea

Every hard classification decision lands in one cell of the confusion matrix:

Predicted +Predicted −
Actual +TPFN
Actual −FPTN

All the named metrics are just ratios over this table, each answering a different question:

Precision=TPTP+FP,Recall (TPR)=TPTP+FN,FPR=FPFP+TN,Accuracy=TP+TNn.\begin{aligned} \text{Precision} &= \frac{TP}{TP+FP}, & \text{Recall (TPR)} &= \frac{TP}{TP+FN}, \\ \text{FPR} &= \frac{FP}{FP+TN}, & \text{Accuracy} &= \frac{TP+TN}{n}. \end{aligned}

Precision and recall fight through the threshold: flag more (lower threshold) and recall rises while precision drifts down toward the base rate. F1 compresses the pair into one number using the harmonic mean,

F1=2 P⋅RP+R,F_1 = 2\,\frac{P \cdot R}{P + R},

which is dominated by the weaker of the two — you cannot F1-launder a degenerate classifier (P=1.0,R=0.02P = 1.0, R = 0.02 gives F1≈0.04F_1 \approx 0.04, not 0.510.51). FβF_\beta generalizes: β>1\beta > 1 weights recall more (F2F_2 for screening), β<1\beta < 1 weights precision.

ROC-AUC evaluates the scores rather than one thresholded decision. The ROC curve plots TPR against FPR as the threshold sweeps; the area under it equals the probability that a random positive outranks a random negative. AUC 0.5 = coin-flip ranking, 1.0 = perfect separation. It is threshold-free and invariant to monotone score transforms — which is both its power (fair model comparison) and its blind spot (says nothing about calibration or about performance at your operating point, and can look rosy under heavy imbalance because FPR divides by a huge negative count).

Threshold lab

Move one decision threshold. The model scores stay fixed; every metric and cost changes around that choice.

TP
70
FP
270
FN
30
TN
630
Precision
20.6%
Recall
70%
F1
31.8%
Accuracy
70%
Cost / 1k
$17,700

Cost assumes $10 per false alarm and $500 per miss. Lower thresholds catch more positives; prevalence determines how many false alarms surround them.

Worked example

A disease classifier on n=1000n = 1000 patients, prevalence 6% (60 positives):

Predicted +Predicted −
Actual + (60)TP = 40FN = 20
Actual − (940)FP = 10TN = 930
Precision=4050=0.80,Recall=4060≈0.667,F1=2⋅0.80×0.6670.80+0.667≈0.727.\begin{aligned} \text{Precision} &= \frac{40}{50} = 0.80, \\ \text{Recall} &= \frac{40}{60} \approx 0.667, \\ F_1 &= 2 \cdot \frac{0.80 \times 0.667}{0.80 + 0.667} \approx 0.727. \end{aligned}

Accuracy=40+9301000=0.97FPR=10940≈0.011\text{Accuracy} = \frac{40 + 930}{1000} = 0.97 \qquad \text{FPR} = \frac{10}{940} \approx 0.011

The readings that matter:

Cheat sheet

From the matrix: P=TPTP+FPP = \frac{TP}{TP+FP}, R=TPR=TPTP+FNR = \text{TPR} = \frac{TP}{TP+FN}, FPR=FPFP+TN=1−specificity\text{FPR} = \frac{FP}{FP+TN} = 1 - \text{specificity}, F1=2PRP+RF_1 = \frac{2PR}{P+R}, Fβ=(1+β2)PRβ2P+RF_\beta = (1+\beta^2)\frac{PR}{\beta^2 P + R}.

You care aboutReach for
Cost of false alarmsprecision
Cost of missesrecall (F2F_2 if you need one number)
Ranking quality, model comparisonROC-AUC
Rare positives, alert budgetsprecision-recall curve, PR-AUC
Probability quality / calibrationlog loss, Brier score, reliability curve
Overall correctness, balanced classesaccuracy (vs majority baseline)