Sub topic 9
Blending with a holdout set
This chapter assumes you know k-fold cross-validation, the difference between training, validation and test data, and basic scikit-learn use. It also assumes you have seen why a meta-learner must not train on predictions the base models made for their own training rows.
The idea
Blending is the simpler cousin of stacking. Stacking builds out-of-fold predictions with cross-validation. Blending sets aside one holdout slice of the training data and uses only that slice to train the meta-learner.
The procedure has four steps:
- Split the training data into a fit part and a holdout part.
- Train every base model on the fit part only.
- Predict on the holdout part. These predictions are the meta-features.
- Train the meta-learner on the holdout meta-features and the holdout labels.
The base models never saw the holdout rows. Their predictions there are honest, so the meta-learner learns how reliable each model is on unseen data. This avoids the leakage problem from the earlier chapter.
A complete example
This script runs as shown. It uses a 70/30 split of the training data.
import numpy as np
from sklearn.base import clone
from sklearn.datasets import make_classification
from sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier
from sklearn.linear_model import LogisticRegression
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=2000, n_features=20, n_informative=8, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)
base = [
("rf", RandomForestClassifier(n_estimators=200, random_state=0)),
("gb", GradientBoostingClassifier(random_state=0)),
("knn", make_pipeline(StandardScaler(), KNeighborsClassifier())),
]
# Step 1: split the training data into a fit part and a holdout part.
X_fit, X_hold, y_fit, y_hold = train_test_split(
X_train, y_train, test_size=0.3, random_state=1
)
# Step 2: train each base model on the fit part only.
fitted = [clone(model).fit(X_fit, y_fit) for _, model in base]
# Step 3: holdout predictions become the meta-features.
hold_meta = np.column_stack([m.predict_proba(X_hold)[:, 1] for m in fitted])
# Step 4: train the meta-learner on the holdout part.
meta = LogisticRegression(max_iter=1000).fit(hold_meta, y_hold)
# Test time: the same fitted base models feed the meta-learner.
test_meta = np.column_stack([m.predict_proba(X_test)[:, 1] for m in fitted])
print("blend accuracy:", meta.score(test_meta, y_test))Each column of hold_meta is one model's probability of class 1. With a binary problem, keeping one column per model is enough, because the other column is its complement.
What you give up
Blending has no repeated fitting. Each base model trains once, so it costs less than 5-fold stacking. It is also easy to reason about, because the holdout is clearly separate.
The cost is data. Each base model sees only 70% of the training rows, and the meta-learner sees only the remaining 30%. With a small holdout, the meta-learner's weights are noisy, and the result depends on which rows landed in the holdout.
Two details are easy to get wrong. Use the base models fitted on the fit part for test predictions. If you refit them on all training data, their outputs shift and no longer match what the meta-learner learned. Also, shuffle or stratify the split for ordinary data, but keep time order if the rows are time-ordered.
Exercise
In blending, why must the base models be trained only on the fit part and not on the whole training set?
Show answer
If a base model trained on the holdout rows, its predictions there would be overly good, because it had seen those labels. The meta-learner would then overtrust that model. Training on the fit part keeps the holdout predictions honest.