Sub topic 11
Regression trees
This chapter assumes you know how a classification tree is built by recursive partitioning, what a split threshold is, and how to fit a model in scikit-learn. A regression tree uses the same machinery to predict a number instead of a class.
What changes from classification
Only two parts of the algorithm change.
- Impurity becomes variance. A node's impurity is the variance of its target values, which is the mean squared error (MSE) around the node mean. Gini and entropy no longer apply.
- Leaves predict a mean. A leaf returns the average target of the training samples that reached it.
Everything else stays the same. The tree still tries thresholds, keeps the best one greedily, and recurses until a stopping rule fires.
Variance reduction
A split is scored like information gain, but with variance. The score is the parent variance minus the sample-weighted variance of the two children.
Take four targets: 1, 2, 9, 10.
- The mean is 5.5, so the parent variance is (20.25 + 12.25 + 12.25 + 20.25) / 4 = 16.25.
- Split between 2 and 9, at threshold 5.5. Each child has variance 0.25.
- The weighted child variance is 0.25, so the variance reduction is 16.25 − 0.25 = 16.0.
The tree takes whichever threshold gives the largest reduction. Equivalently, it picks the lowest weighted child variance, as best_threshold did with Gini earlier.
Fitting one in scikit-learn
The class is DecisionTreeRegressor. Its default criterion is "squared_error".
import numpy as np
from sklearn.tree import DecisionTreeRegressor
rng = np.random.RandomState(0)
X = np.sort(5 * rng.rand(80, 1), axis=0)
y = np.sin(X).ravel() + 0.1 * rng.randn(80)
reg = DecisionTreeRegressor(max_depth=3, min_samples_leaf=5, random_state=0)
reg.fit(X, y)
print(reg.predict([[1.0], [2.5], [4.0]]))With max_depth=3 there are at most 8 leaves, so the model can output at most 8 distinct values. Plotted, the prediction is a staircase that approximates the sine curve.
Common errors
- No extrapolation. Outside the training range, a tree returns the mean of the nearest edge leaf. If
xcan reach 10 but you trained on 0 to 5, every prediction forx > 5is the same constant. A linear model would continue the trend, and a tree will not. - Fitting noise. With no depth limit, each leaf can hold a single sample, so the tree memorizes the noisy targets. Training error is then near zero, and validation error is high. Use
min_samples_leaf,max_depth, orccp_alpha, and check with cross-validation. - Using accuracy. Regression has no accuracy.
reg.scorereturns R², and cross-validation can usescoring="neg_mean_squared_error". The "neg" prefix exists so that higher is always better. - Scaling the target or features. Neither is needed. Thresholds depend only on the ordering of feature values.
Why leaf size matters more here
In classification, a pure leaf is a good outcome. In regression, a leaf with one sample is almost always noise, because a single target value has no averaging to smooth it. This is why min_samples_leaf is the most useful regularizer for regression trees. A leaf of 5 or more samples averages out part of the noise.
Exercise
A regression tree is trained on house sizes from 50 to 200 square meters. What does it predict for a 400 square meter house, and why?
Show answer
It predicts the mean target of the leaf that contains the largest training sizes, the same value it would give for a 200 square meter house. Every threshold in the tree lies inside the training range, so any input above the last threshold lands in the same rightmost leaf. Trees cannot extrapolate a trend.
Exercise
Parent targets are 4, 6, 10, 12. Compute the variance reduction for the split {4, 6} versus {10, 12}.
Show answer
The parent mean is 8, so the parent variance is (16 + 4 + 4 + 16) / 4 = 10. Each child has variance 1, so the weighted child variance is 1. The reduction is 10 − 1 = 9.