Course contentsShow
← Computer Science
Lesson 2799 of 2,87262. Machine Learning FundamentalsFree lesson

Stratified Sampling

Ensuring class proportions are maintained across train/validation/test splits in classification tasks.

Stratified Sampling

You'll learn how to maintain representative class proportions when splitting data into training, validation, and test sets for classification tasks.

The Core Problem

When working on classification tasks, your dataset often has imbalanced classes—maybe 80% of emails are legitimate and only 20% are spam. If you randomly split your data into train/validation/test sets, you might accidentally end up with very different class distributions across these splits. Your training set might be 75% legitimate, while your test set is 85% legitimate. This makes evaluation unreliable and can hurt model performance.

What Stratified Sampling Does

Stratified sampling ensures that each split (train, validation, test) maintains the same class proportions as the original dataset. If your full dataset is 80/20 legitimate/spam, stratified sampling guarantees each split is also roughly 80/20.

Think of it like making sure each slice of a layered cake has the same ratio of chocolate to vanilla—you don't want one person getting mostly chocolate while another gets mostly vanilla.

Why It Matters

When you learned about K-Fold Cross-Validation in the previous lesson, maintaining consistent class distributions across folds is crucial. Stratified sampling makes your:

  • Training more stable: The model sees representative data
  • Validation more meaningful: Performance metrics reflect true class balance
  • Test results more reliable: Final evaluation isn't skewed by sampling luck

This is especially critical when dealing with rare classes. Without stratification, a rare class (say, 5% of data) might be completely missing from one split by random chance.

Key Takeaway: Stratified sampling preserves class proportions across train/validation/test splits, ensuring each split is a miniature representative of your full dataset's class distribution—critical for fair and reliable classification model evaluation.