Skip to content
mentoor

Topic 2 of 6 · 5 min read

F1, ROC-AUC and precision-recall curves

Goal: Pick a decision threshold from a precision-recall curve and explain why accuracy misleads on imbalanced data.

F1 and ROC-AUC compress a whole classifier into one number, but a single number hides the threshold you will actually deploy, and on rare-positive data some of them flatter the model. Use curves to pick a threshold, and use summaries only to compare.

Check yourself · 1 of 3

What does F1 combine?

Choose one answer

Why it matters

Most models output scores, not decisions. The score-to-decision cut-off is a product choice. A summary metric that ignores it can rank models by something you will never ship.

The tools

  • F1 is the harmonic mean of precision and recall: 2 * p * r / (p + r). It is low if either is low, so it punishes lopsided models. It still assumes precision and recall matter equally, and it ignores true negatives.
  • ROC curve plots true positive rate (recall) against false positive rate (FP / (FP + TN)) over all thresholds. ROC-AUC is the area under it: the chance a random positive scores above a random negative.
  • Precision-recall (PR) curve plots precision against recall over all thresholds. Average precision summarises it.

Why accuracy and ROC-AUC mislead on rare positives

Suppose 1 in 1,000 transactions is fraud. Of 100,000 transactions, 100 are fraud and 99,900 are not. A false positive rate of 1% sounds tiny and gives a lovely ROC curve, but it means 999 false alarms against at most 100 real hits. Precision is then under 10%. FPR divides by a huge negative count, so it hides the pain. The PR curve uses precision, which sees those 999 alarms directly.

Picking a threshold

from sklearn.metrics import precision_recall_curve
p, r, t = precision_recall_curve(y_true, scores)
# need recall >= 0.9, then take the best precision
ok = r[:-1] >= 0.9
best = t[ok][p[:-1][ok].argmax()]

State the requirement first ("catch at least 90% of fraud"), then read the threshold off the curve. Do not choose the threshold that maximises F1 by default unless equal weight on precision and recall is really your cost.

Before and after

Before: "Model B has ROC-AUC 0.97, ship it." After: "At the threshold that gives 90% recall, Model B has 12% precision; Model A has 31%. A's PR curve is higher in the region we operate in."

What goes wrong

  • Optimising F1 when costs are unequal. A missed cancer and a needless retest are not the same cost.
  • Choosing the threshold on the test set. Pick it on validation data, then report on test.
  • Comparing AUC across different datasets. Class balance changes PR curves, so compare on the same data.
  • Trusting a high ROC-AUC alone. It says the ranking is good overall, not that the top of the ranking is precise enough.

Examples

The 99.9% accurate model

In a dataset with 1 fraud in 1,000 transactions, a model that always answers "not fraud" is 99.9% accurate and has recall 0. Accuracy rewarded it for ignoring the only thing that matters. Recall and precision on the fraud class expose it immediately.

Threshold from the curve

A PR table for a churn model: threshold 0.7 gives precision 0.62 and recall 0.40; threshold 0.5 gives 0.48 and 0.66; threshold 0.3 gives 0.31 and 0.85. The retention team can call 500 customers a week. The threshold that flags about 500 is the one to use, whatever its F1 is.

Key points

  • F1 is the harmonic mean of precision and recall, so one weak side pulls it down, and it assumes both matter equally.
  • ROC-AUC measures ranking quality across all thresholds, not the quality of any one decision.
  • With rare positives, false positive rate hides a flood of false alarms; precision and the PR curve show it.
  • Choose the threshold from a stated requirement on validation data, then report on held-out test data.
  • Accuracy on imbalanced data can be high for a model that never predicts the rare class.

Common mistakes

  • Tuning the threshold on the test set and then reporting the test score.
  • Maximising F1 by default when a miss and a false alarm have very different costs.
  • Reading a high ROC-AUC as proof of high precision on a rare class.
  • Comparing PR curves from datasets with different positive rates.

Check yourself · 2 of 3

Only 0.5% of loan applications default. Why can a model have 99.5% accuracy and still be useless?

Choose one answer

Check yourself · 3 of 3

You must catch at least 90% of fraud and want the fewest false alarms. How do you pick the threshold?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic