Skip to content
ML to LLMs
Cheat sheets

Classification metrics

Confusion matrix, precision, recall, F1, ROC and PR curves, calibration, and how to pick a threshold.

Predicted positivePredicted negative
Actually positiveTPFN
Actually negativeFPTN
MetricFormulaQuestion it answers
Accuracy(TP+TN)/N(TP + TN)/NHow often is the model right? (misleading when classes are imbalanced)
PrecisionTP/(TP+FP)TP/(TP + FP)Of the alarms, how many were real?
Recall (sensitivity, TPR)TP/(TP+FN)TP/(TP + FN)Of the real positives, how many did we catch?
Specificity (TNR)TN/(TN+FP)TN/(TN + FP)Of the negatives, how many did we leave alone?
FPRFP/(FP+TN)FP/(FP + TN)How often do negatives raise alarms?
F12PR/(P+R)2PR/(P + R)Balance of precision and recall (harmonic mean)
ROC AUCarea under TPR vs FPRProbability a random positive outranks a random negative
PR AUC (average precision)area under precision vs recallRanking quality for rare positives
Log loss−1n∑[yln⁡p+(1−y)ln⁡(1−p)]-\frac{1}{n}\sum[y\ln p + (1-y)\ln(1-p)]Quality of the probabilities themselves
Brier score1n∑(p−y)2\frac{1}{n}\sum(p - y)^2Calibration plus sharpness

Choosing a threshold: minimize cFP⋅FP+cFN⋅FNc_{FP}\cdot FP + c_{FN}\cdot FN on validation data. Lower thresholds raise recall and false positives.

When precision matters more: automated actions, spam filtering, scarce reviewer time. When recall matters more: screening, fraud with human review, safety detection.

Calibration: among predictions near 0.3, about 30% should be positive. Check a reliability diagram; recalibrate with Platt scaling or isotonic regression.

Watch out

Never report accuracy alone on imbalanced data, and never tune the threshold on the test set.

Lessons: Classification metrics and thresholds · Logistic regression · Lab: threshold