Latticework

Command Palette

Search for a command to run...

Scikit-learn

Train/Test Splitting

18 min

Explanation

train_test_split shuffles the data before splitting it, which means two calls without a fixed seed produce DIFFERENT train/test sets every time — making it impossible to reproduce a specific result or fairly compare "model A vs. model B" (since they'd see different data). The random_state parameter fixes this: the shuffle becomes fully deterministic, exactly like random.seed() from Monte Carlo Simulation's seeded-randomness technique, just scoped to scikit-learn's own random number generation.

from sklearn.model_selection import train_test_split

def same_test_set(X, y, test_size, seed_a, seed_b):
    _, X_test_a, _, _ = train_test_split(X, y, test_size=test_size, random_state=seed_a)
    _, X_test_b, _, _ = train_test_split(X, y, test_size=test_size, random_state=seed_b)
    return X_test_a == X_test_b
Try it

test_size=0.25 controls HOW MANY rows go to the test set; random_state controls WHICH rows -- two completely independent knobs that are easy to conflate when first learning this API.

Loading editor…
Exercise

Write `same_test_set(X, y, test_size, seed_a, seed_b)`: split `(X, y)` twice with `train_test_split`, once using `random_state=seed_a` and once using `random_state=seed_b`. Return `True` if both splits produced the exact same `X_test`, `False` otherwise.

Quiz

Why does `train_test_split`'s `random_state` parameter matter for reproducibility, given that a train/test split is supposed to be a RANDOM shuffle?

Checkpoint

You can use train_test_split with a fixed random_state to produce fully reproducible splits, and understand why reproducibility matters for debugging and fair model comparisons.