Module lessons (1/6)
Train and test sets
Why we hold data out, and how to split with random_state and stratify.
What you'll learn in this lesson
- Explain why a model must be evaluated on data it never trained on.
- Split data with
train_test_split, including a fixedrandom_stateandstratify. - Read a test score as an estimate of future performance.
Overview
If you study the exact exam questions and are then graded on those same questions, a perfect mark tells nobody whether you understood the subject. A model is no different. Its score on the data it learned from says how well it remembered; we care about how well it generalises to new data.
The remedy is simple: before training, set aside a portion of the data, the test set, and look at it only once, at the end.
Sections covered in this lesson
- 1The hook: grading with the answer key
- 2Comparing the three dataset splits
- 3Try it: interactive train/validation/test split explorer
- 4Progressive examples: splitting cleanly in Python
- 5Practice
- 6Check your understanding
Unlock "Train and test sets" and the full Machine Learning: from first principles to practice curriculum
Why we hold data out, and how to split with random_state and stratify.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Machine Learning: from first principles to practice plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.