- Introduced by
- Wolpert, D.H.
- Year
- 1992
- Type
- Ensemble (heterogeneous meta-learning)
What does it do?
Stacking, or stacked generalization, is an ensemble method introduced by David Wolpert in 1992.
It combines the predictions of several base models (Level-0) through a separate meta-model (Level-1) that is trained to blend them. Unlike bagging and boosting, which usually combine many models of one type, stacking is designed to use heterogeneous model families.
The intuition behind the method
Different model families tend to make different errors on the same data: a tree ensemble, a support vector machine and a linear model each represent the relationship between inputs and outcome in their own way. Stacking uses their predictions as new input features and trains a second model to learn how much weight each should receive, so it can rely more on a base model where that model performs well and less where it does not. Whether the combined prediction beats the best single base model depends on how different the base models' errors actually are.
History
Stacked generalization was introduced by David H. Wolpert in 1992 as a way to combine several generalizers through a higher-level model. Van der Laan, Polley and Hubbard (2007) proposed the Super Learner, which uses V-fold cross-validation to select weights for combining a library of candidate learners, and they include a practical demonstration of its adaptivity to different data-generating distributions. Their abstract describes the approach as generalizing to any parameter defined as a minimizer of a loss function.
When to use it
Choose stacking when predictive accuracy is the main goal and you have several reasonably strong base models from different model families, so that their errors are not the same. It suits cross-sectional data of the kinds the chosen base learners can handle. The cross-validation step needs enough observations to produce reliable Level-0 predictions, so the sample should be large enough to support k folds for every base learner. No universal minimum sample size applies; the adequate size depends on the number of base learners and on the variance of each learner. Avoid stacking when the sample is small in relation to the number of base learners, or when a single model that is easy to interpret is required. In those cases a single well-tuned model such as a random forest or XGBoost is often the simpler and more transparent choice. Stacking is also a poor choice when the base models are essentially variants of one algorithm, since little diversity is available to exploit.
Limitations and considerations
- Requires enough observations for k-fold cross-validation to produce stable Level-0 predictions. No universal minimum sample size applies; the adequate size depends on the number of base learners and the variance of each learner.
- Gains depend on diversity: when base models make highly correlated errors, the stack improves little over its strongest member.
- Harder to train, tune and interpret than a single model, because it involves several fitted models and a cross-validation scheme.
- Each base learner is refitted once per fold and once more on the full training data, so training cost grows with the number of base models and with k.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Can improve on the individual base models when they are diverse, because the meta-model learns to weight each one where it is reliable.
- Flexible: any set of learners that produce predictions can serve as base models, with a simple meta-model combining their outputs.
- The combination step takes only the base models' predictions as input, so it adds no requirement on raw feature types beyond what the base learners already handle.
- Out-of-fold construction of the meta-features means the meta-model is trained on predictions for observations that were held out from the base model that produced them.
Applications
- Final-stage blending of predictive models where accuracy is the main objective and the base models differ in family.
- Prediction problems where a cross-validated weighted combination of candidate learners is preferred to choosing a single learner, as in the Super Learner framework.
- Comparing whether a combination of models improves on the best single model under cross-validation.
How it works
How it works
A set of base learners (Level-0) is fitted to the training data, for example a random forest, a gradient-boosting model and a support vector machine. The stack can gain only when these models make partly different errors; if they are highly correlated, combining them adds little beyond the best member.
The Level-0 predictions used to train the meta-model are generated by k-fold cross-validation rather than on the data each base model was fitted to. Each observation's meta-feature therefore comes from a model that did not see that observation, which keeps in-sample fit from leaking into the meta-model. Once the meta-model has been trained, each base learner is refitted on all of the training data, and these refitted models produce the Level-0 inputs for new observations at prediction time.
The meta-model (Level-1) is trained on these out-of-fold base-model outputs to produce the final prediction, ŷ = g(f₁(x), f₂(x), …, f_M(x)). It is usually a low-capacity learner such as logistic regression or ridge regression, whose job is to learn blending weights. When the combination is linear, the weights are commonly restricted to be nonnegative and to sum to 1. This turns the blend into a convex combination of the base models and leads to a tractable quadratic programming problem. Restricting the weights in this way can give better results than unrestricted weights. A higher-capacity learner, such as a gradient-boosted model, can overfit the base-model outputs when the sample is limited. The combiner need not be linear: any learning method can be used, and the weights may depend on the input location x.
Formula
\hat{y} = g\big(f_1(x), f_2(x), \dots, f_M(x)\big)Common pitfalls
- Generating Level-0 predictions on the same data the base models were trained on, rather than out-of-fold, which leaks in-sample fit into the meta-model.
- Including many near-duplicate base models, such as several boosting variants with similar settings, and expecting a large gain from them.
- Using a high-capacity meta-model that fits the base-model outputs too closely, instead of a simple combiner.
- Applying stacking to a small dataset, where both base-model diversity and reliable out-of-fold predictions become hard to secure.
- Scoring the finished stack on the same folds or data used to choose the meta-model and base learners, which gives an optimistic estimate of performance. Evaluate the whole pipeline on a held-out test set or with nested cross-validation, so that no model is scored on data used to fit it.
Frequently asked questions
How is stacking different from bagging and boosting?
Bagging and boosting usually combine many models of the same type, whereas stacking deliberately combines heterogeneous model families and learns how to blend them with a separate meta-model. The gain from stacking depends on that diversity.
Why use cross-validation to build the meta-features?
If the meta-model is trained on predictions the base models made on their own training data, in-sample fit leaks into the meta-model. Producing Level-0 predictions out-of-fold with k-fold cross-validation gives the meta-model inputs that the base models did not fit.
What should the meta-model be?
A low-capacity learner such as logistic regression or ridge regression is the usual choice, because it learns blending weights without overfitting the base-model outputs. For linear blending, restricting the weights to be nonnegative and to sum to one is a further safeguard. A higher-capacity learner, such as a small XGBoost, carries more overfitting risk on a limited sample.
When should I not use stacking?
When the sample is small relative to the number of base learners, cross-validation may not produce reliable Level-0 predictions, and base-model diversity is hard to achieve. In that case a single well-tuned model such as a random forest or XGBoost is often the simpler and more interpretable choice.
Related methods
Sources
Wolpert, D.H. (1992). Stacked Generalization. Neural Networks, 5(2), 241–259.
Open DOI referencevan der Laan, M.J., Polley, E.C. & Hubbard, A.E. (2007). Super Learner. Statistical Applications in Genetics and Molecular Biology, 6(1), Article 25.
Open DOI reference2009_Book_TheElementsOfStatisticalLearni, p. 306