Explore Library
Code QuizIntermediate

Stratified K-Fold for Balance

Preserving class proportions across folds with stratification.

Codepython
from sklearn.model_selection import KFold

# Highly imbalanced binary labels
kf = KFold(n_splits=5, shuffle=True, random_state=42)
for train_idx, test_idx in kf.split(X, y):
    # We expect each fold to keep the class ratio balanced
    train_and_evaluate(train_idx, test_idx)

The comment expects each fold to preserve the class ratio. What is the bug?