Sub topic 1
Tabular Data and How to Validate a Model
Prerequisites
This chapter assumes you know Python and pandas basics, and what features, labels and overfitting mean. You should know how a decision tree makes predictions. You should also know common metrics such as RMSE, log loss and AUC. Later chapters build on the validation habits set here.
Why validation comes first
Gradient boosted trees are flexible enough to memorize a training set. Training error keeps falling as you add trees, so it says little about new data. A validation set is data the model never trains on. You use it to measure quality, to decide when to stop adding trees, and to compare settings.
Every later chapter, including early stopping and tuning, depends on a trustworthy validation score. A leaky or badly built split gives a number that looks good and fails in production.
Three sets, three jobs
- Training set: the model learns from it.
- Validation set: you use it to stop training and choose hyperparameters. Because you make decisions from it, its score becomes slightly optimistic.
- Test set: you touch it once, at the end, to report final quality.
If you have little data, replace the single validation set with k-fold cross-validation. It trains k models, each validated on a different fold, and averages the scores.
Choose a split that matches deployment
The split must imitate how the model will be used.
| Situation | Split to use |
|---|---|
| Rows are independent | Random split |
| Rare positive class | Stratified split, which keeps the class ratio |
| Several rows per user, device or patient | Group split, which keeps each group on one side |
| Predicting the future from the past | Time-based split, with training data older than validation data |
A worked example
This example builds a regression split with a fixed seed and a baseline. The baseline predicts the training mean, so any model should beat it.
import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
X, y = fetch_california_housing(return_X_y=True, as_frame=True)
X_train, X_valid, y_train, y_valid = train_test_split(
X, y, test_size=0.2, random_state=0
)
baseline = np.full(len(y_valid), y_train.mean())
rmse = np.sqrt(np.mean((y_valid - baseline) ** 2))
print(f"Baseline RMSE: {rmse:.3f}")
print(X_train.shape, X_valid.shape)A model whose validation RMSE is not clearly below this baseline has learned little.
Common errors
- Preprocessing before splitting. Computing means, encodings or scaling on the full dataset leaks validation information into training. Split first, then fit any transformation on the training part only.
- Reusing the validation set too often. After many tuning rounds, the score reflects luck as well as skill. Keep a separate test set.
- Wrong metric. Pick a metric that fits the task, such as AUC for ranking or log loss for probabilities, and use it consistently.
- Duplicate rows across splits. Remove or group duplicates before splitting.
Exercise
A table has 10 rows per customer, and you want to predict behavior for new customers. Why is a plain random row split a poor choice, and what should you use?
Show answer
Rows from the same customer land in both training and validation, so the model can recognize customers it has already seen, and the validation score is too high. Use a group split (for example scikit-learn's GroupKFold) so each customer appears on only one side.