Sub topic 11
Class imbalance and precision-recall curves
This chapter assumes you can build a ROC curve and read TPR, FPR, and AUC. It also assumes you know the confusion matrix terms and can write basic NumPy and scikit-learn code. Here you will see why ROC AUC can look good when a classifier is poor on rare positives, and how a precision-recall (PR) curve shows the problem.
Why ROC hides the problem
FPR is false positives divided by all negatives. When negatives vastly outnumber positives, a large number of false positives still gives a tiny FPR. The ROC curve barely moves, but the people reviewing the alerts see many wrong ones.
Precision answers a different question: of the examples predicted positive, how many are truly positive?
- Precision = TP / (TP + FP)
- Recall = TP / (TP + FN), which is the same as TPR
Precision uses false positives in the numerator's competitor, not in a ratio against all negatives. So it grows worse as negatives pile up.
A worked example
Suppose a dataset has 100 positives and 9,900 negatives (1% prevalence). A model at one threshold has TPR 0.80 and FPR 0.01. That looks excellent on a ROC plot.
n_pos, n_neg = 100, 9900
tpr, fpr = 0.80, 0.01
tp = tpr * n_pos # 80
fp = fpr * n_neg # 99
precision = tp / (tp + fp)
print(round(precision, 3)) # 0.447More than half of the flagged cases are false alarms. Now keep TPR and FPR fixed but change the class mix:
| Positives | Negatives | TPR | FPR | Precision |
|---|---|---|---|---|
| 5,000 | 5,000 | 0.80 | 0.01 | 0.988 |
| 100 | 9,900 | 0.80 | 0.01 | 0.447 |
TPR and FPR are the same in both rows. Only precision reflects the imbalance.
Computing PR curves with scikit-learn
precision_recall_curve sweeps thresholds like roc_curve. average_precision_score summarizes the curve as a weighted mean of precisions across thresholds.
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,
)
from sklearn.model_selection import train_test_split
X, y = make_classification(
n_samples=20000, n_features=10, n_informative=4,
weights=[0.99, 0.01], random_state=0,
)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=0, stratify=y
)
scores = LogisticRegression(max_iter=1000).fit(X_train, y_train).predict_proba(X_test)[:, 1]
precision, recall, thresholds = precision_recall_curve(y_test, scores)
print("ROC AUC:", roc_auc_score(y_test, scores))
print("Average precision:", average_precision_score(y_test, scores))
print("Baseline (prevalence):", y_test.mean())Compare average precision to the prevalence, not to 0.5. A random scorer has average precision close to the prevalence. In ROC space, a random scorer sits on the diagonal and has AUC 0.5 whatever the mix.
Reading a PR curve
- The ideal point is the top right: precision 1 and recall 1.
- The curve is not monotone. Precision can rise and fall as the threshold changes.
- Do not linearly interpolate between PR points to compute area. That is why
average_precision_scoreuses a step sum instead of the trapezoidal rule. - Because
precision_recall_curvereturns one more precision and recall value than thresholds, the arrays differ in length by one.
When to prefer which
- Use ROC AUC when you care about ranking across both classes and the class mix is moderate or will change.
- Use PR curves or average precision when positives are rare and you care about the quality of the flagged set.
Exercise
A model has TPR 0.90 and FPR 0.02 on a test set with 50 positives and 9,950 negatives. What is its precision at this threshold?
Show answer
TP = 0.90 × 50 = 45. FP = 0.02 × 9,950 = 199. Precision = 45 / (45 + 199) ≈ 0.184. Despite a strong-looking ROC point, about 82% of flagged cases are false positives.