AdaBoost
AdaBoost (adaptive boosting) trains a sequence of tiny trees. After each one, it increases the weight of the rows that were misclassified, so the next tree concentrates on the hard cases. Better trees get a bigger say in the final vote.
Your Path Through This Lesson
0 of 5 done
- Walk through one round of AdaBoost by hand.
- Compute a stump's "amount of say" and the new row weights.
- Train and tune
AdaBoostClassifier.
The Algorithm
AdaBoost usually uses stumps: trees with a single question. Each stump on its own is barely better than guessing.
- Give every training row the same weight, .
- Train a stump using these weights.
- Compute its weighted error : the total weight of the rows it got wrong.
- Give the stump an amount of say:
- Update the weights: multiply misclassified rows by and correct rows by , then rescale so they add up to 1.
- Repeat from step 2.
Prediction: every stump votes, and each vote counts times. The class with the larger total wins.
The Amount of Say
| Weighted error | Meaning | |
|---|---|---|
| 0.01 | 2.30 | Almost perfect: a big say |
| 0.30 | 0.42 | Decent |
| 0.50 | 0 | No better than a coin flip: no say |
| 0.70 | −0.42 | Worse than a coin flip: its vote is flipped |
One Round by Hand
Ten rows, each starting with weight 0.1. The first stump gets 3 rows wrong, so .
New weights before rescaling:
- Each wrong row: . Three of them: 0.458.
- Each right row: . Seven of them: 0.458.
Rescale so the total is 1 (divide by 0.916):
- Each wrong row: 0.167. Each right row: 0.071.
Each misclassified row now weighs more than twice as much as a correct one. In fact, the three hard rows now hold exactly half of the total weight, so the next stump can't ignore them.
import numpy as np
w = np.full(10, 0.1)
wrong = np.array([1, 1, 1, 0, 0, 0, 0, 0, 0, 0], dtype=bool)
eps = w[wrong].sum()
alpha = 0.5 * np.log((1 - eps) / eps)
w = w * np.where(wrong, np.exp(alpha), np.exp(-alpha))
w = w / w.sum()
print(f"alpha = {alpha:.3f}")
print("new weights:", w.round(3))
print("weight on the wrong rows:", round(w[wrong].sum(), 3))alpha = 0.424
new weights: [0.167 0.167 0.167 0.071 0.071 0.071 0.071 0.071 0.071 0.071]
weight on the wrong rows: 0.5In scikit-learn
from sklearn.datasets import load_breast_cancer
from sklearn.ensemble import AdaBoostClassifier
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0, stratify=y)
stump = DecisionTreeClassifier(max_depth=1).fit(X_train, y_train)
print(f"one stump : test accuracy {stump.score(X_test, y_test):.3f}")
for n in [10, 50, 200]:
ada = AdaBoostClassifier(DecisionTreeClassifier(max_depth=1), n_estimators=n, learning_rate=0.5, random_state=0)
ada.fit(X_train, y_train)
print(f"AdaBoost {n:>3} stumps: test accuracy {ada.score(X_test, y_test):.3f}")one stump : test accuracy 0.889
AdaBoost 10 stumps: test accuracy 0.942
AdaBoost 50 stumps: test accuracy 0.930
AdaBoost 200 stumps: test accuracy 0.947One question gets about nine in ten tumours right; a few dozen weighted stumps do much better.
| Setting | Meaning |
|---|---|
estimator |
The weak learner (default: a stump) |
n_estimators |
Number of boosting rounds |
learning_rate |
Shrinks each stump's say; smaller values need more rounds but often generalise better |
Because AdaBoost keeps raising the weight of misclassified rows, mislabelled data and outliers can end up dominating training. If your labels are noisy, gradient boosting with a robust loss, or a random forest, is often safer.
- Redo the hand calculation for a stump that gets 1 of 10 rows wrong. What is α, and what weight does the wrong row end up with?
- Try
learning_rate=1.0and0.1with 200 stumps. Which works better on the test set?
AdaBoost re-weights the rows after each stump so the next one focuses on the mistakes, and gives accurate stumps a bigger vote.
Summary
- AdaBoost chains stumps; each round re-weights rows towards the mistakes.
- Amount of say ; wrong rows × , right rows × .
- The final prediction is a weighted vote.
- Sensitive to outliers and label noise.
- Next: a more general form of boosting that fits the errors directly, gradient boosting.