Skip to content

Train-Test Split Concepts

When you evaluate a model, you want to test on data it hasn’t seen.

  • Train set: used to learn patterns
  • Test set: used only for final evaluation

This simulates real-world performance.

Leakage happens when information from the test set influences training.

Examples:

  • Scaling using mean/std computed on the full dataset
  • Filling missing values using overall mean (including test)
  • Feature engineering that uses future information
train_test_split
import pandas as pd
from sklearn.model_selection import train_test_split
 
X = pd.DataFrame({"age": [20, 21, 22, 23, 24], "score": [80, 85, 78, 90, 88]})
y = pd.Series([0, 0, 0, 1, 1])
 
X_train, X_test, y_train, y_test = train_test_split(
    X, y,
    test_size=0.2,
    random_state=42,
    stratify=y,
)
 
print(X_train)
print(X_test)

If your target classes are imbalanced, use stratify=y so train/test have similar class distribution.

For time series, you often do not shuffle. You train on past and test on future.

  • Keep a final test set untouched.
  • Use cross-validation on training data for tuning.
  • Put preprocessing inside a pipeline.
diagram Train/test split mermaid
The full dataset is split once; the model learns only from train and is evaluated only on the untouched test set.
sketch Splitting one dataset into train and test p5.js
The same rows, rearranged into an 80/20 split — train teaches the model, test grades it.

Exercise 2 – Keep class balance with stratify

Section titled “Exercise 2 – Keep class balance with stratify”

Exercise 3 – Reproducible splits with random_state

Section titled “Exercise 3 – Reproducible splits with random_state”

Once your split strategy is settled, wire scaling, encoding, and the model together with a Preprocessing Pipeline (scikit-learn).

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading