Sub topic 1
Decision Trees as Weak Learners
Prerequisites
This chapter assumes you know Python and NumPy basics, the idea of features, labels and a train/validation split, and what overfitting means. You should also be comfortable with a decision tree at the level of "a series of yes/no questions about features."
What a regression tree does
A decision tree splits the feature space into regions. Each internal node asks a question such as x[3] < 2.5. Each leaf holds one constant prediction. For regression, that constant is the mean of the training targets that fall into the leaf.
The tree picks each split greedily. For every candidate feature and threshold, it measures how much the split reduces the squared error in the two child nodes. It keeps the best split and repeats on each child until a stopping rule applies, such as a maximum depth.
Weak learners
A weak learner is a model that is only slightly better than a naive baseline. A shallow tree fits this description. A tree with max_depth=1 (a stump) makes one split and predicts two values. It cannot capture much structure on its own.
A weak learner has high bias. It underfits. Boosting combines many such learners so that the sum can model complex patterns. This is the opposite of a fully grown tree, which has low bias but high variance because it memorizes noise.
| Tree type | Bias | Variance | Typical use |
|---|---|---|---|
| Stump (depth 1) | High | Low | Very conservative boosting |
| Shallow (depth 3 to 6) | Medium | Low to medium | Standard boosting |
| Fully grown | Low | High | Bagging, such as random forests |
Seeing the effect of depth
The code below fits trees of different depths to a noisy sine curve. It reports training and test error.
import numpy as np
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
rng = np.random.default_rng(0)
X = rng.uniform(0, 6, size=(400, 1))
y = np.sin(X[:, 0]) + rng.normal(0, 0.3, size=400)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=0
)
for depth in [1, 3, None]:
tree = DecisionTreeRegressor(max_depth=depth, random_state=0)
tree.fit(X_train, y_train)
train_mse = mean_squared_error(y_train, tree.predict(X_train))
test_mse = mean_squared_error(y_test, tree.predict(X_test))
print(f"depth={depth}: train={train_mse:.3f} test={test_mse:.3f}")Expect the stump to have high error on both sets. The unlimited tree should reach a training error near zero while its test error is worse than that of the depth-3 tree. The depth-3 tree sits between these cases.
How trees fit into boosting
The next chapters show how each new tree is trained on the errors left by the previous ones.
Common errors
- Using deep trees in boosting. Deep trees overfit quickly when added one after another. Start with a depth of 3 to 6.
- Judging a tree by training error. An unlimited tree reaches near-zero training error and says nothing about generalization.
- Expecting a stump to be useless. On its own it is weak, but many stumps can be a strong ensemble.
Exercise
Why does boosting use shallow trees instead of fully grown ones?
Show answer
Shallow trees have high bias but low variance, so they are weak learners. Boosting reduces bias by adding many of them one at a time, each correcting the earlier errors. Fully grown trees already have low bias and high variance, so adding them would tend to overfit.