
ليه محتاج test set متلمسهاش خالص؟ تقسيم الـ Train والـ Validation والـ Test
- كاتب موثّق
محتويات المقال
الفكرة الغلط الشائعة
أول قاعدة الكل بيتعلّمها هي "ماتدرّبش على الـ test set"، والطريقة دي ماشية عليها بالحرف: الـ fitمابيشوفش غير صفوف الـ training.قراية الـ score بتحسسك إنها مجرد قراية. مفيش حاجة في الموديل بتتغيّر لما تطبع الـ accuracy بتاعته على الـ test. إنك تحتفظ بأعلى score شكله منطق عادي، ولو الداتا صغيرة، تقسيمة تالتة بتبان كإنها تضييع داتا.
اللي بيحصل فعلًا
fit يرجع. اختيار عمق الـ tree، أو مجموعة الـ features، أو خطوة preprocessing، أو نوع الموديل، كلها قرارات برضه، وبتتعلّم من أي داتا بتحكم عليها بيها.
الاختيار جزء من الـ training
fit يلمس الـ test خالص.
لما تختار الأعلى، بتختار الحظ
import numpy as npfrom sklearn.model_selection import ParameterGrid, train_test_splitfrom sklearn.tree import DecisionTreeClassifierrng = np.random.default_rng(3)X = rng.normal(size=(11_000, 20))y = rng.integers(0, 2, size=11_000) # coin-flip labels: nothing to learnX, X_new, y, y_new = train_test_split(X, y, train_size=1000, random_state=0)X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=200, random_state=0)grid = {"max_depth": range(1, 11), "min_samples_leaf": [1, 5, 10, 20, 40], "max_features": [2, 5, 10, 20]} # 200 combinationsresults = []for params in ParameterGrid(grid): tree = DecisionTreeClassifier(**params, random_state=0).fit(X_tr, y_tr) results.append((tree.score(X_te, y_te), tree)) # judged on the test setscore, best = max(results, key=lambda r: r[0])print(f"test accuracy {score:.3f}")print(f"new data accuracy {best.score(X_new, y_new):.3f}")# test accuracy 0.610# new data accuracy 0.503import numpy as npfrom sklearn.model_selection import ParameterGrid, train_test_splitfrom sklearn.tree import DecisionTreeClassifierrng = np.random.default_rng(3)X = rng.normal(size=(11_000, 20))y = rng.integers(0, 2, size=11_000)X, X_new, y, y_new = train_test_split(X, y, train_size=1000, random_state=0)X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=200, random_state=0)X_tr, X_val, y_tr, y_val = train_test_split(X_tr, y_tr, test_size=200, random_state=0) # 600 / 200 / 200grid = {"max_depth": range(1, 11), "min_samples_leaf": [1, 5, 10, 20, 40], "max_features": [2, 5, 10, 20]}results = []for params in ParameterGrid(grid): tree = DecisionTreeClassifier(**params, random_state=0).fit(X_tr, y_tr) results.append((tree.score(X_val, y_val), tree)) # judged on validationscore, best = max(results, key=lambda r: r[0])print(f"validation accuracy {score:.3f}") # used to choose: optimisticprint(f"test accuracy {best.score(X_te, y_te):.3f}") # looked at onceprint(f"new data accuracy {best.score(X_new, y_new):.3f}")# validation accuracy 0.555# test accuracy 0.505# new data accuracy 0.498تلات مجموعات، لكل واحدة شغلانة
الـ training set: الموديل بيتعلّم منها الـ weights بتاعته. الـ validation set: بتقارن عليها الموديلات والـ ، براحتك وبأي عدد مرات. الـ test set: بتأكّد عليها الاختيار النهائي، في الآخر.
الـ cross-validation بتاخد مكان الـ validation set، مش الـ test set
GridSearchCV: اعمل الـ search على development set، واحسب الـ metrics النهائية على evaluation set منفصلة ماشافهاش.
fit مابيشوفهاش | |
تطبقها إزاي في مشروعك
افصل الـ test set الأول شيلها على جنب قبل ما تستكشف أو ترسم أو تعمل tuning. والـ Crash Course بيقول كمان امسح أمثلة الـ test المكررة من الـ training: الاختبار العادل الوحيد بيبقى على أمثلة جديدة. اعمل كل اختياراتك على داتا validation استخدم تقسيمة validation، أو cross-validation على جزء الـ training، للـ hyperparameters والـ features والـ preprocessing والـ thresholds. ثبّت، وبعدين اختبر مرة واحدة ثبّت الـ pipeline كله بإعداداته، وبعدين قيس على الـ test set وقدّم الرقم ده، مش الـ score بتاع الـ validation. ماتعملش tuning بعد الـ test لو الـ score على الـ test خيّب أملك، ارجع اشتغل عادي، بس الـ test set القديمة بقت validation set. احكم على النسخة الجاية بداتا جديدة. سجّل كل بصّة خلّي فيه سجل لكل تقييم على الـ test، عشان الفريق يعرف الـ test set اتهلكت قد إيه.
الخلاصة
إنك ماتدرّبش على الـ test set مش كفاية: ماتختارش على أساسها كمان. لما تحتفظ بأحسن واحد من موديلات كتير، الـ score بتاعه بيتنفخ؛ موديل على labels عشوائية جاب 0.610 على الـ test و0.503 على داتا جديدة. اتعلّم على الـ training set، واختار على داتا validation أو بالـ cross-validation، وأكّد على الـ test set مرة واحدة. الـ cross-validation بتاخد مكان الـ validation set مش الـ test set، والـ test set اللي بتفضل تعيد استخدامها بتتهلك.
التعليقات (0)