Visual explainer
Decision Trees
Follow ten labelled samples through feature thresholds, measured impurity reduction and recursive regions, then trace a query and inspect the limits of rectangular leaves.
A classification tree predicts a class by following feature comparisons to a leaf. The ten training examples have numeric features x and y in arbitrary units; circles mean class A and triangles mean class B. The diamond in the key identifies an unknown query: it is not a plotted coordinate or an eleventh training sample.
The fixed fixture is A: (10,10), (12,20), (18,30), (20,40), (45,90); B: (15,90), (40,10), (42,20), (48,30), (50,40). The labels are targets to predict, not additional input coordinates.
One Question, One Cut
Equality goes left. The left child contains 4 A and 1 B; the right contains 1 A and 4 B. The dashed line tests only x, so it is vertical regardless of y. Both groups remain mixed.
Measure the Remaining Mix
Bar lengths share one scale. Gini impurity is one minus the sum of squared class proportions: the root gives 1 − 0.5² − 0.5² = 0.50. Each child gives 1 − 0.8² − 0.2² = 0.32. Their weighted impurity is (5/10) × 0.32 + (5/10) × 0.32 = 0.32; the bracket spans the 0.18 reduction. Impurity measures training-label mixing, not held-out error.
Recurse Inside Each Region
Both child splits at y ≤ 50 are shown. Each operates inside its own x region; the mint segment belongs to the left child. The lower-left region predicts A from four circles; upper-left predicts B from one triangle. Lower-right predicts B from four triangles; upper-right predicts A from one circle. Every region is an intersection of path conditions.
These thresholds are illustrative, not an alleged library fit. The horizontal threshold lies between observed y values 40 and 90. Pure training regions alone say nothing about unseen correctness.
Read the Same Partition as Branches
Read down from the root; yes means the comparison holds. The leaves in branch order correspond to lower-left A, upper-left B, lower-right B and upper-right A in the previous field. Their counts are 4, 1, 4 and 1. Three internal nodes exist, but a sample follows only two comparisons. The mint A leaf represents x > 30 and y > 50.
Route an Unseen Query
For (46,70), 46 ≤ 30 is false and 70 ≤ 50 is false. The emphasized route reaches A. Dashed paths are unused; the left subtree is collapsed after its decision node for focus. The diamond is a query key, not another tree node. Its true class remains unknown: A is a prediction.
Compare Candidate Splits
The upper candidate x ≤ 13.5 produces a pure two-A child and an eight-sample child with 3 A and 5 B. Its weighted impurity is (2/10) × 0 + (8/10) × [1 − (3/8)² − (5/8)²] = 0.375. The lower candidate leaves 0.32, so its gain 0.18 exceeds 0.50 − 0.375 = 0.125. Count chips only identify the groups; the bars underneath encode weighted impurity on the same scale.
Greedy split search chooses the lowest weighted impurity among eligible candidates. This scene compares two candidates; it does not claim that x ≤ 30 wins an exhaustive fit on these data.
A Mixed Leaf Still Predicts
Now change the model: stop after the x split. The same query (46,70) enters the right leaf, containing 1 A and 4 B. Its class proportions are P(A) = 1/5 = 0.2 and P(B) = 4/5 = 0.8, represented by bar lengths in a 1:4 ratio. It predicts B. Those leaf frequencies are not a guarantee of calibrated confidence or correctness.
Training Fit Does Not Select Depth
The panels use identical scales and the same ten samples, with a smaller field than the earlier drawings. At depth one, each child predicts its majority class. Rings mark the B sample on the left and A sample on the right that it misses: 2/10 training errors. Depth two adds the horizontal comparisons and fits all ten labels, for 0/10. The query's truth is still unknown.
Select depth, minimum leaf size or pruning using held-out evidence. Fewer training mistakes do not establish better generalization; more branches cost complexity and can track noise. The tree formulation and stopping conditions define the rules behind these scenes.
Where It Breaks
This is a new idealized fixture, with equal x/y units over 0 to 100. The true rule is A above y = x and B below it. The finite tree partitions x into four width-25 bands and uses y thresholds 12.5, 37.5, 62.5 and 87.5. Its solid boundary is a staircase, while the target is dashed.
The cross at (20,18.75) lies above the first tree threshold 12.5, so the tree predicts A. It is below the true diagonal because 18.75 < 20, so its true class is B. The small shaded wedge exposes this disagreement. Additional splits can refine the approximation, with added complexity; a finite set of axis-aligned leaves cannot exactly reproduce the continuous oblique boundary.
The Quick Version
- Training examples pair feature coordinates with known target classes.
- One threshold partitions samples by one feature.
- Weighted Gini measures how mixed the child labels remain.
- Recursive comparisons create rectangular prediction regions.
- Each leaf is an intersection of root-to-leaf conditions.
- An unknown query follows one path to a prediction.
- Greedy search compares eligible candidate impurities.
- A mixed leaf predicts from its class proportions.
- Training fit alone does not determine useful depth.
- Finite rectangular leaves approximate diagonal boundaries with steps.