MoonSay /Tutorials/Machine Learning Interview #12
Contents
  1. 01Introduction
  2. 02Accuracy lies
  3. 03Precision and recall
  4. 04F1, the harmonic mean
  5. 05ROC curve and AUC
  6. 06The precision-recall curve
  7. 07Interview follow-ups
  8. 08Common misconceptions
  9. 09Interview questions and model answers
  10. 10Summary
  11. References
Machine Learning Interview #12 · Tutorial

Precision, Recall, F1 and ROC-AUCWhich Metric When?

What each metric measures, how they are connected, and which one to use when

A tutorial companion to the video Machine Learning Interview #12: Precision, Recall, F1 and ROC-AUC: Which Metric When? PDF

01Introduction

1.1The question

“Explain precision, recall and ROC-AUC. When would you use each?” A classifier that outputs a score must eventually say yes or no: block the card or let the payment through, call the patient back or not, show the document or hide it. Each kind of mistake has a different price, and the positives we care about are often rare: frauds, tumors, defective parts, relevant web pages. The single number most people reach for, accuracy, is then almost useless, and the other metrics each answer a different question. Knowing which question you are asking, and what each metric ignores, is what interviewers want to hear.

1.2Prerequisites and notation

Prerequisites. Conditional probability and Bayes’ rule, and logistic regression as a model that outputs a probability. Episode 8 (cross-entropy and log loss) and Episode 11 (validation folds and a locked test set) are used in Chapter 7.

Notation. Each example has a label y\in\{0,1\}, where y=1 is the positive class (here: fraud), and a score s(x); for a probabilistic model s(x)=p(y=1\mid x), written p. A threshold \tau (the Greek letter tau) turns the score into a decision: the model flags the example, written \hat y=1 (the hat marks a prediction), when s(x)\ge\tau, and passes it, \hat y=0, otherwise. There are n_+ positives and n_- negatives, and the prevalence is \pi=n_+/(n_++n_-). On a labeled set, \mathrm{TP}, \mathrm{FP}, \mathrm{FN} and \mathrm{TN} count the true positives (frauds flagged), false positives (legitimate transactions flagged), false negatives (frauds missed) and true negatives (legitimate transactions passed); they depend on \tau. P and R denote precision and recall.

1.3A short history

The two families of metrics grew up in two different fields. The ROC curve comes from signal detection theory, the study of how a receiver, a radar operator or a human observer separates a signal from noise; the name receiver operating characteristic survives from there. Swets (1988) brought it to the evaluation of diagnostic systems, and Hanley and McNeil (1982) showed that the area under the curve equals the probability that a randomly chosen diseased patient is rated above a randomly chosen healthy one, which is the statistic of the rank test of Mann and Whitney (1947). Bradley (1997) and Fawcett (2006) established the ROC curve and its area in machine learning. Precision and recall come from information retrieval, where a search system returns a set of documents and only a small fraction of the collection is relevant. The Cranfield experiments of the 1960s measured both and found that one could only be raised at the expense of the other (Cleverdon et al., 1966). van Rijsbergen (1979) combined them into his effectiveness measure E; one minus it is today’s F score, a weighted harmonic mean (Manning et al., 2008, §8.3). Davis and Goadrich (2006) worked out how ROC and precision–recall curves are related. The textbook treatment used here is Murphy (2022, §2.3.1, §5.1.2–5.1.4). The data are the card transactions released by Dal Pozzolo et al. (2015). All numbers in this tutorial were computed for this tutorial with the same data and model as the companion video.