How to apply oversampling when doing Leave-One-Group-Out cross validation?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
When you combine class imbalance with grouped data, the order of operations matters. In Leave-One-Group-Out cross-validation, oversampling must happen inside each training fold only, otherwise you leak information from the held-out group into the model-selection process.
The Correct Rule
For each LOGO split:
- keep one group completely untouched as the test fold
- take all remaining groups as training data
- run oversampling only on that training portion
- fit the model
- evaluate on the untouched group
That is the entire principle. If you oversample before the split, synthetic samples will be influenced by data that should have remained held out.
A Safe Implementation with imblearn
Using imblearn.pipeline.Pipeline is the cleanest approach because the sampler is applied during fit and not during predict. The pipeline below is fully runnable if you have scikit-learn and imbalanced-learn installed.
The important detail is that SMOTE sees only X[train_idx] and y[train_idx] inside each fold.
Why Pre-Oversampling Is Wrong
Suppose group 7 is the held-out group for one fold. If you run SMOTE on the full dataset first, the synthetic training samples may be interpolated using points from group 7. Even though you later separate the indices, the training data has already been contaminated by the test group.
That makes the score optimistic. The whole reason to use LOGO is to test generalization across groups, so leaking group information destroys the point of the validation strategy.
Watch the Minority Count in Each Fold
SMOTE has another practical constraint: it needs enough minority samples in the current training fold. If one split contains very few minority examples, k_neighbors=5 may be impossible.
A defensive pattern is to adapt the sampler per fold:
That way the fold does not crash just because the minority class is tiny in one group split.
Pipeline Design Matters
If you also scale features, select features, or tune hyperparameters, keep those steps inside the fold as well. A common pattern is sampler plus model in an imblearn pipeline, then manual LOGO iteration or a group-aware search procedure.
The rule is consistent: anything that learns from data must be fit only on the training groups for that fold.
Common Pitfalls
- Oversampling the full dataset before LOGO splitting. That is leakage.
- Using a plain
sklearn.pipeline.Pipelinewith a sampler. Samplers needimblearn.pipeline.Pipeline. - Forgetting that some folds may have too few minority examples for the default SMOTE neighbor count.
- Reporting ordinary accuracy on a severely imbalanced problem and calling the evaluation complete.
- Ignoring the meaning of groups. If groups represent subjects, sessions, or devices, leakage across them is exactly what LOGO is designed to prevent.
Summary
- Apply oversampling after the LOGO split, never before it.
- The sampler must see only the training groups in each fold.
- '
imblearn.pipeline.Pipelineis the cleanest implementation pattern.' - Check minority counts per fold because SMOTE may need a smaller
k_neighborsor a fallback sampler. - Group-aware validation is only useful if every preprocessing step respects the group boundary.
Related reading
- How to apply StandardScaler in Pipeline in scikit-learn sklearn?
- How to approach machine learning problems with high dimensional input space?
- How to appropriately plot the losses values acquired by loss_curve_ from MLPClassifier
- How to approximate the determinant with keras
- How to asynchronously run Matplolib server-side with a timeout? The process hangs randomly
- How to average summaries over multiple batches?
- How to Argsort in Tensorflow?
- How to assign a name for a pytorch layer?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.