
إمتى الـ ROC AUC يخدعك؟ ولما الـ positives نادرة تستخدم Precision-Recall
- كاتب موثّق
محتويات المقال
الفكرة الغلط الشائعة
مش محتاج threshold ومعناه أنيق. الـ Machine Learning Crash Course بتاع Google بيوصفه إنه احتمال إن الموديل يرتّب positive عشوائي فوق negative عشوائي. الصدفة دايمًا 0.5، مهما كان توزيع الـ classes، فالـ 0.93 دايمًا بتبان ممتازة. مابيتحركش مع نسبة الـ positives، وده بيخلّي الـ scores ممكن تتقارن بين datasets مختلفة. ودي ميزة حقيقية فعلًا.
اللي بيحصل فعلًا
الـ ROC بيقسم على الـ negatives، والـ precision على الإنذارات
نفس الموديل، نفس الـ scores، خلطة مختلفة
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.90اقرا الـ average precision قصاد الـ baseline بتاعه
average_precision_score، مش بـ auc على نقط المنحنى. الـ guide بيحذّر إن الـ linear interpolation اللي auc بيستخدمه متفائل زيادة عن اللزوم مع منحنيات الـ precision-recall.
إمتى الـ ROC AUC يفضل هو الأداة الصح
تطبقها إزاي في مشروعك
اكتب نسبة الـ positives الحقيقية قيسها من داتا production حديثة؛ كل رقم precision معتمد عليها. ارسم المنحنيين استخدم RocCurveDisplayوPrecisionRecallDisplayمعplot_chance_level=True، عشان كل منحنى يوري الـ baseline بتاعه.قدّم الـ AP جنب الـ ROC AUC اكتب الـ average precision ومعاه مستوى الصدفة بتاعه، اللي هو نسبة الـ positives، جنب الـ ROC AUC. ثبّت نقطة التشغيل حدّد الـ recall اللي محتاجه، أو الطابور اللي تقدر توفّرله ناس، وقدّم الـ precision عندها. اختبر على خلطة الـ production خلّي توزيع الـ classes في الـ test set زي ما هو في الـ production.
الخلاصة
الـ ROC AUC بيقسم الـ false positives على كل الـ negatives، فبالعافية بيتأثر بندرة الـ positives. نفس الـ scores جابت ROC AUC قدّه 0.933 و0.929، بس precision عند recall قدّه 80% قدّها 0.09 و0.90. مستوى الصدفة بتاع الـ average precision هو نسبة الـ positives؛ اقراه قصادها، وعلى خلطة الـ classes الحقيقية. استخدم الـ ROC AUC للترتيب المستقل عن نسبة الـ positives، والـ precision-recall لما تتصرف على أول اللستة.
📚 المصادر
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)