Lesson 33
Gradient Boosting Classifier
Add shallow trees that reduce the classification loss, each one trained on the errors left by the trees before it. One example is on this page.
Intermediate12 min
What You Will Learn
- Idea
- Example
- When to use it
Idea
Boosting for classification fits the next tree to the gradient of the loss, not to a raw residual of 0 and 1. Early trees set the broad boundary. Later trees fix the rows that are still uncertain. A small learning rate keeps any one tree from taking over.
Example
import numpy as np
from sklearn.ensemble import GradientBoostingClassifier
X = np.array([[1], [2], [3], [7], [8], [9]], dtype=float)
y = np.array([0, 0, 0, 1, 1, 1])
model = GradientBoostingClassifier(
n_estimators=20, max_depth=1, learning_rate=0.2, random_state=0
)
label = model.fit(X, y).predict([[6]])[0]
print(f"label: {int(label)}")Output:
label: 1When to use it
- Use it when a forest is good and you want a tighter fit on structured data.
- Shallow trees plus a small learning rate is the stable setup.
- Watch a validation score. Extra rounds eventually fit noise.
The theory lesson Gradient Boosting walks through the sequence.
Loading playground…