Back to Blog
Machine Learning December 15, 2024 5 min read

Cross-Validation Strategies: Which One to Use and When

K-Fold, Stratified, GroupKFold, TimeSeriesSplit — a practical guide to choosing the right CV strategy based on your data structure.

Decision Tree

  1. Time series data? → TimeSeriesSplit (never shuffle time!)
  2. Groups that must stay together? → GroupKFold
  3. Imbalanced classes? → StratifiedKFold
  4. Default tabular → StratifiedKFold with 5 folds

TimeSeriesSplit Example

from sklearn.model_selection import TimeSeriesSplit
tscv = TimeSeriesSplit(n_splits=5)
for train_idx, val_idx in tscv.split(X):
    X_train, X_val = X.iloc[train_idx], X.iloc[val_idx]

GroupKFold: Preventing Leakage

If you have user IDs, patient IDs, or store IDs — always use GroupKFold so the same entity never appears in both train and validation.

Cross-ValidationModel EvaluationTime SeriesKaggleBest Practices
O

Ossama Elhakki

AI Engineer & ML Systems Builder — Morocco