Machine Learning from Scratch · Classifying
Decision Trees: Finding the Best Question
A decision tree learns by asking yes/no questions. Try four questions of the form "is the size above t?" and measure which one separates apples from lemons best, with Gini impurity.
tree_split.py
# Fruits by size, and their kind: 1 = apple, 0 = lemon.
sizes = [2, 3, 4, 6, 7, 8]
kinds = [0, 0, 1, 1, 0, 1]
# A decision tree asks one question: "is the size above t?" Which t splits them best?
best_t, best_gini = None, 1.0
for t in [2.5, 3.5, 5, 6.5]:
left = [k for s, k in zip(sizes, kinds) if s <= t]
right = [k for s, k in zip(sizes, kinds) if s > t]
p, q = sum(left) / len(left), sum(right) / len(right) # share of apples on each side
gini = (len(left) * (1 - p * p - (1 - p) ** 2) + len(right) * (1 - q * q - (1 - q) ** 2)) / len(sizes)
if gini < best_gini:
best_t, best_gini = t, gini
print("best question: size >", best_t, "with gini", round(best_gini, 2))
Step by step
- Six fruits, measured by size.
- Their kinds: 1 for apple, 0 for lemon. Small fruits are lemons and big ones apples, except one: the fruit of size 7 is a lemon. On the chart, the y position shows the kind too.
- We will remember the best split found so far. Gini impurity is at most 0.5 for two kinds, so starting at 1.0 means the first split tried will beat it.
- The first question to try: "is the size above 2.5?" The dashed line on the chart is this split.
- Left of the line (size ≤ 2.5): just one fruit, a lemon.
- Right of the line: the other five, a mix of 3 apples and 2 lemons.
- The share of apples on each side: 0 on the left, 3 out of 5 = 0.6 on the right.
- Gini impurity measures how mixed a group is: 1 − (share of apples)² − (share of lemons)². The left is pure (0); the right is 1 − 0.6² − 0.4² = 0.48. Weighted by size: (1 × 0 + 5 × 0.48) / 6 = 0.4.
- 0.4 is better than 1.0, the starting value.
- So size > 2.5 becomes the best question so far: the solid green line.
- The next question: "is the size above 3.5?"
- Left: two lemons.
- Right: four fruits, 3 apples and the one odd lemon.
- Apple shares: 0 on the left, 0.75 on the right.
- Left is pure; right is 1 − 0.75² − 0.25² = 0.375. Weighted: (2 × 0 + 4 × 0.375) / 6 = 0.25.
- 0.25 beats 0.4.
- Size > 3.5 is the new best question.
- Try "is the size above 5?"
- Left: 2 lemons and an apple.
- Right: 2 apples and a lemon.
- One third apples on the left, two thirds on the right.
- Both sides are mixed (0.444 each), so the weighted Gini is 0.444.
- 0.444 is worse than 0.25: the best stays at 3.5.
- The last question: "is the size above 6.5?"
- Left: 2 lemons and 2 apples.
- Right: one apple and the odd lemon.
- Half and half on both sides.
- The most mixed split possible: Gini 0.5.
- Worse again. The Gini chart shows the dip at the second question.
- All four questions have been tried.
- The tree's first question is "size > 3.5?". It is not perfect (the lemon of size 7 lands with the apples), so a real tree would ask another question on that side, and so on, until the groups are pure enough.