Sub topic 13
Passing raw features to the meta-learner
This chapter assumes you can build a StackingClassifier, you know what out-of-fold predictions are, and you are comfortable with regularized logistic regression. Here you will learn what the passthrough option does, when it helps, and when it hurts.
What passthrough does
By default, the meta-learner sees only the predictions of the base models. With passthrough=True, scikit-learn appends the original input features to those predictions. If you have 3 base models and 20 features, the meta-learner trains on 3 + 20 = 23 columns.
The raw features are not out-of-fold. They are the same values the base models saw, and they carry no leakage on their own. Only the prediction columns need the cross-validation treatment.
Why it can help
Base models may be better in some regions of the feature space than in others. A meta-learner that sees only predictions can weight the models by how much they agree, but it cannot tell where it is in the input space. With raw features, it can learn rules such as "trust the k-nearest-neighbors model when feature 3 is small".
A linear meta-learner can only add a feature's effect to the predictions. It cannot make a model's weight depend on that feature. For region-dependent weighting, use a meta-learner that models interactions, such as a shallow gradient-boosted tree model.
Why it can hurt
- More inputs, more variance. The meta-learner now has dozens or hundreds of extra columns, while the prediction columns are often only a handful. It can overfit the raw features instead of using the predictions.
- Scale mismatch. Probabilities lie between 0 and 1. Raw features may not. A regularized linear meta-learner penalizes columns unevenly unless you scale them.
- Duplicated work. The base models already extracted most of the signal from the raw features. Passing them again often adds noise.
Trying it
The example below compares both settings with the same base models. It uses a scaled logistic regression as the meta-learner, so the penalty treats all columns fairly.
from sklearn.datasets import make_classification
from sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier, StackingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
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)
base = [
("rf", RandomForestClassifier(n_estimators=200, random_state=0)),
("gb", GradientBoostingClassifier(random_state=0)),
("knn", make_pipeline(StandardScaler(), KNeighborsClassifier())),
]
for passthrough in (False, True):
stack = StackingClassifier(
estimators=base,
final_estimator=make_pipeline(
StandardScaler(), LogisticRegression(C=0.1, max_iter=1000)
),
cv=5,
stack_method="predict_proba",
passthrough=passthrough,
)
scores = cross_val_score(stack, X, y, cv=5)
print(f"passthrough={passthrough}: {scores.mean():.3f} +/- {scores.std():.3f}")Compare the mean score against the standard deviation. If the difference is smaller than the spread across folds, you have no evidence that passthrough helped.
When to use it
Start without passthrough. Try it when you have a meta-learner that can use the raw features, when you have plenty of rows relative to columns, and when the nested cross-validation score improves by more than its noise. Keep the choice if it wins, and drop it if it does not.
Exercise
You turn on passthrough with a logistic regression meta-learner. The training score rises, but the cross-validated score falls. What is the most likely cause, and what are two ways to respond?
Show answer
The meta-learner is overfitting the many extra raw-feature columns. Respond by turning passthrough off, or by regularizing more strongly (a smaller C) after scaling the inputs. Switching to a meta-learner that can use interactions is a third option, but only if the cross-validated score then improves.