الانتقال إلى المحتوى
Machine Learningتقييم الموديلات· 7 دقائق للقراءة

Why You Need a Test Set You Never Touch: Train, Validation and Test Splits

Picking the winner by its test score quietly turns the test set into training data. Choose on validation data, then test once.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

A team spends two weeks on a churn model. Every afternoon someone tries a few new settings, checks accuracy on the held-out test set and keeps the winner. The model never trains on a single test row, so the final number goes into the launch deck with confidence. A month after launch, live accuracy is lower than promised and nobody can find a bug. There isn't one. The evaluation was the problem.

The common misconception

Many developers believe the test set stays clean as long as the model never trains on it, so using it to compare models and tune settings is fine. It's an easy belief to hold:

  • The first rule everyone learns is "don't train on the test set", and this workflow follows it to the letter: fit only 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.

The half-truth: the model's weights never saw the test rows. But you did, and you made decisions based on them. scikit-learn's guide to common pitfalls puts it plainly: test data should never be used to make choices about the model.

What actually happens

Training doesn't end when 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

scikit-learn's grid search guide describes model selection as a way of using labeled data to "train" the parameters of the grid. If the test set drives those choices, it's training data, just used more slowly. The cross-validation guide names the result: when settings are tweaked until the model performs best on the test set, knowledge about the test set can leak into the model, and the metrics no longer report how it will do on new data. It's a form of that never touches fit.

Picking the maximum picks the luck

Every test score is the model's real skill plus noise: which rows happened to land in the test set. Compare two candidates and the noise rarely matters. Compare 200 and keep the top one, and you also select the candidate whose noise was most flattering. The winner sits at the top of a noisy list, so its score is biased upward.

To see it beyond doubt, give the model nothing to learn. The script draws coin-flip labels, so no model can beat 50% on new data. It tries 200 decision-tree settings, keeps the one with the best test score, and checks it on 10,000 extra rows that stand in for production.

Python
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.503

The winner reports 0.610 on the test set and gets 0.503 on new data. Not one test row was used for training, yet the reported number is 11 points too high for a model that knows nothing. With real signal the same inflation is harder to spot: the model is genuinely good, just not as good as the test set claims.

The fix adds one split. Carve a out of the training data and make every choice there. The validation winner is just as lucky, 0.555, because it was picked the same way. But the test set played no part in the choice, so its score, 0.505, tracks the 0.498 on new data.

Three sets, three jobs

Google's Machine Learning Crash Course divides the data three ways, each with one job:

  • 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.

The Crash Course also warns that test and validation sets "wear out" with repeated use: the more you consult them, the more the model fits their quirks and the less you can trust them about new data. Its remedy is to refresh them with newly collected data when you can.

Cross-validation replaces the validation set, not the test set

On a small dataset, one validation split is noisy and costs rows. uses the training data better by rotating which part plays validation. The scikit-learn guide is precise about its scope: with cross-validation the validation set is no longer needed, but a test set should still be held out for final evaluation. The grid search guide says the same for GridSearchCV: run the search on a development set and compute the final metrics on a separate evaluation set it never saw.

MythReality
The test set stays clean as long as fit never sees itEvery choice made by looking at it leaks information into the model
Keeping the best test score is just good engineeringThe best of 200 coin-flip models reported 0.610 and got 0.503 on new data
The score you picked the winner by is honestIt's optimistic too: 0.555 on validation for a model stuck at 0.5
Cross-validation removes the need for a test setIt replaces the validation set; the test set still gives the final number
A test set stays trustworthy foreverRepeated use wears it out; refresh it with new data

How to apply it in your project

  1. Split off the test set firstHold 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.
  2. Make every choice on validation dataUse a validation split, or cross-validation on the training part, for hyperparameters, features, preprocessing and thresholds.
  3. Freeze, then test onceFix the whole pipeline and its settings, then score the test set and report that number, not the validation score.
  4. Don't tune after the testIf 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.
  5. Log every lookKeep 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.

📚 المصادر

  1. Datasets: Dividing the original dataset — Google Machine Learning Crash Course
  2. Cross-validation: evaluating estimator performance — scikit-learn User Guide
  3. Tuning the hyper-parameters of an estimator — scikit-learn User Guide
  4. Common pitfalls and recommended practices — scikit-learn User Guide

التعليقات (0)