Ensembles and AutoML
Combine runs you already have with no retraining, keep the score honest by cross-fitting, and let AutoML follow a plan you can read rather than a black box.
What runs in CodeSplash
- Linear / logistic regression Verified in Python and R
- Random forest Verified in Python and R
- XGBoost Verified in Python and R
You will be able to
- Compare simple averaging, greedy selection and stacking
- Explain why stacking weights must be cross-fitted
- Read an AutoML plan and decide if its result is worth keeping
Different mistakes
ReadAveraging helps when models err differently. A logistic model and a forest see the data in different ways, so their errors cancel better than two forests'.
Three models on the same Pima folds: 0.857, 0.835 and 0.843. That's the raw material. The app builds an ensemble from their saved predictions with no retraining, so no engine is needed.
Three ways to combine
Read| Ensemble | What it does |
|---|---|
| Simple average | Equal weights: a robust baseline for similar models |
| Greedy selection | Adds members one at a time while the cross-validated score improves (Caruana) |
| Stacking | Learns non-negative weights on out-of-fold predictions, cross-fitted |
Each needs models that share a split plan. Rows are matched by fold and row, so a run with different folds can't join.
Where's the leak?
PredictPredict first
You learn stacking weights from out-of-fold predictions, then score the stack on those same predictions. Why is that score optimistic?
Reveal
The weights have seen the rows they score. Each member's predictions are honest, yet the weights were chosen to fit them. So the app cross-fits: the weights applied to one fold come from the other folds only. Test rows use weights learned on all out-of-fold rows.
Stacking weights
ReadView data
| member | weight |
|---|---|
| Gradient boosting | 0.5200 |
| Elastic net | 0.3100 |
| Random forest | 0.1700 |
| k-nearest neighbors | 0 |
Weights are non-negative and sum to one, so the stack is a blend, and it stays in range. A member with weight near zero wasn't adding anything to the others, which can happen even when its own score is good. Don't read that as "this model is bad".
Averaging by hand
ExploreEnsembles don't remove bias, which is what you see when you bag stumps. Set Tree depth to 1, then to 6, and read Variance, the bag.
DExplorable · Averaging models, and going past the interpolation point
When can adding more model lower the error instead of raising it?
Classical advice says more model means more variance. Going much further here (25 trees instead of one, or six times as many features as rows) will make predictions on new data…
Experiment
Commit your prediction above to see the result
Make your prediction to see the result.
AutoML, in the open
ReadAutoML… runs a plan you can read first:
- Baselines.
- Candidates chosen by three dials: Accuracy, Time and Interpretability.
- A race on the first folds.
- Tuning for the leaders.
- Optionally, an ensemble.
- A recommendation.
The dialog lists Baselines, Candidates and Left out, each with a reason. Only the selected models keeps it to your own choices. It's all ordinary runs on your plan, so everything shows up on the leaderboard.
Worth the complexity?
ReflectAn ensemble of six models beats your best single model by a small amount on the leaderboard. To keep it you'd need to store six models, and explaining it gets harder because the Explanations tab isn't available for ensembles. What gain would justify that? Who would have to maintain it?
Check your understanding
CheckpointCheck yourself
Which pair is likely to gain the most from averaging?
Show the answer
Answer: b
Correlation sets the floor. Similar quality with different errors is the best case, and a weak member only drags things down.
Check yourself
What makes a stack's cross-validated score honest in the app?
Show the answer
Answer: a
That's cross-fitting. Weights fitted on the same rows they score would flatter the blend.
Check yourself
You set Interpretability to its top setting in AutoML. What should you expect?
Show the answer
Answer: a
The dial's top setting reads "Glass-box models only". The plan is deterministic and spells out each choice, before anything runs.
Build one
In the appOn the Leaderboard, tick at least two runs, open Ensemble and pick a method. The result appears with an ensemble badge. To go further, press AutoML… in the Learners step, move the dials and read the plan before you start it.
import pandas as pd
from sklearn.ensemble import RandomForestClassifier, StackingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import StratifiedKFold, cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
df = pd.read_csv("pima.csv")
X, y = df.drop(columns="diabetic"), df["diabetic"]
members = [("logit", make_pipeline(StandardScaler(), LogisticRegression())),
("forest", RandomForestClassifier(n_estimators=200, random_state=1))]
outer = StratifiedKFold(5, shuffle=True, random_state=1)
for name, m in members + [("stack", StackingClassifier(members, cv=5))]:
print(name, cross_val_score(m, X, y, cv=outer, scoring="roc_auc").mean().round(3))library(ranger)
df <- rbind(MASS::Pima.tr, MASS::Pima.te)
auc <- function(y, p) as.numeric(pROC::auc(y, p, quiet = TRUE))
set.seed(1)
fold <- sample(rep(1:5, length.out = nrow(df)))
p1 <- p2 <- numeric(nrow(df))
for (k in 1:5) {
tr <- df[fold != k, ]; te <- df[fold == k, ]
p1[fold == k] <- predict(glm(type ~ ., binomial, tr), te, type = "response")
p2[fold == k] <- predict(ranger(type ~ ., tr, num.trees = 200, probability = TRUE, seed = 1), te)$predictions[, "Yes"]
}
round(c(logit = auc(df$type, p1), forest = auc(df$type, p2), average = auc(df$type, (p1 + p2) / 2)), 3)Recap
Recap- Ensembles combine saved predictions: average, greedy selection or cross-fitted stacking.
- They pay when members err differently, at a real cost in maintenance and explanation.
- AutoML is a plan of ordinary runs, with a reason for each choice. Read it first.
Concepts in this lesson