Skip to main content
Module 4 · Lesson 1 of 615/56 in the course~18 min
Module lessons (1/6)

Train and test sets

Lesson preview5 hands-on exercises readyMachine Learning: from first principles to practice · Module 4 — Generalization and evaluation

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 fixed random_state and stratify.
  • 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

  1. 1The hook: grading with the answer key
  2. 2Comparing the three dataset splits
  3. 3Try it: interactive train/validation/test split explorer
  4. 4Progressive examples: splitting cleanly in Python
  5. 5Practice
  6. 6Check your understanding
Pro Lesson · Module 4 — Generalization and evaluation

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.