Sub topic 1
Decision Trees as the Building Block of Random Forests
A random forest combines many decision trees. This chapter assumes you know features, labels, training and test sets, and basic classification metrics.
Prerequisites.
You should understand supervised learning, NumPy-style arrays, and the difference between classification and regression.
Decision trees as the building block
A decision tree makes predictions by asking a sequence of questions about features. Each question splits the data into smaller groups. A leaf is the final group that stores a prediction.
For classification, a leaf usually predicts its most common class. For regression, it usually predicts the mean target value in that leaf.
How a tree picks its splits
At each node, the tree tests candidate features and threshold values. It chooses the split that produces the largest reduction in impurity.
Impurity measures how mixed a node is:
- Gini impurity measures the chance of assigning the wrong class if a label is sampled according to the node's class proportions.
- Entropy measures uncertainty in the class distribution.
- Variance reduction is commonly used for regression. It favors splits that make target values within each child more similar.
The tree evaluates a weighted impurity for the child nodes. It repeats this process until a stopping rule applies, such as maximum depth or minimum leaf size.
Why single trees overfit: bias and variance
A fully grown tree often has low bias: it can represent complicated patterns. However, it also has high variance. Small changes in training data can produce a very different tree. This causes overfitting, where training accuracy is high but test accuracy is worse.
Averaging several varied trees reduces variance while keeping much of the flexibility of individual trees.
Bootstrap sampling
A bootstrap sample is created by drawing training rows randomly with replacement. A sample has the same number of rows as the original data, but some rows appear repeatedly and others are absent. On average, about 63% of unique rows appear in a bootstrap sample.
Bagging: training on resampled data
Bagging, short for bootstrap aggregating, trains one model on each bootstrap sample. For classification, it combines predictions by majority vote. For regression, it averages predictions. The samples make the trees different, so their errors can partially cancel.
> callout note > Bagging reduces variance most effectively when the models are accurate but make different errors.
Exercise
Why can averaging fully grown trees improve test performance?
Show answer
The trees may each overfit differently, so averaging their predictions reduces their shared variance while preserving useful nonlinear patterns.