
ليه الـ predict_proba مش احتمال حقيقي؟ يعني إيه Calibration
- كاتب موثّق
- scikit-learn 1.9
محتويات المقال
الفكرة الغلط الشائعة
predict_proba على إنه فرصة إن التوقع صح: الـ 0.8 يعني فرصة 80%. وده سهل تفهم جه منين:
الـ method اسمها حرفيًا predict_proba، واللي بترجّعه بيتصرّف زي الاحتمالات: بين 0 و1، ومجموعه 1 على كل الـ classes.الـ logistic regression، أول classifier أغلب الناس بيتعلّموه، غالبًا بيدّي احتمالات calibrated كويس، فالعادة بتتكوّن بدري. الـ accuracy والـ ROC AUC الكويسين بيدّوا إحساس إن كل اللي الموديل بيطلّعه مضمون.
اللي بيحصل فعلًا
الـ calibration كلام عن مجموعات
predict_proba قريب من 0.8، حوالي 80% منها فعلًا positive. ده كلام عن مجموعة حالات متقارنة بالنتايج، عمره ما بيبقى عن حالة واحدة. الـ 0.8 الـ calibrated معناها "8 من كل 10 حالات زي دي". وكمان مابتقولش حاجة عن فايدة الموديل: موديل بيدّي كل حالة الـ base rate هيبقى تمامًا ومالوش أي لازمة. الترتيب والـ calibration جودتين منفصلين، والموديل ممكن يبقى عنده واحدة من غير التانية.
ليه الـ scores بتبعد عن النسب الحقيقية
الـ Naive Bayes بيفترض إن الـ features مستقلة عن بعض لما الـ class تبقى معروفة. ولما كذا feature يشيلوا نفس الدليل، بيعدّ الدليل ده كذا مرة، فبيزق الـ scores ناحية 0 و1. الـ Max-margin methods زي الـ SVMs والـ boosted trees بتبعّد الـ scores عن 0 و1، تشويه على شكل sigmoid وصفه Niculescu-Mizil وCaruana؛ والـ guide بتاع scikit-learn بيوري الـ random forests نادرًا ما بتوصل لـ 0 أو 1. الـ Neural networks الحديثة غالبًا الـ calibration بتاعها وحش، زي ما لقى Guo وزمايله، وبيربطوا ده بالعمق والعرض والـ weight decay والـ batch normalization. الـ Class weights والـ resampling بيغيّروا توازن الـ classes اللي الموديل بيتعلّم منه، فالـ scores بتوصف العالم المتظبّط ده، مش عالمك إنت.
from sklearn.calibration import calibration_curvefrom sklearn.datasets import make_classificationfrom sklearn.metrics import brier_score_loss, roc_auc_scorefrom sklearn.model_selection import train_test_splitfrom sklearn.naive_bayes import GaussianNB# 2 informative features plus 10 redundant mixes of themX, y = make_classification(n_samples=20_000, n_features=20, n_informative=2, n_redundant=10, random_state=11)X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.5, random_state=0)proba = GaussianNB().fit(X_tr, y_tr).predict_proba(X_te)[:, 1]low = proba < 0.05 # auto-dismissed as "under 5% likely"print(f"under 5%: {low.sum()} reports, {y_te[low].mean():.1%} violating")share, score = calibration_curve(y_te, proba, n_bins=5)print("mean score ", score.round(2))print("share positive", share.round(2))auc, brier = roc_auc_score(y_te, proba), brier_score_loss(y_te, proba)print(f"ROC AUC {auc:.3f}, Brier {brier:.3f}")# under 5%: 5078 reports, 13.7% violating# mean score [0.01 0.3 0.5 0.71 0.99]# share positive [0.16 0.55 0.57 0.7 0.93]# ROC AUC 0.934, Brier 0.121from sklearn.calibration import CalibratedClassifierCV, calibration_curvefrom sklearn.datasets import make_classificationfrom sklearn.metrics import brier_score_loss, roc_auc_scorefrom sklearn.model_selection import train_test_splitfrom sklearn.naive_bayes import GaussianNBX, y = make_classification(n_samples=20_000, n_features=20, n_informative=2, n_redundant=10, random_state=11)X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.5, random_state=0)model = CalibratedClassifierCV(GaussianNB(), method="isotonic", cv=5)proba = model.fit(X_tr, y_tr).predict_proba(X_te)[:, 1]low = proba < 0.05print(f"under 5%: {low.sum()} reports, {y_te[low].mean():.1%} violating")share, score = calibration_curve(y_te, proba, n_bins=5)print("mean score ", score.round(2))print("share positive", share.round(2))auc, brier = roc_auc_score(y_te, proba), brier_score_loss(y_te, proba)print(f"ROC AUC {auc:.3f}, Brier {brier:.3f}")# under 5%: 1839 reports, 4.7% violating# mean score [0.08 0.3 0.52 0.75 0.95]# share positive [0.08 0.31 0.52 0.75 0.95]# ROC AUC 0.934, Brier 0.098الـ CalibratedClassifierCV بيتعلّم إزاي
cv=5، بيعمل fit لنسخة من الـ classifier على أربع أخماس داتا الـ training، وبيعمل fit للـ calibrator على الخمس الباقي، خمس مرات، وبعدين بياخد متوسط الاحتمالات بتاعة الخمس موديلات. يعني الـ calibrator عمره ما بيشوف scores لصفوف الـ classifier اتدرّب عليها. واختار الطريقة على حسب الداتا بتاعتك:
الـ "sigmoid"هي طريقة Platt: logistic regression على الـ scores، بـ parameters قليلة، كويسة مع الداتا الصغيرة والتشويه اللي على شكل sigmoid.الـ "isotonic"بتصلّح أي تشويه monotonic، بس الـ API reference بيحذّر إنها بتعمل overfitting لو عينات الـ calibration أقل بكتير من 1,000.الـ "temperature"، اللي اتضافت في scikit-learn 1.8، بتقسم الـ logits على قيمة واحدة بتتعلّمها، وبتدعم الـ multiclass من غير لف ودوران.
predict_proba بيرجّع احتمالات | |
تطبقها إزاي في مشروعك
قرّر إنت محتاج احتمالات أصلًا ولا لأ الترتيب، أو cutoff متظبّط على داتا validation، بيشتغلوا مع أي scores. أما القيم المتوقعة، والثقة اللي بتظهر للمستخدم، والقواعد الثابتة على الاحتمال، فمحتاجين calibration. ارسم المنحنى على داتا held-out استخدم calibration_curveمع عدد الحالات في كل bin، للموديل بالظبط زي ما هو شغال في الـ production.اعمل calibration بـ CalibratedClassifierCV استخدم الـ sigmoid مع sets الـ calibration الصغيرة، والـ isotonic لو عندك أكتر من حوالي 1,000 عيّنة، والـ temperature للـ multiclass. قيّم على داتا الـ calibrator ماشافهاش اعرض المنحنى مع الـ log loss أو الـ Brier score من test split منفصل. راجع بعد ما الدنيا تتغيّر base rate جديد، أو سوق جديد، أو training من جديد بـ class weights، ممكن يبوّظوا الـ calibration من غير ما حد يحس. ارسم المنحنى على داتا حديثة.
الخلاصة
الـ predict_probaبيدّي scores؛ والـ 0.8 معناها 80% بس لو الموديل calibrated، ودي حاجة بتقيسها.الـ calibration كلام عن مجموعات: من كل الحالات اللي خدت 0.8، المفروض حوالي 80% يطلعوا positive. الـ naive Bayes والـ max-margin والـ deep networks والـ class weights كلهم بيشوّهوا الـ scores؛ وهنا "أقل من 5%" طلعت 13.7%. الـ CalibratedClassifierCVصلّحها من غير ما يغيّر الترتيب: الـ ROC AUC فضل 0.934.
📚 المصادر
Probability calibration — scikit-learn User Guide Probability Calibration curves — scikit-learn examples CalibratedClassifierCV — scikit-learn API Reference Niculescu-Mizil and Caruana, Predicting Good Probabilities With Supervised Learning (ICML 2005) Guo et al., On Calibration of Modern Neural Networks (ICML 2017)
التعليقات (0)