Sub topic 4
Precision-recall and ROC curves, and when each is informative
This chapter assumes you can fit a scikit-learn classifier and read a confusion matrix with precision, recall and F1. It also assumes you know that predict_proba returns scores before any threshold is applied.
Why one threshold is not enough
Precision, recall and F1 describe a model at a single decision threshold. Most classifiers actually produce a score, and the threshold turns it into a label. A curve shows the model's behavior at every threshold, so you can compare models without committing to one cutoff.
The two curves
The ROC curve plots the true positive rate (recall) against the false positive rate (FPR). FPR is FP / (FP + TN), the share of negatives wrongly flagged. The area under it is ROC AUC. A random scorer gets 0.5.
The precision-recall (PR) curve plots precision against recall. Its summary is average precision (AP), a weighted sum of precision at each recall step. A random scorer gets an AP equal to the positive rate, not 0.5. With 5% positives, a random AP is about 0.05.
| Row | ROC curve | PR curve |
|---|---|---|
| Axes | FPR vs recall | Recall vs precision |
| Uses true negatives | Yes (in FPR) | No |
| Random baseline | 0.5 AUC | Positive rate |
| Sensitive to imbalance | Little | Strongly |
Why ROC can look too good
FPR divides by the number of negatives. When negatives vastly outnumber positives, many false positives still give a small FPR.
Suppose a test set has 2,375 negatives and 125 positives. At some threshold the model finds 100 positives and raises 100 false alarms. Recall is 0.80 and FPR is 100/2375, about 0.04. That point sits near the top-left corner of the ROC plot. Precision is 100/200 = 0.50, so half of all alerts are wrong. The PR curve shows this cost directly because precision uses false positives against true positives, not against all negatives.
Computing both
This example is self-contained. It fits a model and reports both summaries next to their baselines.
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import (
average_precision_score,
precision_recall_curve,
roc_auc_score,
roc_curve,
)
from sklearn.model_selection import train_test_split
X, y = make_classification(
n_samples=10000, n_features=20, n_informative=5,
weights=[0.95, 0.05], flip_y=0.01, random_state=42,
)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, stratify=y, random_state=42
)
model = LogisticRegression(max_iter=1000).fit(X_train, y_train)
scores = model.predict_proba(X_test)[:, 1]
print("ROC AUC:", roc_auc_score(y_test, scores))
print("average precision:", average_precision_score(y_test, scores))
print("random AP baseline:", y_test.mean())
fpr, tpr, _ = roc_curve(y_test, scores)
precision, recall, _ = precision_recall_curve(y_test, scores)Expect ROC AUC to look comfortably high while AP is noticeably lower. Both describe the same ranking. The gap comes from how each metric treats the many negatives.
Common errors
- Passing
predict()labels instead of scores toroc_auc_scoreoraverage_precision_score. Labels give a curve with one point and a misleading number. - Judging AP against 0.5. Compare it with the positive rate.
- Using
auc(recall, precision)and treating it as AP. Trapezoidal interpolation on a PR curve can be too optimistic. Useaverage_precision_score. - Comparing AP across datasets with different positive rates. AP depends on prevalence, so only compare models on the same test set.
Exercise
A fraud model has 0.98 ROC AUC and 0.30 average precision on a test set with 1% fraud. A colleague says the model is excellent because of the AUC. What do you reply, and what baseline do you compare AP with?
Show answer
The high AUC is partly an effect of the huge number of negatives, which keeps FPR small even when there are many false alarms. The AP of 0.30 says that at useful recall levels most alerts are wrong. Compare AP with the positive rate, 0.01. The model is 30 times better than random on AP, but it may still be too noisy for the business, so check precision at the recall you need.