Decision tree
A decision tree grown live in your browser, one split per step: Gini, entropy or MSE, every candidate threshold scored, tree diagram, 3D cone tree and pruning. Matches scikit-learn node for node.
Updated
- Impurity: Gini, entropy, MSE
- How the best split is chosen
- Depth and leaf-size limits
- Overfitting on a test set
- Cost-complexity pruning
Intuition
Carving rectangles one cut at a time
To separate three classes of points, a decision tree finds a horizontal or vertical line that splits the points into two piles, each as "pure" (mostly one class) as possible. Then it finds a cut for each pile, and so on, until the piles are pure enough or the tree is deep enough. Every cut looks at a single feature, so every region is a rectangle.
The three small labs below really grow trees, one cut at a time. Each rectangle is a leaf coloured by its majority class (darker = purer); the black line is the latest cut.
First picture: the first cut is the horizontal line , which slices off almost the whole green cluster at the top; later cuts separate the bottom-left and bottom-right clusters, then tidy the corners. The tree is fully grown after 18 steps with 18 leaves, 99% training accuracy and 91% test accuracy.
The second and third use the same XOR data (bottom-left and top-right are one class, top-left and bottom-right the other). With depth 2 the test accuracy is only 49%, no better than guessing; with depth 6 it reaches 95%. The culprit is greediness: whichever single cut you make on XOR data, both sides are still half and half, so the first cut looks worthless and the tree has to make a few arbitrary cuts before later ones start to help.
Math
Impurity
A node has samples with class proportions . Two common impurities:
Both are 0 for a pure node (one class) and largest when the classes are equally represented. Three equal classes give a Gini of .
The best cut
For every feature of node and every candidate threshold (the midpoint between two consecutive distinct values), split the samples into left and right and compute the impurity decrease:
is the total number of training samples (the factor makes decreases at different nodes comparable, as in scikit-learn). Take the with the largest .
The root of this page's default data: 200 points, about a third per class (67, 67, 66), Gini ≈ 0.667. The best cut is :
| Samples | Class counts | Gini | |
|---|---|---|---|
| Left (≤) | 127 | 62, 62, 3 | 0.523 |
| Right (>) | 73 | 5, 5, 63 | 0.246 |
Weighted impurity , decrease .
When to stop
A node becomes a leaf if any of these holds: it is pure; it reached the maximum depth; it has fewer samples than the split minimum; or every split would leave one side with fewer than the leaf minimum. A leaf predicts its majority class (its mean, for regression).
Cost-complexity pruning
A fully grown tree is often too fine-grained. Define the penalised cost , where is the weighted impurity summed over leaves and the number of leaves. For each internal node , measure whether removing its subtree is worth it:
is the cost of collapsing into a leaf and the cost of its subtree. Repeatedly prune the node with the smallest (the weakest link) until the smallest exceeds .
With a maximum depth of 14 these data grow 19 leaves with 91% test accuracy; α = 0.01 prunes to 9 leaves at the same 91%; α = 0.02 prunes to 4 leaves and test accuracy falls to 83%.
Depth and overfitting
| Max depth | Training accuracy | Test accuracy |
|---|---|---|
| 1 | 62.5% | 63% |
| 2 | 93% | 83% |
| 4 | 96.5% | 84% |
| 6 (default) | 99% | 91% |
| 10 | 100% | 91% |
At depth 10 the tree classifies every training point correctly (19 leaves), yet test accuracy does not improve: the extra leaves are memorising noise.
How it is computed, with real numbers
Below is the computation behind the lab's latest split. Step the lab once more and this switches to the new cut.
Once the lab above has loaded, this panel works through the same formulas with its current numbers.
Experiment
Follow the lesson
Course
Lesson 1: greedy cuts and overfitting
5 steps · each one: set up and run → look → answer
1. The first cut
Take one step. Look at the black line in "Feature space" and the split-search chart on the right.
2. Fully grown
Continue until the tree is fully grown. Compare training and test accuracy.
3. XOR fools the greedy search
Switch to XOR with max depth 2.
4. Go deeper
Set the max depth to 6.
5. Pruning
Back to the three clusters with max depth 14 and pruning strength α = 0.01. Watch it grow fully, then prune.
Try these settings
Exercises
Exercises
Exercise 1: crack XOR with as few leaves as possible
At least 95% test accuracy with no more than 11 leaves.
Hint
Max depth 6 is exactly right: 11 leaves, 95%. Depth 5 gives only 84%; depth 7 adds a leaf.
- Test accuracy ≥ 0.950
- Leaves ≤ 11
Exercise 2: slim down without losing accuracy
Max depth is fixed at 14 (fully grown: 19 leaves, 91% test). Bring the leaves down to 10 or fewer without losing test accuracy.
Hint
Two ways: pruning strength α = 0.01 (9 leaves), or a leaf minimum of 5 (10 leaves). A leaf minimum of 10 goes too far: 82% test.
- Leaves ≤ 10
- Test accuracy ≥ 0.910
Exercise 3: circles without overfitting
The defaults give 100% training but only 88% test accuracy (9 leaves). Reach 91% test accuracy with at most 7 leaves.
Hint
A leaf minimum of 5: 7 leaves, 91%. Or max depth 14 with α = 0.02: 5 leaves, 91%.
- Test accuracy ≥ 0.910
- Leaves ≤ 7
Quiz
Further reading
Frequently asked questions
How does a decision tree choose its splits?
For every candidate threshold of every feature it computes how much the weighted impurity (Gini or entropy) of the two children falls below the parent's, and takes the largest decrease. It is greedy: it considers only this cut, not the ones after it.
What is the difference between Gini and entropy?
Both measure how mixed a node is and are 0 when it is pure. Gini needs no logarithm and is slightly faster; entropy is more sensitive to small probabilities. In practice they pick the same split almost every time — on this page's three clusters, entropy also gives 18 leaves and 91% test accuracy.
Why do decision trees overfit, and what can I do?
Unconstrained, a tree keeps splitting until every leaf holds one point: 100% training accuracy, but the little rectangles trace noise. Limit the maximum depth or the minimum leaf size, or grow fully and then apply cost-complexity pruning (scikit-learn's ccp_alpha); more thoroughly, average many trees (random forests).
Do decision trees need feature scaling?
No. A split only compares values of one feature, so any monotonic transformation (scaling, taking logs) leaves the tree unchanged.
Can decision trees do regression?
Yes. A leaf outputs the mean of its region and the impurity becomes the variance (mean squared error). This page's "Wave" data use a regression tree.
Further reading
- Breiman, Friedman, Olshen, Stone, Classification and Regression Trees, 1984: the original account of CART and cost-complexity pruning.
- Quinlan, Induction of Decision Trees, 1986: ID3 and information gain.
- Hastie, Tibshirani, Friedman, The Elements of Statistical Learning, section 9.2: tree-based methods.
- scikit-learn, Decision Trees user guide and cost-complexity pruning example: this page's reference implementation.