
Why You Need a Test Set You Never Touch: Train, Validation and Test Splits
- كاتب موثّق
محتويات المقال
The common misconception
The first rule everyone learns is "don't train on the test set", and this workflow follows it to the letter: fitonly ever sees training rows.Reading a score feels read-only. Nothing about the model changes when you print its test accuracy. Keeping the best-scoring candidate sounds like common sense, and on a small dataset a third split feels like wasted data.
What actually happens
fit returns. Choosing a tree depth, a feature set, a preprocessing step or a model family are decisions too, and they're learned from whatever data you judge them on.
Choosing is part of training
fit.
Picking the maximum picks the luck
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.498Three sets, three jobs
Training set: the model learns its weights from it. Validation set: you compare models and on it, as often as you like. Test set: you confirm the final choice on it, at the end.
Cross-validation replaces the validation set, not the test set
GridSearchCV: run the search on a development set and compute the final metrics on a separate evaluation set it never saw.
fit never sees it | |
How to apply it in your project
Split off the test set first Hold it out before you explore, plot or tune. The Crash Course also says to delete test examples that duplicate training examples: the only fair test is on new examples. Make every choice on validation data Use a validation split, or cross-validation on the training part, for hyperparameters, features, preprocessing and thresholds. Freeze, then test once Fix the whole pipeline and its settings, then score the test set and report that number, not the validation score. Don't tune after the test If the test score disappoints, go back to work, but the old test set is now a validation set. Judge the next version on fresh data. Log every look Keep a record of each test evaluation, so the team knows how worn the test set is.
Key takeaways
Never training on the test set isn't enough: never choose by it either. Keeping the best of many candidates inflates its score; a coin-flip model reported 0.610 and got 0.503 on new data. Learn on the training set, choose on validation data or cross-validation, and confirm on the test set once. Cross-validation replaces the validation set, not the test set, and a test set you keep reusing wears out.
التعليقات (0)