StudyAIStudyAI
Pro
Lesson 48 min

Train / Test Split

Why you must evaluate on data the model has never seen.

What you will learn
  • Why a test set is essential
  • Typical split ratios
  • What data leakage is

Explanation

If you test a model on the same data it trained on, it can simply memorise and look perfect — but fail on new data. So you split the data first.

A common split is 80% train, 20% test. The model never sees the test set during training, so its test score estimates real-world performance.

Watch out for data leakage — accidentally letting test information influence training (e.g. scaling using the whole dataset). It produces falsely high scores.

Code Example

python
1
from sklearn.model_selection import train_test_split
2
 
3
Xtr, Xte, ytr, yte = train_test_split(
4
    X, y, test_size=0.2, random_state=42
5
)
Real-world use

A model that scores 99% on training data but 60% on the test set is overfitting — the split is what reveals it.

Common mistakes
  • Reporting the training-set score and calling it the model's accuracy.
Practice

Split any dataset 80/20 and note how the train and test accuracies differ.

Knowledge check
0/1 answered

1. Why hold out a test set?

Answer all questions to check.