Decision tree visualizer

A decision tree classifies a point by asking a series of yes-or-no questions about its features, each of the form "is x below some value?". Grow one on labelled data, see the regions each question carves out, and watch what happens to accuracy on new data as the tree gets deeper.

Training accuracy
89%
Accuracy on new data
89%
Leaves
8
Data
Maximum depth
Smallest leaf
Impurity
Click to add a point of class

class A class B shaded region: what the tree predicts there, darker when the leaf is purer

The tree
  1. x ≤ 5.76 80 pts, Gini 0.497, gain 0.135
  2. y ≤ 3.33 44 pts, Gini 0.351, gain 0.144
  3. x ≤ 2.31 15 pts, Gini 0.480, gain 0.173
  4. predict B 2 A, 5 B
  5. predict A 7 A, 1 B
  6. y ≤ 8.54 29 pts, Gini 0.067, gain 0.007
  7. predict B 0 A, 22 B
  8. predict B 1 A, 6 B
  9. y ≤ 7.24 36 pts, Gini 0.375, gain 0.077
  10. x ≤ 9.75 25 pts, Gini 0.211, gain 0.025
  11. predict A 21 A, 2 B
  12. predict A 1 A, 1 B
  13. x ≤ 9.46 11 pts, Gini 0.496, gain 0.223
  14. predict B 2 A, 6 B
  15. predict A 3 A, 0 B

Each split sends points with the value at or below the threshold to the first branch listed under it, and the rest to the second.

Accuracy by depth
DepthTrainingNew dataLeaves
176%70%2
281%86%4
389%89%8
495%81%14
6100%79%21
8100%79%21
12100%79%21

How to use the simulator

Each dataset has 80 training points of two classes, A and B, with about one label in twelve flipped at random as noise. The tree is grown on those points, and the shaded regions show which class it would predict anywhere in the square. A separate set of 200 new points from the same source measures how well the tree generalizes. Click the plot to add training points of the chosen class.

  • On Diagonal, raise the maximum depth step by step. Training accuracy climbs to 100%, while accuracy on new data peaks and then falls: the deep tree is drawing boxes around noise.
  • Set the smallest leaf to 5 or 10 at full depth. The tree can no longer isolate single points, and accuracy on new data recovers.
  • Try Checkerboard at depth 1. No single cut helps, because each half is still half A and half B; the tree needs two levels before it can see the pattern.
  • Add a single point of the wrong class in the middle of a region and watch the deep tree carve out a box for it.

The tree-growing code is checked against scikit-learn's DecisionTreeClassifier: on every dataset, criterion, leaf size and depth tested, it picks the same root split and reaches the same training accuracy.

What is a decision tree?

A decision tree is a model that makes a prediction by following a path of questions. Each internal node tests one feature against a threshold, such as "is income at most 40,000?", and sends the item left or right; each leaf holds a prediction, here the majority class of the training points that reached it. Reading the path from root to leaf gives a plain explanation of any single prediction.

Trees are learned from labelled examples. They handle numeric and categorical features without scaling, capture interactions between features naturally, and are fast to evaluate. On their own they are unstable and prone to overfitting, which is why the strongest tree-based methods combine many of them.

How a tree is grown

Finding the best possible tree is computationally hard, so tree learners grow one greedily, top down. The CART method, used here and in scikit-learn, works like this at each node:

  1. For each feature, sort the node's points by that feature and consider a threshold halfway between each pair of neighbouring distinct values.
  2. For each candidate, split the points into "at or below" and "above", and compute the impurity of each side, weighted by its size.
  3. Keep the split with the lowest weighted impurity; the drop from the node's own impurity is the split's gain.
  4. Repeat on each side, until a stopping rule applies: the maximum depth is reached, the node is pure, or a split would leave a side with fewer points than the minimum leaf size.

Because each choice is made locally, the tree can miss splits that only pay off one level later, which is exactly what the checkerboard data shows at depth 1.

Gini impurity and entropy

An impurity measure scores how mixed a node is: zero when every point has the same class, highest when the classes are evenly mixed. With class proportions pi:

Gini = 1 − Σ pi² Entropy = − Σ pi log₂ pi

Gini is the probability that two points drawn at random, with replacement, have different classes; entropy is the average number of bits needed to say which class a point has, and the reduction in entropy from a split is called information gain. For a node with 30 points of one class and 10 of the other, Gini is 0.375 and entropy 0.811 bits. The two usually lead to the same or very similar trees; switch between them in the simulator to compare.

Depth and overfitting

A tree with no limits keeps splitting until every leaf is pure, which on noisy data means building small regions around individual mislabelled points. Training accuracy reaches 100%, but those regions are wrong for new data. The table shows accuracy on 200 new points for the page's four datasets at different depths, averaged over 20 random samples of each:

DataDepth 1Depth 2Depth 3Depth 4Depth 6Depth 8Depth 12
Two clusters80%80%79%78%76%75%75%
Diagonal69%78%79%80%79%80%79%
Checkerboard51%64%76%79%80%81%81%
Circle63%68%76%83%80%80%80%

The effect is largest on the two-cluster data: accuracy on new points is best at depth 2 (80%) and falls to 75% at depth 12, where training accuracy is 100% with 17 leaves on average. With about one label in twelve flipped, no tree can beat roughly 92% on new data; the deep tree loses ground by fitting the flipped labels. The usual remedies are limiting depth, requiring a minimum number of points per leaf, pruning a fully grown tree back (scikit-learn implements minimal cost-complexity pruning through its ccp_alpha parameter), and, most effective, averaging many trees.

Axis-aligned boundaries

Each split tests a single feature, so every boundary a tree draws is parallel to an axis, and its regions are boxes. Data whose true boundary is along an axis is easy: the two-cluster data reaches 80% on new points at depth 2, on average. A diagonal boundary has to be approximated by a staircase of small boxes, which needs many leaves and many points; a circle needs even more. Methods with oblique splits exist, but most libraries keep axis-aligned splits because they are fast to search and easy to read, and rely on ensembles to smooth the staircase.

Random forests and boosting

Single trees change a lot when the training data changes slightly. Ensembles turn that weakness into a strength:

  • Random forests (Leo Breiman, 2001) train many deep trees, each on a bootstrap resample of the data and considering only a random subset of features at each split, then take a majority vote. Averaging trees whose errors differ cancels much of their variance.
  • Gradient boosting (Jerome Friedman, 2001) adds small trees one at a time, each fitted to the errors of the ensemble so far. Implementations such as XGBoost (2016), LightGBM (2017) and CatBoost are among the most widely used methods for tabular data.

History

Leo Breiman, Jerome Friedman, Richard Olshen and Charles Stone published Classification and Regression Trees in 1984, introducing CART with Gini impurity, binary splits and cost-complexity pruning. Independently, J. Ross Quinlan developed ID3, published in 1986 in "Induction of decision trees", which used information gain, and its successor C4.5 in 1993. Breiman's random forests followed in 2001, the same year as Friedman's gradient boosting machine.

Decision tree questions and answers

How does a decision tree choose a split?

At each node it tries every feature and every threshold between consecutive values, measures how mixed the two resulting groups are with an impurity score such as Gini or entropy, and keeps the split with the lowest weighted impurity. It then repeats the process on each side until a stopping rule, such as a maximum depth, is reached. This greedy method is called CART.

What is Gini impurity?

Gini impurity is the chance that two items drawn at random from a node, with replacement, belong to different classes: 1 minus the sum of the squared class proportions. It is 0 for a pure node and 0.5 for an even two-class mix. A node with 30 items of one class and 10 of the other has Gini 0.375.

Gini or entropy: which is better?

They usually pick the same or very similar splits. Entropy, from information theory, measures the bits needed to encode a class (0.811 bits for a 30 to 10 node) and is slightly slower to compute because of the logarithm. Gini is the default in scikit-learn and in the original CART method.

Why do decision trees overfit?

A tree that keeps splitting can always separate the training points perfectly, including noise and mislabelled points, by drawing small boxes around them. Accuracy on the training data then reaches 100% while accuracy on new data falls. On this page’s two-cluster data, averaged over 20 samples, a full-depth tree scores 100% on training data and 75% on new data, against 80% at depth 2.

What is the difference between a decision tree and a random forest?

A random forest trains many trees, each on a random resample of the data and with a random subset of features considered at each split, and averages their votes. The randomness makes the trees’ errors less correlated, so the average generalizes much better than any single tree, at the cost of losing the single readable tree.

Do decision trees need feature scaling?

No. Splits compare one feature against a threshold, so multiplying a feature by a constant or shifting it moves the thresholds but does not change which points go where. That, along with handling mixed feature types and being easy to read, is one reason trees are popular for tabular data.

Further reading

Published Updated