
ROC AUC Can Look Great on Rare Positives: When to Use Precision-Recall Curves
- كاتب موثّق
محتويات المقال
The common misconception
It needs no threshold and has an elegant meaning. Google's Machine Learning Crash Course describes it as the probability that the model ranks a random positive above a random negative. Chance is always 0.5, whatever the class balance, so a 0.93 always sounds excellent. It doesn't move with the positive rate, which makes scores comparable across datasets. That really is a strength.
What actually happens
ROC divides by the negatives, precision by the alarms
Same model, same scores, different class mix
from sklearn.datasets import make_classificationfrom sklearn.ensemble import HistGradientBoostingClassifierfrom sklearn.metrics import roc_auc_scorefrom sklearn.model_selection import train_test_splitX, y = make_classification(n_samples=100_000, n_features=20, n_informative=6, weights=[0.99], flip_y=0, class_sep=1.0, random_state=0) # 1% of transactions are fraudX_tr, X_te, y_tr, y_te = train_test_split(X, y, stratify=y, random_state=0)model = HistGradientBoostingClassifier(random_state=0).fit(X_tr, y_tr)score = model.predict_proba(X_te)[:, 1]print(f"ROC AUC {roc_auc_score(y_te, score):.3f}")# ROC AUC 0.933import numpy as npfrom sklearn.datasets import make_classificationfrom sklearn.ensemble import HistGradientBoostingClassifierfrom sklearn.metrics import average_precision_score, precision_recall_curvefrom sklearn.metrics import roc_auc_scorefrom sklearn.model_selection import train_test_splitX, y = make_classification(n_samples=100_000, n_features=20, n_informative=6, weights=[0.99], flip_y=0, class_sep=1.0, random_state=0)X_tr, X_te, y_tr, y_te = train_test_split(X, y, stratify=y, random_state=0)model = HistGradientBoostingClassifier(random_state=0).fit(X_tr, y_tr)score = model.predict_proba(X_te)[:, 1]rng = np.random.default_rng(0) # same scores, rebalanced test setpos, neg = np.flatnonzero(y_te == 1), np.flatnonzero(y_te == 0)half = np.concatenate([pos, rng.choice(neg, size=len(pos), replace=False)])for name, rows in [("1% fraud ", slice(None)), ("50% fraud", half)]: y_true, s = y_te[rows], score[rows] precision, recall, _ = precision_recall_curve(y_true, s) print(f"{name}: ROC AUC {roc_auc_score(y_true, s):.3f}" f" AP {average_precision_score(y_true, s):.3f}" f" precision@80%recall {precision[recall >= 0.8].max():.2f}")# 1% fraud : ROC AUC 0.933 AP 0.494 precision@80%recall 0.09# 50% fraud: ROC AUC 0.929 AP 0.939 precision@80%recall 0.90Read average precision against its baseline
average_precision_score, not auc on the curve's points. The guide warns that the linear interpolation auc uses is overly optimistic for precision-recall curves.
When ROC AUC is still the right tool
How to apply it in your project
Write down the real positive rate Measure it from recent production data; every precision number depends on it. Plot both curves Use RocCurveDisplayandPrecisionRecallDisplaywithplot_chance_level=True, so each curve shows its own baseline.Report AP next to ROC AUC Give average precision with its chance level, the positive rate, alongside ROC AUC. Fix the operating point State the recall you need, or the queue you can staff, and report the precision there. Test on the production mix Keep the test set's class balance as it is in production.
Key takeaways
ROC AUC divides false positives by all negatives, so it barely reacts to how rare positives are. The same scores gave ROC AUC 0.933 and 0.929, but precision at 80% recall of 0.09 and 0.90. Average precision's chance level is the positive rate; read it against that, on the real class mix. Use ROC AUC for prevalence-free ranking and precision-recall when you act on the top of the list.
📚 المصادر
Classification: ROC and AUC — Google Machine Learning Crash Course Saito and Rehmsmeier, The Precision-Recall Plot Is More Informative than the ROC Plot When Evaluating Binary Classifiers on Imbalanced Datasets (PLOS ONE, 2015) McDermott et al., A Closer Look at AUROC and AUPRC under Class Imbalance (2024) Precision-Recall — scikit-learn examples Metrics and scoring: quantifying the quality of predictions — scikit-learn User Guide
التعليقات (0)