Stratified K-Fold for Classification
What you'll learn: How to ensure your cross-validation folds maintain the same class proportions as your original dataset, preventing biased evaluation in classification tasks.
The Problem with Regular K-Fold
When you split data using regular K-Fold cross-validation, each fold gets a random selection of samples. This works fine—unless your classes are imbalanced. Imagine a dataset where 90% of examples are "not spam" and 10% are "spam." Random splitting might create a fold with only 2% spam emails, while another has 18%. This inconsistency makes your model evaluation unreliable.
What Is Stratified K-Fold?
Stratified K-Fold is a smarter version of K-Fold that preserves the class distribution in every fold. If your full dataset has 30% Class A and 70% Class B, each fold will also have approximately 30% Class A and 70% Class B.
Think of it like dealing cards: regular K-Fold just shuffles and deals randomly, but Stratified K-Fold carefully ensures each hand gets the same proportion of suits.
Why It Matters
Without stratification, one fold might accidentally contain very few examples of a rare class. Your model might perform brilliantly on that fold just by predicting the majority class, giving you misleadingly good scores. Stratification ensures every fold is a representative "mini-version" of your full dataset.
Using Stratified K-Fold in Scikit-learn
from sklearn.model_selection import StratifiedKFold
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
for train_idx, val_idx in skf.split(X, y):
X_train, X_val = X[train_idx], X[val_idx]
y_train, y_val = y[train_idx], y[val_idx]
# Train and evaluate your model
Notice you pass both X and y to .split()—the method needs class labels to maintain proportions.
Key Takeaway: Stratified K-Fold ensures each cross-validation fold mirrors your dataset's class distribution, providing more reliable and consistent evaluation metrics for classification tasks.