Sub topic 5
Meta-features: probabilities or labels
This chapter assumes you know how out-of-fold (OOF) predictions work and can fit a scikit-learn classifier. It covers one design choice: what each base model hands to the meta-learner.
What a meta-feature is
A meta-feature is a column in the meta-learner's training table. Each column comes from one base model's prediction for a row. For classification, you can pass either of two things:
- Labels: the predicted class, such as 0 or 1.
- Probabilities: the model's confidence for each class, such as 0.83.
Why probabilities usually win
A label throws away how sure the model was. Suppose two models predict class 1. One gives 0.51 and the other gives 0.99. As labels, they look identical. As probabilities, the meta-learner can trust the second far more.
Probabilities also let the meta-learner learn weights. It can learn that the random forest deserves more weight than the k-nearest-neighbors model. With labels it can mostly only count votes.
| Meta-feature | Columns per model (binary) | Keeps confidence | Typical use |
|---|---|---|---|
| Labels (`predict`) | 1 | No | Models with no usable probabilities |
| Probabilities (`predict_proba`) | 1 (class 1 only) | Yes | Default choice |
In the binary case the two probability columns sum to 1. Keeping both would add redundancy, so scikit-learn drops one. With k classes (k > 2), each model contributes k columns.
Comparing both in scikit-learn
The stack_method argument of StackingClassifier selects the meta-feature type. The code below fits one stack per setting on the same data.
from sklearn.datasets import make_classification
from sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier, StackingClassifier
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())),
]
for method in ["predict", "predict_proba"]:
stack = StackingClassifier(
estimators=base,
final_estimator=LogisticRegression(max_iter=1000),
cv=5,
stack_method=method,
)
stack.fit(X_train, y_train)
print(method, "accuracy:", round(stack.score(X_test, y_test), 4))Expect the probability version to match or beat the label version. The gap depends on the data, so run it rather than assume it.
Common errors
- Using labels by default. This discards information. Choose labels only when a base model cannot produce probabilities.
- Uncalibrated probabilities. Some models, such as random forests and support vector machines, give probabilities that do not match real frequencies. A flexible meta-learner can partly correct this. A strict one cannot.
- Too many columns. With many classes and many base models, the meta-feature table gets wide. Keep the meta-learner simple and regularized.
- Mixing train and test conditions. The meta-learner must train on OOF predictions. At test time it receives predictions from base models refit on all training data. Both must use the same method.
Exercise
You stack 3 base models on a problem with 5 classes and use stackmethod="predictproba". How many meta-feature columns does the meta-learner receive? How many would it receive with stack_method="predict"?
Show answer
With predict_proba, each model gives 5 probability columns, so 3 × 5 = 15 columns. With predict, each model gives 1 label column, so 3 columns. The probability version keeps confidence information but gives the meta-learner a wider table.