Skip to content

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.

Advanced ~12 min10 steps

What runs in CodeSplash

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

Read first

01

Different mistakes

Read

Averaging 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.

02

Three ways to combine

Read
EnsembleWhat it does
Simple averageEqual weights: a robust baseline for similar models
Greedy selectionAdds members one at a time while the cross-validated score improves (Caruana)
StackingLearns 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.

03

Where's the leak?

Predict

Predict first

You learn stacking weights from out-of-fold predictions, then score the stack on those same predictions. Why is that score optimistic?

Nothing: out-of-fold predictions are honestThe weights have seen the rows they scoreStacking only works on test rows
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.

04

Stacking weights

Read
Ensemble weightsHow much does each member contribute to the ensemble?
View data
memberweight
Gradient boosting0.5200
Elastic net0.3100
Random forest0.1700
k-nearest neighbors0

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".

05

Averaging by hand

Explore

Ensembles 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…

How sure?

Experiment

25
6
40
0.30
Simulating…

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

06

AutoML, in the open

Read

AutoML… runs a plan you can read first:

  1. Baselines.
  2. Candidates chosen by three dials: Accuracy, Time and Interpretability.
  3. A race on the first folds.
  4. Tuning for the leaders.
  5. Optionally, an ensemble.
  6. 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.

07

Worth the complexity?

Reflect

An 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?

08

Check your understanding

Checkpoint

Check 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.

09

Build one

In the app

On 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.

Python
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))
R
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)
10

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