AI Concepts

What is Stacking?

Stacking is an ensemble method that combines predictions from multiple different models by training a higher-level model on their outputs. A second learner receives the base models' predictions as its input features and discovers how to best blend them, producing a final answer that is typically more accurate than any single model alone.

Mar 27, 2026
Updated Mar 27, 2026
9 min read

Stacking, also known as stacked generalization, is an ensemble learning technique in which multiple diverse models are combined through a higher-level model that learns how to best integrate their predictions. Rather than relying on a single learning algorithm, stacking leverages the complementary strengths of several base learners by training a meta-learner to weigh and synthesize their outputs. This approach frequently achieves superior predictive performance compared to any individual model acting alone, making it one of the most powerful strategies in the machine learning practitioner's toolkit.

Core idea behind stacking

The fundamental intuition behind stacking is that different learning algorithms capture different patterns in data, and no single algorithm is universally optimal across all regions of the input space. By training a second-level model on the outputs of several first-level models, stacking enables the system to learn which base learner is most trustworthy under which circumstances. The meta-learner effectively discovers an optimal combination strategy from the data itself rather than relying on a fixed rule such as simple averaging or majority voting.

This distinguishes stacking from other ensemble methods like bagging and boosting, which typically combine models of the same type. Bagging reduces variance by averaging predictions from multiple instances of one algorithm trained on bootstrap samples, while boosting sequentially corrects the errors of weak learners of the same family. Stacking, by contrast, encourages diversity by using fundamentally different learning algorithms as its base models and then learning a principled way to merge their predictions.

How stacking works in practice

The stacking procedure unfolds in two main stages. In the first stage, a set of base-level models, often called level-zero learners, are trained on the original training data. These base models can be any combination of algorithms, such as a decision tree and a support vector machine, or a neural network paired with a logistic regression model.

In the second stage, the predictions generated by these base models are collected and used as input features for a meta-learner, known as the level-one model. The meta-learner is trained to map these base-model predictions to the true target variable. To prevent information leakage and overfitting, the base-model predictions used for training the meta-learner are typically generated through cross-validation on the training set rather than by predicting on the same data used to fit each base model.

Role of cross-validation in stacking

Cross-validation is essential to the integrity of the stacking procedure. If the base learners were simply allowed to predict on their own training data, those predictions would be unrealistically accurate due to overfitting, and the meta-learner would learn a distorted mapping. By using k-fold cross-validation, each training example receives a base-model prediction that was generated without that example being part of the training set for that fold.

In a typical implementation, the training data is split into k folds. For each fold, each base learner is trained on the remaining k minus one folds and then generates predictions for the held-out fold. After cycling through all folds, every training instance has an out-of-fold prediction from every base learner. These out-of-fold predictions form the new feature matrix on which the meta-learner is trained. This procedure ensures that the meta-learner sees predictions that reflect each base model's genuine generalization ability.

Choosing base learners

The effectiveness of stacking depends heavily on the diversity of its base learners. If all base models make similar errors, the meta-learner has little room to improve upon any single one of them. Diversity can be achieved by selecting algorithms with fundamentally different inductive biases. For example, combining a tree-based model like a random forest with a linear model like ridge regression ensures that one captures nonlinear interactions while the other captures smooth, additive trends.

It is also possible to introduce diversity by varying the feature subsets, hyperparameters, or data preprocessing pipelines supplied to models of the same algorithm family. However, mixing distinct algorithm types generally provides the most meaningful diversity. The number of base learners is a design choice, but practitioners commonly use between three and ten to balance predictive gain against computational cost.

Selecting the meta-learner

The meta-learner sits at the top of the stacking architecture and is responsible for making the final prediction. A common and effective choice is a simple model such as logistic regression for classification or linear regression for regression tasks. The simplicity of the meta-learner acts as a regularizer, reducing the risk of overfitting to the base-model predictions.

However, more complex meta-learners can also be used if the relationship between base-model outputs and the target is nonlinear. A gradient-boosted tree or a small neural network can serve as the meta-learner when the ensemble is large and the data is plentiful. The key consideration is that the meta-learner should be expressive enough to capture useful patterns in the base predictions without memorizing noise.

Multi-level stacking

Stacking can be extended beyond two levels to create deeper ensemble hierarchies. In multi-level stacking, the outputs of the first-level meta-learner can themselves become inputs to a second-level meta-learner, and so on. Each additional layer has the opportunity to correct residual errors from the layer below.

In practice, however, diminishing returns set in quickly, and stacking architectures with more than two or three levels rarely provide significant gains. Each added level also increases computational cost and the risk of overfitting, especially on smaller datasets. Most successful applications use a two-level architecture with one set of base learners and a single meta-learner.

Stacking for classification versus regression

Stacking is applicable to both classification and regression problems, though the details differ slightly. In classification, the base learners typically output class probabilities rather than hard class labels, because probabilities contain richer information that the meta-learner can exploit. A base model that assigns a probability of 0.51 to a class conveys uncertainty, whereas a hard label of one or zero discards this nuance.

For regression tasks, the base learners produce continuous predictions, and the meta-learner learns to combine these into a single output. In both settings, the meta-learner can also receive the original input features alongside the base-model predictions, a variant sometimes called stacking with passthrough features. Including original features gives the meta-learner direct access to raw information that base models may have partially lost.

Advantages of stacking

Stacking offers several compelling advantages. Its primary benefit is improved predictive accuracy. By combining diverse models, stacking can reduce both bias and variance simultaneously, something that bagging and boosting individually tend to address only one of. The meta-learner's ability to weight base models adaptively means that a poorly performing base model is effectively down-weighted rather than dragging down the ensemble.

Another advantage is flexibility. Stacking imposes no restrictions on the types of models that can be combined, making it a model-agnostic ensembling technique. This allows practitioners to incorporate domain-specific models alongside general-purpose algorithms, tailoring the ensemble to the problem at hand.

Potential drawbacks and challenges

Despite its power, stacking introduces significant computational overhead. Training multiple base learners with cross-validation and then training a meta-learner on their outputs can be many times more expensive than training a single model. This cost scales with the number of base learners, the number of cross-validation folds, and the size of the dataset.

Overfitting is another concern, particularly when the meta-learner is too complex or when the stacking procedure does not rigorously separate training data from prediction data at each level. Information leakage, where a base model's predictions on its own training data are used to fit the meta-learner, can lead to overly optimistic performance estimates and poor generalization. Careful use of cross-validation is the primary safeguard against this problem.

Interpretability is also sacrificed in stacking. While a single decision tree or linear model can be inspected and understood, a stacked ensemble of five or more heterogeneous models with a meta-learner on top becomes significantly harder to explain. This limits its use in domains where model transparency is a regulatory or practical requirement.

Stacking in competitions and real-world applications

Stacking has gained particular prominence in machine learning competitions, where marginal improvements in accuracy can determine rankings. Many winning solutions on platforms such as Kaggle employ multi-model stacking as a core strategy. The competitive setting rewards complexity and predictive performance without the same constraints on latency, cost, or interpretability that production systems face.

In real-world deployments, stacking is used when predictive performance is the paramount concern, such as in fraud detection, medical diagnosis support, and demand forecasting. Organizations often weigh the additional engineering complexity and inference time against the accuracy gains. In latency-sensitive applications, distilling the stacked ensemble into a single simpler model is sometimes used to capture most of the benefit without the full inference cost.

Relationship to blending and other ensemble strategies

Stacking is closely related to a technique called blending, which uses a fixed holdout set rather than cross-validation to generate base-model predictions for meta-learner training. Blending is simpler to implement and faster to run but uses less data for training both the base models and the meta-learner. Stacking's use of cross-validation makes it more data-efficient and generally more robust.

Stacking also differs from simple model averaging or voting, which assign equal or predetermined weights to each model. In stacking, the meta-learner determines these weights implicitly through its training process, and it can learn nonlinear combinations that fixed-weight schemes cannot represent. This learned combination is what gives stacking its edge over simpler aggregation strategies.

Practical tips for implementing stacking

When building a stacking ensemble, it is advisable to start with a small, diverse set of well-tuned base learners rather than a large number of poorly configured ones. Each base learner should be independently validated to ensure it provides meaningful predictions before being included in the stack. Removing a base model that adds only noise can improve the meta-learner's job.

Using out-of-fold predictions consistently and avoiding any form of data contamination between levels is critical. Practitioners should also evaluate the full stacking pipeline using an entirely held-out test set that was never seen during any stage of training. This honest evaluation protocol ensures that reported performance metrics reflect true generalization capability rather than artifacts of the stacking process itself.

Stacking remains one of the most effective ensemble techniques available, offering a principled and flexible framework for combining diverse models into a unified predictor. When applied with care to avoid overfitting and information leakage, it consistently delivers performance gains that justify its additional complexity across a wide range of machine learning tasks.



Top Contributors
Community content improvers