You can use StratifiedKFold in Scikit-learn to maintain the same class distribution across folds when class imbalance exists in a classification problem.
Here is the code snippet you can refer to:

In the above code we are using the following key points:
- StratifiedKFold(n_splits=5) ensures each fold has a proportional representation of classes.
- shuffle=True randomizes the data before splitting for more representative folds.
- cross_val_score() evaluates the model using stratified cross-validation.
- mean() computes the average accuracy across folds, measuring consistent performance.
Hence, StratifiedKFold prevents biased evaluation by preserving class distribution, leading to more reliable model validation on imbalanced datasets. Let me know if you’d like any tweaks or deeper explanations!