Sub topic 5
Splitting Data into Train, Validation and Test Sets
This chapter assumes you know basic Python, can use pandas DataFrames, and understand what a supervised model is. You will learn why a model needs three separate sets of rows and how to create them with scikit-learn.
Why one dataset is not enough
A model that is scored on rows it has already seen looks better than it really is. It may have memorized those rows. You want to know how the model performs on new data, so you hold some rows back.
Each set has one job:
| Set | Used for | Used how often |
|---|---|---|
| Training | Fitting model parameters | Every time you train |
| Validation | Comparing models and choosing settings | Many times |
| Test | Reporting the final score | Once |
Every time you look at a score and change something, information from that set leaks into your choices. The validation set absorbs this over many rounds. The test set stays untouched, so its score is an honest estimate.
Making the split
Split off the test set first, then split the remainder. A common ratio is 60% train, 20% validation and 20% test.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
X, y = load_breast_cancer(return_X_y=True, as_frame=True)
X_temp, X_test, y_temp, y_test = train_test_split(
X, y, test_size=0.2, stratify=y, random_state=42
)
X_train, X_val, y_train, y_val = train_test_split(
X_temp, y_temp, test_size=0.25, stratify=y_temp, random_state=42
)
print(len(X_train), len(X_val), len(X_test))
print(y_train.mean(), y_val.mean(), y_test.mean())The second split uses test_size=0.25 because 25% of the remaining 80% is 20% of the whole. The printed class proportions should be close to each other.
Two arguments matter here:
stratify=ykeeps the class proportions the same in each part. Without it, a rare class can end up almost missing from one set.random_state=42makes the shuffle repeatable, so a colleague gets the same rows.
Cases where a random split is wrong
- Time series. If rows are ordered by time, a random split lets the model train on the future and predict the past. Train on earlier rows and test on later ones.
TimeSeriesSplitsupports this. - Grouped rows. If one customer or patient appears in many rows, put all of that person's rows in the same set. Use
GroupShuffleSplitorGroupKFold. Otherwise the model recognizes people instead of learning patterns. - Duplicates. Identical rows in both train and test inflate the score. Remove them before splitting.
Validation sets and cross-validation
With small datasets, a single validation set gives a noisy score. Cross-validation repeats the train and validation split several times on the training data and averages the results. You still keep the test set aside. A later chapter covers it.
Exercise
A dataset has 1,000 rows. You want 60% train, 20% validation and 20% test. After the first split removes 20% as the test set, what value of test_size do you pass to the second train_test_split, and why?
Show answer
Pass test_size=0.25. After the first split 800 rows remain, and the validation set should have 200 of them. 200 / 800 = 0.25.
Exercise
A hospital dataset has several rows per patient. Why can a random row-level split give an overly optimistic test score, and what should you use instead?
Show answer
The same patient can appear in both train and test, so the model can score well by recognizing patient-specific traits rather than learning general patterns. Use a group-aware split such as GroupShuffleSplit or GroupKFold, so each patient's rows stay in one set.