Skip to chapter content
navan✳THE BUILD LABMACHINE LEARNING / GUIDE
0/28 read
CHAPTER 06 / 28 · LEARNING AND EVIDENCE

Training, validation, and test data.

Understand why an honest evaluation uses cases the model did not learn from.

BY NAVAN SAMILONG READTHEORY FIRST
01 / FROM THE BEGINNING

Start with the meaning.

Suppose you learn the answers to a set of flashcards and then grade yourself with those same cards. A high score may show memory rather than understanding. A fitted model has the same problem. Training data supplies examples used to set its parameters. A held-out test set supplies different examples used to estimate how it may behave on future cases. The test set is not another source of training answers. Its value comes from remaining outside the model-building decisions.

A validation set sits between training and final testing. It helps compare model families and hyperparameters, such as a tree's depth. The development process can use training and validation repeatedly; the test set should remain untouched until a final choice is made. In small datasets, cross-validation rotates which training rows serve as validation rows, but the final test set still answers a separate question about the selected whole procedure.

02 / THE MODEL

How the idea works.

A random split is suitable only when examples are sufficiently independent and the future resembles a random sample of the collected data. If many rows belong to one patient or customer, rows from the same person in both sets can make the test easier than real prediction for a new person. A grouped split keeps related rows together. If a model predicts the future from the past, a chronological split often better matches deployment than shuffling all dates together.

Stratification tries to preserve class proportions across a classification split. If positive cases are rare, a naive small random test set might contain none, making performance on that class impossible to judge. Stratification is helpful, but it does not cure all sampling problems: a dataset collected from one city still cannot prove performance in another city.

Leakage occurs when information that should be unavailable to the model influences training or evaluation. A column recorded after the outcome is direct leakage. Computing a mean or scaling statistic from the whole dataset before splitting is subtler: the training process has now seen a summary of test rows. Preprocessing steps that learn from data must be fitted on training rows only and then applied unchanged to validation and test rows. Pipelines make this order easier to preserve.

12345678910TRAININGHELD OUT
Training rows teach the procedure. Held-out rows estimate how it handles cases it did not learn from.
03 / PYTHON, EXPLAINED

Follow the code.

training-validation-test.py
from sklearn.model_selection import train_test_split

X = [[50], [60], [70], [80], [90], [100]]
y = [150, 180, 210, 240, 270, 300]
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=2, random_state=7
)
print(len(X_train), len(X_test))

Each one-element inner list is a feature row. The target at the same position is its known answer. train_test_split shuffles and divides X and y together, preserving their row pairing. test_size=2 asks for two rows in the test set, leaving four for training. random_state=7 makes this example's random division repeatable; it does not make the data inherently representative. The printed lengths are 4 and 2.

A model would fit on X_train and y_train. Predictions for X_test would then be compared with y_test. Because six rows are far too few for a reliable performance estimate, this small example teaches the mechanics of separation rather than claiming a useful model. In a real dataset, the split rule must reflect who or what the model will predict later.

04 / THE BOUNDARY

What the result does not say.

The test set is a stand-in for future data, so its distribution matters. If a company changes its product, data collected before the change may no longer represent after it. Evaluation should describe the target population, time period, and measurement process. A number without that context can be precise and still misleading.

Repeatedly checking the final test score and changing the model in response is another kind of leakage. The model builder has learned from the test, even if the fit method never received those rows. A fresh final evaluation may be needed after extensive iteration.

05 / KEEP THIS

One clear idea.

An honest test asks how a finished procedure handles genuinely unseen, representative cases.

THE CODE IN CONTEXT

The ML cookbook

Complete, explained Python workflows sit alongside this reading course.

Browse the cookbook
Finished reading?
Built with curiosity. By Navan Sami. ↗Machine learning with Python · From examples to trustworthy models.