# Train-test split

> Canonical: https://skillalmanac.com/glossary/train-test-split | Last verified: 2026-11-25 | Index status: index-eligible

A train-test split divides a dataset into one part used to fit a model and a separate held-out part used only to check how it performs on examples it has not seen.

Also called: train test split, train/test split.

## In plain language

If you teach a student with a set of practice questions and then test them on the same questions, a good score may reflect memorization rather than success on new questions. A train-test split avoids this. Before training, you set some of your data aside. The model learns from the rest. The set-aside part can provide a useful final check if it is sufficiently large, appropriately separated, and representative of the data the model will face. Many people also keep a third part, a validation set, for trying out settings along the way. The split only helps if the parts are truly separate: if near-copies of the same example land on both sides, the score looks better than it should. A small dataset also leaves little to test on.

## Technical definition

The scikit-learn train_test_split function randomly splits arrays into training and test subsets. Its documentation lists test_size and train_size as a fraction or a count, shuffle defaulting to true, stratify to split in a stratified fashion using the class labels, in scikit-learn's words, and random_state to make the split reproducible. The scikit-learn user guide explains that testing on the training data can hide overfitting, and that tuning against the test set lets knowledge of it leak into the model. It recommends a separate validation set for tuning, or cross-validation, while still holding out a test set for final evaluation. Google's Machine Learning Crash Course adds that validation or test examples that duplicate training examples should be removed.

## How it shows up when you use agents

When you ask Claude Code, Codex or Cursor to build a classifier or a forecasting script, the generated code often begins by splitting the data, commonly with scikit-learn's train_test_split. What the agent chooses there decides whether the reported score means anything. A random split suits many tables of independent rows. It can mislead when rows are related, for example several records from the same customer, or when time order matters.

What breaks when this is misunderstood: the agent reports a high score, you trust it, and the model fails on real data because test examples were also in training, or because settings were tuned against the test set until it looked good. The scikit-learn guide describes that second route as knowledge about the test set leaking into the model. A next action is to read the split code the agent wrote: check that tuning uses a validation set or [cross-validation](/glossary/cross-validation), that duplicates are removed, and that the test set is touched once at the end. A sound split helps reveal [overfitting](/glossary/overfitting); it does not itself prevent the fitted model from overfitting.

The same idea applies to LLM work. An eval set you keep separate from the examples you tuned prompts on is a held-out set in spirit. See [training data](/glossary/training-data) for what goes into the fitting side, and [LLM benchmark](/glossary/llm-benchmark) for shared test sets.

## Common confusions

- **Test set versus validation set.** The Google crash course describes the validation set as used during development to check and adjust the model, and the test set as a final double-check. scikit-learn warns that tuning on the test set leaks knowledge of the test set into the model.
- **Train-test split versus cross-validation.** Cross-validation repeats fitting and scoring across training/validation folds. In the held-out-test workflow described here, it can replace a separate validation split for tuning; reserve the test set for the final check.
- **Myth.** Shuffling makes a split safe. Shuffle is on by default in scikit-learn, but shuffled rows that are duplicates or share a group can still sit on both sides, which is what a leakage check looks for.
- **Random state versus fairness.** A fixed random state makes the split reproducible. It does not make the split representative. Stratification preserves selected label proportions; group- or time-aware splitting addresses particular dependence or ordering problems. Representativeness still depends on the sampling and deployment setting.

## Worked examples in this catalogue

### [split-leakage-check](https://skillalmanac.com/skills/skill-almanac/split-leakage-check)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: Checks of train, validation and test files for duplicates, shared groups, time and label leakage, printing JSON.

Why it exercises the concept: It tests the property the definition depends on, that the held-out part is separate from the training part, on files you already split. It does not create the split.

### [llm-eval-test-sets](https://skillalmanac.com/skills/skill-almanac/llm-eval-test-sets)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: An eval dataset for an LLM feature, with a checker for duplicates, missing graders, imbalance and leakage.

Why it exercises the concept: An eval set for an LLM feature plays the role of the held-out test set, and its checker looks for leakage and imbalance. It is built for LLM features, not for fitting a model.

### [data-analysis-eda](https://skillalmanac.com/skills/skill-almanac/data-analysis-eda)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: A fixed exploratory-analysis procedure and CSV profiler that keeps findings descriptive, evidenced and caveated.

Why it exercises the concept: Adjacent: profiling a CSV before you split it shows class balance and duplicates that affect how you split. It profiles the data; it does not create the split.

## Related terms

- [Overfitting](https://skillalmanac.com/glossary/overfitting.md)
- [Cross-validation](https://skillalmanac.com/glossary/cross-validation.md)
- [Training data](https://skillalmanac.com/glossary/training-data.md)
- [Supervised learning](https://skillalmanac.com/glossary/supervised-learning.md)
- [Machine learning (ML)](https://skillalmanac.com/glossary/machine-learning.md)
- [LLM benchmark](https://skillalmanac.com/glossary/llm-benchmark.md)

## Sources

- [train_test_split](https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html), scikit-learn (accessed 2026-11-24)
- [Cross-validation: evaluating estimator performance](https://scikit-learn.org/stable/modules/cross_validation.html), scikit-learn (accessed 2026-11-24)
- [Datasets: Dividing the original dataset](https://developers.google.com/machine-learning/crash-course/overfitting/dividing-datasets), Google for Developers (accessed 2026-11-24)

Definitions are written by Skill Almanac from the sources above and checked against them on 2026-11-25. [Glossary index](https://skillalmanac.com/glossary.md).
