Machine Learning · easy

Explain precision vs recall with an example.

Asked in Data Scientist interviews, in the Machine Learning round.

Short answer

Precision is the share of predicted positives that are correct, recall is the share of actual positives that were caught.

PrecisionRecall
FormulaTP / (TP + FP)TP / (TP + FN)
AnswersOf what I flagged, how much was rightOf what was real, how much did I find
Hurt byFalse positivesFalse negatives
Fraud exampleAnalysts wasting time on clean accountsFraud you never reviewed
Optimize it whenActing on a flag is expensiveMissing a case is expensive
Precision is how often an alarm is real. Recall is how many real ones you caught.

How to answer it

Definition

Two different questions about the same predictions. Precision asks how much of what you flagged was right; recall asks how much of what was real you caught. The table above compares them.

Formula

precision = TP / (TP + FP)      of what I flagged, how much was right
recall    = TP / (TP + FN)      of what was real, how much did I find

F1        = 2 * P * R / (P + R)     harmonic mean, equal weighting
F-beta    weights recall by beta; beta = 2 favors recall

Example

1,000 transactions, 50 are fraud. The model flags 40, of which 30 are fraud.

TP = 30      FP = 10      FN = 20

precision = 30 / 40 = 75%     three quarters of the alerts are real
recall    = 30 / 50 = 60%     but two fifths of the fraud was missed

The tradeoff

They move against each other as you slide the threshold. Lower it and you flag more, catching more fraud and raising recall while precision falls. Raise it and precision improves while cases slip through. No threshold maximises both, which is why the question is always which error is more expensive.

Optimize recall     missing a case is expensive
                    cancer screening, fraud, safety alerts

Optimize precision  acting on a false alarm is expensive
                    auto-blocking accounts, spam deletion, an offer with a
                    real cost attached

Key points

FAQ

The 30-second answer

Precision is how much of what I flagged was actually right. Recall is how much of what was really there I managed to catch.
With a fraud example: fifty real cases, the model flags forty, and thirty of those are genuine. Precision is thirty out of forty, seventy-five percent. Recall is thirty out of fifty, sixty percent. So three quarters of the alerts are real, and two fifths of the fraud still got through.
They trade off through the threshold, so the real question is which error costs more. For cancer screening or fraud I would favor recall, because missing a case is worse than an extra review. For something that automatically blocks an account I would favor precision, because a false positive hits a real customer.

In the interview

Practice this for real