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≤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 pk is the fraction of class k in a node, its Gini impurity is G=1−∑kpk2. A pure node scores 0; a node split evenly between two classes scores 0.5. For children of sizes nL and nR, compare splits using
Gsplit=nnLGL+nnRGR,n=nL+nR.
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.
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=−∑kpklog2pk, 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?