Sub topic 4
The meta-learner: a model that learns from models
This chapter assumes you know supervised learning, train/validation/test splits, and basic scikit-learn. It explains what the second-level model in a stacking ensemble does and how it differs from simple averaging.
From averaging to learning
Suppose you have three trained classifiers. The simplest way to combine them is to average their predicted probabilities. That treats every model as equally trustworthy everywhere. In practice a random forest may be reliable on one kind of example and a nearest-neighbor model on another.
A meta-learner (also called a level-2 model or final estimator) is an ordinary model whose inputs are the predictions of other models. The models that feed it are the base learners. The meta-learner learns from data how much to trust each base learner.
What the meta-learner sees
For each training example, the meta-learner receives one row. Each column is the prediction of one base learner. With three base learners and a binary problem, that is three columns, each holding a probability for class 1. The target is the same true label y that the base learners were trained on.
The meta-learner never needs the original features in its basic form. Its job is smaller than the base learners' job: weigh a handful of numbers.
A minimal example
The probabilities below are made up to show the mechanics. Each row is one example. The columns come from models A, B and C.
import numpy as np
from sklearn.linear_model import LogisticRegression
meta_X = np.array([
[0.9, 0.6, 0.8],
[0.2, 0.7, 0.3],
[0.8, 0.4, 0.7],
[0.1, 0.5, 0.2],
[0.7, 0.3, 0.9],
[0.3, 0.6, 0.1],
])
y = np.array([1, 0, 1, 0, 1, 0])
meta = LogisticRegression().fit(meta_X, y)
print("weights per base model:", meta.coef_)
print("intercept:", meta.intercept_)
print("blended probability:", meta.predict_proba([[0.85, 0.5, 0.75]])[:, 1])The coefficients are the learned trust. A large positive weight means the meta-learner follows that model. A weight near zero means it mostly ignores it.
Why a simple meta-learner usually wins
The meta-learner has few inputs and often few rows, so it overfits easily. Base-model predictions are also highly correlated with each other, which makes flexible meta-learners unstable. Common choices are:
- Logistic regression for classification.
- Ridge regression for regression.
- A small, shallow model only when you have plenty of data.
A linear meta-learner is also easy to inspect. You can read the weights and see which base learners matter. If a model gets a weight near zero, it adds little and may be worth removing.
What the meta-learner can and cannot fix
The meta-learner can correct for a base model that is over- or under-confident. It can also favor the stronger models. It cannot invent information that none of the base models captured. If all base learners make the same mistakes, the meta-learner has nothing to work with. This is why the diversity of base learners, covered earlier in the plan, sets the ceiling for the whole ensemble.
Exercise
A stack has five base learners and a logistic regression meta-learner. Two base learners get weights close to zero. What does this suggest, and what could you do?
Show answer
The meta-learner finds little extra information in those two models, probably because their predictions duplicate those of other models or are weak. You could drop them to cut training and prediction cost, and check that cross-validated performance does not get worse.