Foundations to Frontier
Tests
D. Classical Machine Learning › D0 Core Concepts (concept pages)

Classification Metrics: Accuracy, Precision, Recall, F1, ROC-AUC, PR-AUC, Log-Loss, MCC and Cohen's κ

Classification models are scored from the counts of correct and wrong decisions (true and false positives and negatives) or from their scores and probabilities. Accuracy counts correct decisions; precision and recall focus on the positive class; F1 combines them; MCC and Cohen's κ correct for chance and imbalance; ROC-AUC measures ranking quality across thresholds; PR-AUC does the same with attention to rare positives; and log-loss and the Brier score judge probabilities. This page defines all of them, shows how they relate, and explains which ones break under class imbalance. In the exercises you implement every metric, including ROC and PR curves with ties; then you hold a classifier's ranking ability fixed and measure how each metric changes as positives go from 50% to 0.2% of the data.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Catching the rare thing

A smoke alarm can be wrong in two ways. It can stay quiet during a fire (a miss), or beep when you are only making toast (a false alarm). If fires are very rare, an alarm that never beeps is “right” 99.99% of the time, yet useless. So we ask two separate questions:

  • Of the real fires, how many did it catch (recall)?
  • Of its beeps, how many were real fires (precision)?
Thresholds, imbalance and classification metrics
predict positive →
predicted +predicted −
actual +TP 69FN 31
actual −FP 60TN 840
accuracy 0.909 (all-negative: 0.900) · precision 0.535 · recall 0.691 · F1 0.603 · MCC 0.559
ROC-AUC 0.921 · PR-AUC 0.660 (random classifier: 0.5 and 0.10)
ROC: FPR → TPRPR: recall → precision
Blue: negative scores, orange: positive scores (heights scaled by class frequency). Lower the positive share: ROC-AUC does not move, but precision, F1 and PR-AUC fall, and accuracy approaches that of predicting "negative" for everything.
Why engineers care

Fault detectors, collision warnings and anomaly alarms all hunt for rare events. A detector that reports “99.9% accuracy” may never detect a single fault. The right metric decides whether operators trust the alarm or switch it off.

📝 Quick check (Level 0)
0/3 answered
  1. 1. An alarm that never sounds, when fires happen 1% of the time, has accuracy…
  2. 2. Recall asks…
  3. 3. Precision asks…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

The confusion matrix and its metrics

predicted + predicted −
actual + TP FN
actual − FP TN
Metric Formula Question
accuracy (TP + TN) / n how often correct
precision TP / (TP + FP) how reliable a positive call is
recall (sensitivity, TPR) TP / (TP + FN) how many positives are found
specificity (TNR) TN / (TN + FP) how many negatives are cleared
F1 2PR / (P + R) balance of precision and recall
MCC (TP·TN − FP·FN) / √(…) correlation between prediction and truth (−1 to 1)
Cohen’s κ (p_o − p_e) / (1 − p_e) agreement beyond chance

Score-based metrics: ROC-AUC (TPR against FPR over all thresholds), PR-AUC or average precision (precision against recall), log-loss and Brier score (quality of probabilities).

from sklearn.metrics import classification_report, roc_auc_score, average_precision_score, matthews_corrcoef, log_loss
print(classification_report(y, y_pred)); print(roc_auc_score(y, scores), average_precision_score(y, scores))
📝 Quick check (Level 1)
0/3 answered
  1. 1. TP = 30, FP = 10: precision?
  2. 2. TP = 30, FN = 20: recall?
  3. 3. ROC-AUC of a random scorer?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

How the metrics relate

  • ROC-AUC equals the probability that a random positive is scored above a random negative (Mann–Whitney). It depends only on the ranking and does not change with class balance.
  • Average precision summarises the precision–recall curve. A random scorer gets PR-AUC equal to the prevalence π, so its scale changes with imbalance. That is why it is informative for rare positives.
  • F1 ignores true negatives. MCC uses all four cells and is high only when the classifier does well on both classes. κ is close to MCC in practice.
  • Precision depends on prevalence: with fixed TPR and FPR, precision = π·TPR / (π·TPR + (1−π)·FPR).
  • Probability metrics: log-loss heavily punishes confident mistakes; Brier is the mean squared error of probabilities. Both reward calibration (D0.10). Their raw values also shrink when one class dominates.
  • Multiclass: macro averages weight classes equally; micro averages weight examples; weighted averages weight by class size.
🐍 Exercise 1: classification metrics from scratch
Compute the confusion matrix, accuracy, precision, recall, F1, MCC and Cohen's κ; build ROC curves and AUC with tied scores grouped; compute average precision, log-loss and the Brier score; check that AUC equals the pairwise ranking probability and that an all-negative classifier gets F1, MCC and κ of zero.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Which metric is unchanged when positives become rarer (same ranking quality)?
  2. 2. PR-AUC of a random scorer with 5% positives?
  3. 3. Which metric ignores true negatives?

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Choosing metrics

  • Balanced classes, equal costs: accuracy is fine; add ROC-AUC for ranking quality.
  • Rare positives: report precision and recall at the operating threshold, PR-AUC and MCC. Never report accuracy alone.
  • Unequal costs: choose the threshold from the costs (D0.9) and report expected cost.
  • Probabilities used downstream: log-loss or Brier, plus calibration (D0.10).
  • Always show the confusion matrix and compare with trivial baselines: all-negative, all-positive, prevalence-rate random.

Interview questions.

  • Why can ROC-AUC look excellent for a fraud model that is unusable? With 0.1% fraud, even a small FPR means false alarms far outnumber true frauds, so precision is tiny. Use PR-AUC.
  • What is the difference between macro and micro F1? Macro averages per-class F1, so it treats classes equally; micro pools counts, so it treats examples equally.
📝 Quick check (Level 3)
0/3 answered
  1. 1. With 0.1% positives, FPR = 1% and TPR = 90%, precision is about…
  2. 2. Macro-averaged F1 treats…
  3. 3. For a rare-event detector, a key baseline is…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Imbalance changes the scoreboard

Exercise 1. For 10 cases with TP = 3, FP = 1, FN = 1, TN = 5:

Metric Value
accuracy 0.80
precision, recall and F1 0.75 each
MCC and κ 0.583 each
ROC-AUC (from scores) 0.833
average precision 0.830

Predicting all negatives scores accuracy 0.6 but F1 = MCC = κ = 0.

Exercise 2: one classifier, different class balances. Setup:

  • scores are N(0, 1) for negatives and N(2, 1) for positives, so the ranking ability is fixed;
  • probabilities are the exact Bayes posteriors, thresholded at 0.5;
  • 200 000 test cases per prevalence.
Positives Accuracy All-negative accuracy Precision Recall F1 Best-threshold F1 MCC ROC-AUC PR-AUC Log-loss
50% 0.841 0.498 0.843 0.841 0.842 0.845 0.683 0.921 0.922 0.357
20% 0.889 0.801 0.777 0.619 0.689 0.707 0.628 0.921 0.785 0.268
5% 0.959 0.950 0.706 0.310 0.431 0.519 0.451 0.922 0.531 0.118
1% 0.991 0.990 0.670 0.092 0.162 0.320 0.246 0.917 0.262 0.037
0.2% 0.998 0.998 0.286 0.005 0.010 0.148 0.038 0.909 0.075 0.011
  • ROC-AUC stays at about 0.92 throughout.
  • Accuracy rises to 0.998 while the classifier finds 0.5% of the positives. It merely tracks the all-negative baseline.
  • Log-loss also “improves” tenfold, because most examples are easy negatives.
  • PR-AUC, F1 and MCC fall with prevalence. They reveal that, at 1% positives, a good ranker still produces mostly false alarms or misses.
  • The default threshold of 0.5 maximises accuracy, not F1. Choosing the threshold for F1 doubles it at 1% prevalence (0.162 → 0.320).
🐍 Exercise 2 (research): metrics under class imbalance
Simulate a classifier with fixed Gaussian score separation at prevalences from 50% to 0.2%; compute accuracy, the all-negative baseline, precision, recall, F1 (at 0.5 and at the best threshold), MCC, κ, ROC-AUC, average precision and log-loss on 200 000 cases each.

Classification metrics in AI, robotics and aerospace

  • Fault detection: recall at a fixed false-alarm rate per hour of operation, plus PR-AUC.
  • Object detection: mean average precision (mAP) over classes and IoU thresholds.
  • Collision warning: high recall is mandatory; precision governs operator trust.
  • Inter-annotator agreement: Cohen’s κ for labelling quality of training data.

References

  1. T. Fawcett (2006). “An introduction to ROC analysis.” Pattern Recognition Letters 27(8).
  2. T. Saito, M. Rehmsmeier (2015). “The precision-recall plot is more informative than the ROC plot when evaluating binary classifiers on imbalanced datasets.” PLoS ONE 10(3).
  3. D. Chicco, G. Jurman (2020). “The advantages of the Matthews correlation coefficient over F1 score and accuracy.” BMC Genomics 21.
  4. J. Cohen (1960). “A coefficient of agreement for nominal scales.” Educational and Psychological Measurement 20(1).
  5. J. Davis, M. Goadrich (2006). “The relationship between precision-recall and ROC curves.” ICML.