Decision trees explained
Should a bank approve a loan, is an email spam, which species is this flower? A decision tree answers by asking a short series of simple questions, each about one property, and following the answers down to a verdict. Unlike many machine-learning models, you can read a tree like a flowchart and see exactly why it decided what it did. Learning a good tree from examples turns out to be a neat mix of information theory and greed.
A flowchart learned from examples
A decision tree is a set of nested questions. The top node might ask whether a petal is shorter than 2.5 cm; if so, the flower is a setosa, and if not, the next node asks about its width. Each inner node tests one feature against a threshold, each branch is an answer, and each leaf holds a verdict: a class for a classification tree, a number for a regression tree. What makes it machine learning is that nobody writes the questions: the algorithm finds them from a table of labelled examples.
Measuring how mixed a group is
A good question splits the examples into groups that are each as pure as possible, ideally one class per group. Two measures of impurity are common. The Gini impurity is the probability that two examples drawn at random from the group belong to different classes: 0 for a pure group, 0.5 for an even mix of two classes. The entropy, from information theory, is the average number of bits needed to tell which class an example has. The drop in entropy achieved by a split is called its information gain.
Growing the tree
The algorithm starts with all examples at the root. It tries every feature and every threshold between neighbouring values, and keeps the split with the largest drop in impurity. Then it repeats the procedure separately in each of the two groups, and so on. It stops when a group is pure, too small to split, or at a set maximum depth, and the leaf then votes for its most common class. This top-down, greedy recipe underlies CART, introduced by Leo Breiman, Jerome Friedman, Richard Olshen and Charles Stone in 1984, and Ross Quinlan's ID3 and C4.5.
Boxes and staircases
Because each question involves a single feature, every split is a cut parallel to one axis, and the tree divides the space into rectangular boxes. Boundaries that run along the axes are learned quickly, but a diagonal boundary needs a staircase of many small boxes, and a circle needs a ring of them. Greed adds another weakness: on a pattern like XOR, where no single cut helps on its own, the algorithm cannot see that two cuts together would solve it, and can waste its first levels on small, accidental gains.
Overfitting and pruning
A tree allowed to grow without limit will keep splitting until every training example sits in a pure leaf. It then makes no mistakes on the training data, but many of its deepest splits describe noise: chance quirks of the sample rather than the real pattern. On new data it does worse than a smaller tree. The remedy is to hold back test data, limit the depth or the size of the leaves, or grow a large tree and prune it back, removing branches that do not improve accuracy on data the tree has not seen.
Forests and boosting
Single trees are unstable: a few changed examples can change the whole tree. Leo Breiman's random forests of 2001 turn that into a strength. They grow hundreds of trees, each on a random resample of the data and with a random subset of the features to choose from at every node, and let them vote; the errors of the individual trees largely cancel out. Gradient boosting instead adds small trees one at a time, each correcting the mistakes of those before. Libraries such as XGBoost and LightGBM win many competitions on tabular data with boosted trees.
Where trees are used
Decision trees and their ensembles are used for credit scoring, fraud detection, medical triage, customer churn prediction and quality control in factories. A single small tree is often preferred where decisions must be explained, for example to a patient or a regulator, because its reasoning can be followed step by step. Trees also handle mixed data, numbers and categories side by side, without the scaling and preparation that many other methods need.
Explore it
Open the decision tree builder to grow trees on clusters, stripes, a diagonal, a circle, XOR or a spiral, or on points you click in yourself. Change the maximum depth and the smallest leaf, compare Gini impurity with information gain, click any box or node to see its rule, and follow on the chart how training and test accuracy part ways as the tree grows deeper.
Frequently asked questions
Do decision trees need the data to be scaled?
No. A split only compares a feature with a threshold, so stretching or shifting a feature changes the threshold but not the tree. That makes trees easy to use on raw data, unlike methods such as neural networks or k-nearest neighbours.
Can a decision tree predict numbers?
Yes. A regression tree works the same way but chooses splits that reduce the variance of the target values, and each leaf predicts the average of its examples. The prediction is then a step function, flat within each box.
How deep should a tree be?
Deep enough to capture the pattern, shallow enough not to learn the noise. The usual way to find out is to try several depths and keep the one that does best on data held back from training, which is what the chart in the builder shows.
Is finding the best possible tree easy?
No. Finding the smallest tree that classifies a set of examples correctly is NP-hard, which is why practical algorithms grow trees greedily, one best split at a time, accepting that the result may not be the best possible tree.
Ready to try it?
Open PictorX in your browser — no install, no credit card required to start.
Open the decision tree builder