yesnoyesno1def predict(hours, sleep):2 if hours <= 2.5:3 return "fail"4 if sleep <= 5.5:5 return "fail"6 return "pass"WEIGHTED GINI OF EACH QUESTIONh≤2.5h≤5.5s≤5.5s≤7.58 STUDENTS8 · Gini 0.47hours ≤ 2.5?3 · Gini 0FAIL5 · Gini 0.48sleep ≤ 5.5?FAILPASSPASS · 3 of 4?
Teal blocks: questions · salmon / green blocks: leaves that answer fail / passTokens: students · green ✓ passed, salmon ✗ failed, blue ? not labelled yetShelf: weighted Gini of each question tried (lower = purer split)Eight students (green tokens passed, salmon failed) gather on the root. For each question tried, they slide into a yes pile and a no pile, and the shelf at the back records that question's weighted Gini impurity; the lowest wins. The winning question, hours ≤ 2.5, sends three students to a pure fail leaf and five to a second question, sleep ≤ 5.5, which splits them into two pure leaves. The card at the back is the same tree written as Python. A new student walks the two questions to the pass leaf; an odd student lands in that leaf without changing its answer when the depth is capped. The eight students are made up, and far too few for a real model; they keep every number small enough to check by hand.

Measuring the mix

  1. Our training data: eight students. For each we know two features, hours studied and hours slept, and a label: did they pass or fail the quiz? A decision tree learns to predict the label by asking yes/no questions.

Growing the tree

Using the tree

What happens

;; step 1/9 · data
hours  sleep  label
  1      8     fail
  2      6     fail
  2      9     fail
  3      7     pass
  4      4     fail
  4      8     pass
  5      5     fail
  6      6     pass
5 fail, 3 pass

The eight students are made up, and far too few for a real model; they keep every number small enough to check by hand. This is the CART recipe (Breiman et al., 1984): binary questions scored by Gini impurity. ID3 and C4.5 (Quinlan) use entropy and information gain instead; both usually pick similar questions.