Machine Learning Basics·Machine learning from data to a responsible model

Train, validation and test

Hold out data so your score means something.

Intermediate 13 min read Lesson 3 of 300 Challenge included
What you will be able to do
  • Split before scaling or imputing.
  • Use a fixed random_state for reproducibility.
  • Stratify classification splits.
  • Keep a final untouched test set.

In plain English

The test set estimates performance on unseen data. The validation set is for tuning. Never touch the test set until the end, and split before fitting any preprocessing step.

split.pyPython
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y
)

print(len(X_train), len(X_test))
Your turn

Make a stratified split

Easy

Split X and y with a 20% test size and stratification.

PYTHON · starter
train_test_split(X, y, test_size=0.2)
Check your understanding before moving on.
Knowledge checkQuestion 1

Why split before preprocessing?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Regression
Lesson 3 of 300 · 0% of course