Sub topic 10
Stacking versus blending: which to use
This chapter assumes you know k-fold cross-validation, the leakage problem from earlier chapters, and how out-of-fold predictions feed a meta-learner. Here you compare the two ways of building that meta-learner's training data and pick one for a project.
The core difference
Both methods train a meta-learner on predictions that the base models made on rows they did not train on. They differ in where those rows come from.
- Stacking uses k-fold cross-validation on the whole training set. Every training row gets one out-of-fold prediction from each base model. The meta-learner trains on all of those rows.
- Blending splits the training set once. Base models train on the first part. The meta-learner trains on their predictions for the second part, the holdout.
Trade-offs
| Aspect | Stacking | Blending |
|---|---|---|
| Data for meta-learner | Every training row | Only the holdout rows |
| Data for base models | Each fold model sees (k-1)/k of the data, then a final refit sees all of it | Only the fit part, with no refit |
| Cost | About k+1 fits per base model | One fit per base model |
| Code | More moving parts | A few lines |
| Variance of meta-learner | Lower, because it sees more rows | Higher, especially with a small holdout |
| Leakage risk | Low if folds are done correctly | Low, and easy to see by inspection |
Stacking also has a subtle mismatch. The meta-learner trains on predictions from models fit on (k-1)/k of the data, but at test time it receives predictions from models refit on all of it. Those test-time predictions are slightly better than what the meta-learner saw. In practice this rarely hurts, and it shrinks as k grows.
Blending avoids that mismatch because the same fitted models produce both the holdout predictions and the test predictions. Its cost is that both stages see less data.
A practical comparison
The two earlier examples use the same data and base models, so you can compare them directly. Run them one after another and read the printed accuracies:
# Assumes the stacking, manual stack, and blend examples above have run.
print("sklearn stack :", stack.score(X_test, y_test))
print("manual stack :", meta_stack.score(test_meta_stack, y_test))
print("blend :", meta_blend.score(test_meta_blend, y_test))This snippet needs the meta-learner and test matrix from each example saved under distinct names, because the examples reuse meta and test_meta. Rename them as shown, or print each score right after its example. Expect the three numbers to be close. A gap of a few tenths of a percentage point on 500 test rows is within noise, so do not rank the methods on one split.
Which to choose
Choose stacking when:
- The dataset is small or medium, so every row matters.
- Training time is affordable. Five folds multiply the cost of slow base models.
- You want the most stable meta-learner.
Choose blending when:
- The dataset is large, so a holdout of 10 to 30 percent still gives the meta-learner thousands of rows.
- Base models are expensive, and a single fit each is all you can afford.
- You need a pipeline that is simple to explain and audit.
Exercise
You have 1,000 labeled rows and three base models that each take a few seconds to train. Would you choose stacking or blending, and why?
Show answer
Stacking. With only 1,000 rows, blending would leave perhaps 300 rows for the meta-learner and fewer for the base models. Training is cheap, so the extra k-fold fits cost little, and stacking gives the meta-learner all 1,000 out-of-fold rows.
Exercise
Why do stacking's meta-learner inputs at test time differ slightly in quality from its training inputs?
Show answer
During training it sees out-of-fold predictions from models fit on (k-1)/k of the data. At test time the base models are refit on the full training set, so their predictions are slightly more accurate than the ones the meta-learner learned from.