Papa Programmer
Lesson 10 of 36

Training, validation and test sets

9 min read

Optional

Three sets, three jobs

  • Training set. The model updates its weights from these Training dataExamples used to update a model's trainable parameters. The examples and labels should be appropriate and permitted for the task..
  • Validation set. You use it repeatedly to compare learning rates, stop at the best point and choose between runs. Because you make choices from it, it slowly stops being a fair test. The Validation setA held-out set used during development to compare settings or choose a checkpoint. A separate test set is kept for a final assessment. is for decisions.
  • Test set. You use it once, after all decisions are made, to estimate how the model will do on new cases.

If you used only one held-out set for both decisions and the final score, each choice would fit that set a little more, and the final number would flatter the model.

How big should each set be

A common start is 80 percent training, 10 percent validation and 10 percent test. The right sizes depend on the task. The validation and test sets need enough examples of every label you care about. With 5 technical tickets in a 100-example test set, one wrong answer moves recall by 20 points, and the score tells you very little. For rare labels, set aside more evaluation data and use fewer for training.

Where leakage comes from

Data leakageWhen information from the evaluation data reaches training, so scores look better than real performance. Duplicates shared between splits are a common cause. means information from the test set reaches the model during training. It makes the test score too good, and the model then disappoints in use. The usual causes are:

  • Duplicates across sets. The same message sits in training and test.
  • Related records. Two messages from the same customer, ticket thread or document land in different sets, so the model has effectively seen the answer.
  • The future. For a task that predicts what comes next, a random split lets the model train on events that happened after the ones it is tested on.

The fix is to decide the split by the unit that must stay together, not by the single row.

A group-aware split

The program below splits tickets by customer, so every message from one customer lands in a single set. It hashes the customer name to pick the set, which makes the split the same each time you run it, even when new rows arrive.

python
import hashlib

tickets = [(f"cust{c}", f"message {m}") for c in range(1, 11) for m in range(1, 4)]

def bucket(customer):
	digest = hashlib.sha256(customer.encode()).digest()[0] / 255
	return "test" if digest < 0.2 else "validation" if digest < 0.4 else "train"

splits = {"train": [], "validation": [], "test": []}
for customer, text in tickets:
	splits[bucket(customer)].append((customer, text))

for name, rows in splits.items():
	print(name, len(rows), sorted({c for c, _ in rows}))

shared = {c for c, _ in splits["train"]} & {c for c, _ in splits["test"]}
print("customers in both train and test:", sorted(shared))

It prints:

text
train 24 ['cust10', 'cust2', 'cust3', 'cust4', 'cust6', 'cust7', 'cust8', 'cust9']
validation 3 ['cust5']
test 3 ['cust1']
customers in both train and test: []

The set of customers in both training and test is empty, which is the check you want. The sizes happen to land on 24, 3 and 3 rows, which is 80, 10 and 10 percent, but with only ten customers that is luck, because each customer goes in whole. With thousands of groups, the shares come close to the targets. Run the leakage check as a test in your own project: it takes one line and fails loudly.

A split by time

When the model will predict future cases, train on the past and test on later cases. Sort by date and cut.

python
dated = [("2026-01-05", "a"), ("2026-02-11", "b"), ("2026-03-02", "c"),
		 ("2026-04-19", "d"), ("2026-05-23", "e"), ("2026-06-30", "f")]

dated.sort()
cut_validation, cut_test = 4, 5
train, validation, test = dated[:cut_validation], dated[cut_validation:cut_test], dated[cut_test:]

print("train up to", train[-1][0])
print("validation", validation[0][0])
print("test", test[0][0])
print("test is after train:", test[0][0] > train[-1][0])

It prints:

text
train up to 2026-04-19
validation 2026-05-23
test 2026-06-30
test is after train: True

A time split usually gives lower scores than a random one. That is the honest score for a task that looks forward.

Rules to keep

  1. Split before you do anything that learns from the data, such as choosing label weights or building a vocabulary.
  2. Deduplicate across all three sets, as Cleaning data and checking quality showed.
  3. Use the validation set for as many decisions as you like, and the test set for one.
  4. If you must look at the test set and change the model, collect a fresh test set.
  5. Keep the three files in the same format, as Formatting data for training explained.

For other ways to estimate performance, such as cross-validation, see the scikit-learn cross-validation guide.

Three jobsTrain to learn, validate to decide and test once to report.
Keep groups togetherSplit by customer, thread or document, not by single row.
Check for overlapAssert in code that no group or text appears in two sets.

Your reading position is saved

0%

0 of 36 complete

Contents

Learning tools