liveBook · Manning

StratifiedKFold

As explained in Ensemble Methods for Machine Learning

StratifiedKFold is a scikit-learn cross-validation splitter that creates folds while preserving class proportions across the folds.

StratifiedKFold creates cross-validation folds that retain the original class proportions. If positive and negative examples occur in a 2:1 ratio, each fold aims to preserve that balance, making validation comparisons more representative. section 3.3.2

Use the splitter to generate training and validation subsets without copying the dataset:

  • Call splitter.split(X, y) to obtain training and validation indices for each fold. section 3.3.2
  • Average the validation results to compare parameter choices such as learning rates. section 5.4.1

Stratified stacking validation

A five-fold stacking procedure trains each base estimator on four folds, generates predictions for the held-out fold, and combines those predictions into meta-examples. After all folds contribute predictions, the level-2 estimator trains on the complete meta-dataset, while the base estimators are retrained on all available data.

Stratification improves the comparability of folds but increases computation when models are retrained repeatedly. Six base estimators with five folds require 30 model fits, and ten-fold parameter searches repeat training even more often. section 3.3.2 section 6.4.1

How Ensemble Methods for Machine Learning covers it

How deep it goes
Explains fold construction, index generation, model reuse, and averaged validation, but does not develop the splitter’s implementation internals.
Examples
Uses 2:1 class ratios, five-fold stacking, and ten-fold learning-rate searches on breast cancer data.
What it assumes
Assumes familiarity with labeled classification data, training-validation splits, estimators, and cross-validation.
What you can do after
Apply StratifiedKFold through scikit-learn to generate balanced training-validation indices for model evaluation and parameter comparison.
How it connects
Connects StratifiedKFold with cross-validation, KFold, stacking, gradient boosting, learning rate, and model parameter selection.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Ensemble Methods for Machine Learning. The section links point to the parts of the book it draws on.