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.

Machine Learning · beginner · 12 min · 31 steps · free

What you'll learn

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

  1. Six fruits, measured by size.
  2. 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.
  3. 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.
  4. The first question to try: "is the size above 2.5?" The dashed line on the chart is this split.
  5. Left of the line (size ≤ 2.5): just one fruit, a lemon.
  6. Right of the line: the other five, a mix of 3 apples and 2 lemons.
  7. The share of apples on each side: 0 on the left, 3 out of 5 = 0.6 on the right.
  8. 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.
  9. 0.4 is better than 1.0, the starting value.
  10. So size > 2.5 becomes the best question so far: the solid green line.
  11. The next question: "is the size above 3.5?"
  12. Left: two lemons.
  13. Right: four fruits, 3 apples and the one odd lemon.
  14. Apple shares: 0 on the left, 0.75 on the right.
  15. Left is pure; right is 1 − 0.75² − 0.25² = 0.375. Weighted: (2 × 0 + 4 × 0.375) / 6 = 0.25.
  16. 0.25 beats 0.4.
  17. Size > 3.5 is the new best question.
  18. Try "is the size above 5?"
  19. Left: 2 lemons and an apple.
  20. Right: 2 apples and a lemon.
  21. One third apples on the left, two thirds on the right.
  22. Both sides are mixed (0.444 each), so the weighted Gini is 0.444.
  23. 0.444 is worse than 0.25: the best stays at 3.5.
  24. The last question: "is the size above 6.5?"
  25. Left: 2 lemons and 2 apples.
  26. Right: one apple and the odd lemon.
  27. Half and half on both sides.
  28. The most mixed split possible: Gini 0.5.
  29. Worse again. The Gini chart shows the dip at the second question.
  30. All four questions have been tried.
  31. 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.

Common mistakes

Best practices

Key takeaways

Check yourself

A group has 2 apples and 2 lemons. What is its Gini impurity?

Answer: 0.5. 1 − 0.5² − 0.5² = 1 − 0.25 − 0.25 = 0.5.

Split A has weighted Gini 0.3, split B 0.1. Which does the tree choose?

Answer: B. Lower impurity means purer groups: the tree picks B.

·

More in Classifying