
ليه الـ Scaling قبل تقسيم البيانات بيسرّب الـ test set؟ أهمية الـ Pipelines
- كاتب موثّق
- scikit-learn 1.9
محتويات المقال
الفكرة الغلط الشائعة
الـ preprocessing بيبان كإنه تنضيف مش تعلّم. متوسط العمود والانحراف المعياري بتاعه مش شكلهم موديل خالص. tutorials كتير بتنادي fit_transformعلىXكلها في أول cell وبتقسم بعدين، والكود بيبقى أقصر.وفي حالات كتير الـ score فعلًا مابيتحركش. في الـ script اللي تحت، إنك تعمل scaling للداتا كلها الأول مابيغيّرش أي حاجة.
اللي بيحصل فعلًا
fit على داتا الـ test.
كل fit بيتعلّم حاجة
fit بتاع أي transformer بيقدّر أرقام من الصفوف اللي بيشوفها. الـ StandardScaler بيتعلّم متوسط كل عمود وانتشاره، والـ SimpleImputer بيتعلّم قيم الملء، والـ PCA بيتعلّم اتجاهات، والـ SelectKBest بيتعلّم أنهي أعمدة مرتبطة أكتر بالـ y. في الـ production، ولا رقم من دول ممكن ييجي من requests بكرة. ولو جت من صفوف الـ test وقت التقييم، يبقى التقييم بيوصف موديل مش هتقدر تعمله deploy أصلًا.
اللي بيتسرّب بيعتمد على الخطوة بتقرا إيه
import numpy as npfrom sklearn.feature_selection import SelectKBestfrom sklearn.linear_model import LogisticRegressionfrom sklearn.model_selection import cross_val_scorefrom sklearn.preprocessing import StandardScalerrng = np.random.default_rng(3)X = rng.normal(size=(300, 5000)) # 300 patients, 5,000 gene readingsy = rng.integers(0, 2, size=300) # coin-flip labels: nothing to learnX_all = StandardScaler().fit_transform(X) # fitted on every rowX_all = SelectKBest(k=20).fit_transform(X_all, y) # reads every labelscores = cross_val_score(LogisticRegression(), X_all, y, cv=5)print(f"accuracy {scores.mean():.3f}")# accuracy 0.777import numpy as npfrom sklearn.feature_selection import SelectKBestfrom sklearn.linear_model import LogisticRegressionfrom sklearn.model_selection import cross_val_scorefrom sklearn.pipeline import make_pipelinefrom sklearn.preprocessing import StandardScalerrng = np.random.default_rng(3)X = rng.normal(size=(300, 5000))y = rng.integers(0, 2, size=300)pipe = make_pipeline(StandardScaler(), SelectKBest(k=20), LogisticRegression())scores = cross_val_score(pipe, X, y, cv=5) # every step refit per foldprint(f"all steps in the pipeline: accuracy {scores.mean():.3f}")X_scaled = StandardScaler().fit_transform(X) # only the scaler outsidepipe = make_pipeline(SelectKBest(k=20), LogisticRegression())scores = cross_val_score(pipe, X_scaled, y, cv=5)print(f"scaler fitted on all rows: accuracy {scores.mean():.3f}")# all steps in the pipeline: accuracy 0.503# scaler fitted on all rows: accuracy 0.503SelectKBest رتّب 5,000 عمود عشوائي حسب قد إيه كل واحد ماشي مع الـ 300 label كلهم. ومن 5,000 عمود، فيه 20 هيمشوا معاهم بالصدفة البحتة، حتى على الصفوف اللي كل fold هيستخدمها بعدين للـ validation. فالموديل بـ"يتوقع" labels هي نفسها اللي اختارت الـ features بتاعته، ويطلّع 0.777 على ولا حاجة. والـ guide بتاع scikit-learn بيوري نفس التأثير بـ 10,000 feature عشوائية: 0.76 بدل 0.5.
الـ pipeline بيخلّي كل fold يعيد fit لكل حاجة
cross_val_score أو GridSearchCV مع الداتا الخام، وكل fold هيعمل fit لنسخة جديدة: كل transformer بيتعلّم من صفوف الـ training بتاعة الـ fold ده بس، وبعدين يحوّل صفوف الـ validation باللي اتعلّمه. والـ guide بتاع scikit-learn بيعتبر ده هدف الأمان بتاع الـ pipeline: إنه يمنع تسريب إحصائيات الـ test للموديل، لأن الـ transformers والـ predictor بيتدرّبوا على نفس الصفوف.
GridSearchCV يقدر يظبط الـ preprocessing والموديل مع بعض، بأسماء زي selectkbest__k. والـ pipeline بعد الـ fit بيشيل الـ scaler والـ imputer معاه للـ production، فالصفوف الجديدة بتتحوّل بالظبط زي وقت الـ training.
دي مش غلطة مبتدئين بس
fit بيتعلّم إيه | ||
|---|---|---|
StandardScaler | ||
SimpleImputer | ||
PCA | ||
SelectKBest | y | |
تطبقها إزاي في مشروعك
قسّم قبل ما تبص شيل الـ test set على جنب قبل أي رسم أو تنضيف أو اختيار. حط كل خطوة بتتعلّم جوه الـ pipeline الـ imputers والـ scalers والـ encoders والـ selectors والـ resamplers كلهم يدخلوا Pipelineواحد؛ واستخدمColumnTransformerلأنواع الأعمدة المختلفة.ادّي الـ cross-validation الداتا الخام ادّي الـ pipeline والـ Xمن غير تحويل لـcross_val_scoreأوGridSearchCV، عمرك ما تديله نسخة متحوّلة قبلها.اختبر التسريب شغّل اختبار الـ labels المتلخبطة كل ما الـ preprocessing يتغيّر. اعمل deploy للـ pipeline كله احفظ الـ pipeline كله بعد الـ fit واعمله deploy، عشان صفوف الـ production تتحوّل زي وقت الـ training بالظبط.
الخلاصة
كل fitبيتعلّم من الصفوف اللي بيشوفها؛ والـ fit قبل التقسيم بيخلّي صفوف الـ test تشكّل الموديل.الـ scaler اللي اتعمله fit على كل الصفوف غالبًا بيغيّر حاجة بسيطة، 0.503 في الحالتين هنا، وعشان كده العادة عايشة. الخطوات اللي بتقرا الـ labels بتسرّب جامد: الـ feature selection على كل الصفوف جاب 0.777 على labels عشوائية. حط كل خطوة بتتعلّم من الداتا جوه Pipeline، واعمل cross-validation للـ pipeline على الداتا الخام.
📚 المصادر
Common pitfalls and recommended practices — scikit-learn User Guide Pipelines and composite estimators — scikit-learn User Guide Kapoor and Narayanan, Leakage and the Reproducibility Crisis in ML-based Science (2022) Moscovich and Rosset, On the cross-validation bias due to unsupervised pre-processing (2022)
التعليقات (0)