ContentsChapter 7 · Trees and ensembles1 of 3

7.1

Decision trees

In this chapter
  1. 7.1Decision trees
  2. 7.2Bagging and random forests
  3. 7.3Boosting
Before you readYou can ask one yes/no question about a water sample before predicting whether it is safe. What makes one question better than another?
Watch: Decision Trees · StatQuest with Josh Starmer

A decision tree tests one feature at a time. A test such as x≤tx \le t sends each sample down one of two branches. Repeat until you reach a leaf, which predicts the most common training label there. To train the tree, start with all the samples and choose the split that makes the two groups least mixed.

If pkp_k is the fraction of class kk in a node, its Gini impurity is G=1−∑kpk2G = 1 - \sum_k p_k^2 A pure node scores 00 a node split evenly between two classes scores 0.50.5 For children of sizes nLn_L and nRn_R compare splits using

Gsplit=nLnGL+nRnGR,n=nL+nR.G_{\text{split}} = \frac{n_L}{n}G_L + \frac{n_R}{n}G_R, \qquad n = n_L + n_R.

A pure group of one sample counts less than a pure group of seven. The best split is the one with the smallest weighted impurity, or equivalently the largest drop from the parent’s impurity.

A1A2A3B4A5B6B7B8
x≤4.5x \le 4.5
YesNo3 A · 1 B1 A · 3 B
Yes: 3 A · 1 BNo: 1 A · 3 BWeighted Gini: 0.375
The eight samples are sorted by feature value. Move the threshold between neighbours. Can you find a split that makes one branch pure without leaving just one sample there?

You can also measure impurity with entropy, H=−∑kpklog⁡2pkH = -\sum_k p_k \log_2 p_k with a zero contribution from any absent class. The parent’s entropy minus the weighted child entropy is information gain. A regression tree uses the same split-and-repeat idea, but minimises squared error about the child means and predicts the mean at each leaf.

The tree picks the best split at the current node. It doesn’t look ahead to see whether a different split would make a better whole tree. Let it grow until every leaf is pure and it may learn quirks of the training set. Limiting depth or requiring more samples in each leaf can help it generalise.

Recall
Why weight child impurity by group size?
Recall
What is information gain at a split?
Exercise
A node contains four samples of each class. A split makes a pure group of two and a group with two samples of one class and four of the other. By how much does Gini impurity drop?
Exercise
A node has two samples of each class. A split sends both samples of the first class left and both samples of the second class right. What is the information gain in bits?