Sub topic 3
Stacking versus Blending Ensembles: Which to Use
Assume you know supervised learning, train/validation/test splits, cross-validation, overfitting, and basic scikit-learn. Both methods train base learners, then train a meta-learner on their predictions.
Stacking versus blending
Stacking creates meta-features with out-of-fold predictions. Each training row receives a prediction from a model that did not train on that row. This uses most training data efficiently but requires cross-validation.
Blending trains base learners on one subset and trains the meta-learner on predictions for a separate holdout subset. It is simpler and faster, but fewer examples train each stage.
| Method | Meta-training data | Main advantage | Main cost |
|---|---|---|---|
| Stacking | Out-of-fold predictions | Uses data efficiently | More computation |
| Blending | Holdout predictions | Simple implementation | Wastes holdout data |
Stacking with scikit-learn
StackingClassifier performs the cross-validation needed to create training predictions for the meta-learner.
from sklearn.ensemble import StackingClassifier, RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
X, y = make_classification(n_samples=1000, n_features=12, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=0
)
models = [
("forest", RandomForestClassifier(n_estimators=100, random_state=0)),
("knn", make_pipeline(StandardScaler(), KNeighborsClassifier())),
]
stack = StackingClassifier(
estimators=models,
final_estimator=LogisticRegression(max_iter=1000),
cv=5,
stack_method="predict_proba",
)
stack.fit(X_train, y_train)
print(stack.score(X_test, y_test))Meta-learners and raw features
Start with a simple, regularized meta-learner such as logistic regression. Regularization limits overly large coefficients and reduces overfitting. Probabilities usually provide more information than hard class labels because they express confidence.
With passthrough=True, scikit-learn gives the meta-learner both the model predictions and the original features. This can help when base learners miss useful patterns, but it increases dimensionality and overfitting risk.
Honest evaluation and deeper stacks
Keep the test set untouched until every model choice is complete. Use identical test data and metrics for each individual model and ensemble. For time-ordered data, use time-based splits rather than ordinary shuffled folds. Training on future observations can leak information.
Multi-level stacking adds another stack above the first meta-learner. It can capture additional patterns, but each level increases computation, tuning complexity, and leakage risk. Extra levels often provide diminishing returns. Stacking is not worthwhile when models are highly correlated, training is expensive, or the improvement is smaller than operational cost.
Exercise
Which method is usually preferable when training data is limited and computation is available?
Show answer
Stacking, because out-of-fold predictions let both the base learners and meta-learner use the data more efficiently.