Sub topic 2
How a tree picks its splits
This chapter assumes you know what a decision tree is (a sequence of yes/no questions on features) and can read basic NumPy. Here you will see how a tree chooses each question.
The goal of a split
A split is a rule like feature <= threshold. It sends samples to a left or right child. A good split makes each child purer than its parent, meaning its samples mostly share one label. The tree needs a number that measures purity. That number is the impurity criterion.
Impurity measures
For classification, let p_k be the fraction of samples in a node that belong to class k.
- Gini impurity is 1 minus the sum of p_k squared. It is 0 for a pure node and 0.5 for a 50/50 binary node.
- Entropy is minus the sum of pk times log2(pk). It is 0 for a pure node and 1 bit for a 50/50 binary node.
For regression, the usual measure is the variance of the target in the node. A split that lowers variance is a split that groups similar target values.
Scoring a candidate split
A split is scored by how much it reduces impurity. Each child's impurity is weighted by its share of the samples:
decrease = impurity(parent) − (nleft/n × impurity(left) + nright/n × impurity(right))
Take a node with 10 samples, 5 positive and 5 negative. Its Gini impurity is 0.5. A candidate split produces these children:
| Child | Samples | Positive | Negative | Gini |
|---|---|---|---|---|
| Left | 4 | 4 | 0 | 0.000 |
| Right | 6 | 1 | 5 | 0.278 |
The weighted child impurity is 0.4 × 0 + 0.6 × 0.278 = 0.167. The decrease is 0.5 − 0.167 = 0.333.
The greedy search
At each node the tree tries every feature. For each feature, it sorts the values and tests thresholds halfway between neighboring distinct values. It keeps the split with the largest decrease, then repeats on each child. The tree never looks ahead or revisits a choice, so this procedure is greedy.
Running the search by hand
This code reproduces the example above for one feature.
import numpy as np
def gini(y):
_, counts = np.unique(y, return_counts=True)
p = counts / counts.sum()
return 1.0 - np.sum(p ** 2)
def best_threshold(x, y):
parent = gini(y)
best_t, best_gain = None, 0.0
values = np.unique(x)
for t in (values[:-1] + values[1:]) / 2:
left, right = y[x <= t], y[x > t]
child = (len(left) * gini(left) + len(right) * gini(right)) / len(y)
if parent - child > best_gain:
best_t, best_gain = t, parent - child
return best_t, best_gain
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = np.array([1, 1, 1, 1, 0, 0, 0, 0, 0, 1])
t, gain = best_threshold(x, y)
print(f"threshold={t}, gain={gain:.3f}")The output is threshold=4.5, gain=0.333. A real tree runs this loop for every feature and picks the winner.
Choosing a criterion in scikit-learn
DecisionTreeClassifier accepts criterion="gini" (the default), "entropy" or "log_loss". DecisionTreeRegressor uses criterion="squared_error" by default. Gini and entropy usually produce very similar trees, so the choice rarely changes accuracy much. Gini is slightly cheaper because it needs no logarithm.
Common errors
- Forgetting to weight the children. An unweighted average lets a tiny, pure child look as good as a large one.
- Reading the criterion as a quality score for the whole model. It only guides local choices inside the tree.
- Expecting a split on a high-cardinality feature, such as an ID column, to generalize. Many unique values give many thresholds, and some will fit the training data by chance.
Exercise
A node has 8 samples: 4 of class A and 4 of class B. A split sends 3 A and 1 B left, and 1 A and 3 B right. What is the Gini decrease?
Show answer
The parent Gini is 0.5. Each child has 4 samples with a 3/1 mix, so its Gini is 1 − (0.75² + 0.25²) = 0.375. The weighted child impurity is 0.5 × 0.375 + 0.5 × 0.375 = 0.375. The decrease is 0.5 − 0.375 = 0.125.