C · Classic ML
Decision Trees
A flowchart of yes/no questions about single features, each chosen to make the groups below it as pure as possible.
Try it: C3 · Decision Trees
Intuition
A decision tree predicts the way people often decide: by asking a sequence of yes/no questions. "Is the income above 40,000? If so, is the loan longer than 10 years?" Each answer leads to the next question, until a final answer is reached. The model is a flowchart, which makes it one of the few machine learning models a person can read in full.
The tree is learned top-down. Among all questions of the form "is feature \(j\) below threshold \(t\)?", pick the one that best separates the classes. That splits the data in two. Repeat inside each half, then inside each quarter, until the groups are pure or a stopping rule says enough. Every question cuts the space with a straight line parallel to an axis, so a tree carves it into boxes, each with its own answer.
The math
Impurity
A node is pure when all its points share one class. With class proportions \(p_1, \dots, p_K\) in a node, two common measures of impurity are:
\[ \text{Gini} = 1 - \sum_{k} p_k^2, \qquad \text{Entropy} = -\sum_k p_k \log_2 p_k \]Both are 0 for a pure node and largest for an even mix (Gini 0.5, entropy 1 bit, for two classes). In practice they choose nearly the same splits.
Choosing a split
A split sends \(n_L\) of a node's \(n\) points left and \(n_R\) right. Its quality is the drop in impurity, the weighted average of the children's impurity subtracted from the parent's:
\[ \Delta = I(\text{parent}) - \frac{n_L}{n} I(\text{left}) - \frac{n_R}{n} I(\text{right}) \]With entropy, \(\Delta\) is the information gain. The search is exhaustive but cheap: for each feature, sort the values and try a threshold between each pair of neighbours. Then pick the best feature and threshold overall.
Prediction
A leaf predicts its majority class, and the class proportions in it serve as probabilities. For regression the leaf predicts the mean of its points, and splits minimize the squared error instead of impurity.
Worked example. A node with 5 blue and 5 orange points: Gini \(= 1 - 0.5^2 - 0.5^2 = 0.5\).
Split A gives \(\{4\text{ blue}, 1\text{ orange}\}\) and \(\{1, 4\}\). Each child has Gini \(1 - 0.8^2 - 0.2^2 = 0.32\); weighted average 0.32, drop 0.18.
Split B gives \(\{5, 2\}\) and \(\{0, 3\}\). Ginis 0.408 and 0; weighted \(0.7 \cdot 0.408 = 0.286\), drop 0.214. B wins: it makes one group completely pure, even though the other stays mixed.
Controlling size
Grown until every leaf is pure, a tree memorizes the training set, noise included: the classic overfitting of Regularization. Limits on growth keep it general:
- Maximum depth, or a minimum number of points per leaf or per split.
- Minimum impurity decrease: refuse splits that barely help.
- Pruning: grow the full tree, then remove the branches that do not improve validation error (cost-complexity pruning adds a penalty per leaf, like a weight penalty).
Ensembles
A single tree is unstable: change a few training points and the top split, with everything below it, may change. Combining many trees turns that weakness into strength.
- Random forests train each tree on a bootstrap sample of the data and let each split consider only a random subset of features. The trees make different mistakes, and averaging their votes cancels much of them.
- Gradient boosting (XGBoost, LightGBM, CatBoost) adds small trees one after another, each fitted to the errors the ensemble still makes; it is gradient descent in the space of functions. On tables of mixed numeric and categorical data it often beats neural networks.
Strengths and pitfalls
- No scaling needed. A threshold does not care about units, so trees need no standardization and handle features of very different scales.
- Interactions for free. A split inside a branch only applies to that branch, so trees capture "if A then B matters" without designed features.
- Staircase boundaries. A diagonal boundary needs many small axis-parallel steps. Linear models handle it with one weight.
- No extrapolation. A regression tree predicts the mean of a leaf, so beyond the training range the prediction is flat.
- Greedy. Each split is the best one now, not the best for the tree as a whole; finding the optimal tree is computationally intractable.
History
Leo Breiman, Jerome Friedman, Richard Olshen and Charles Stone published CART (Classification and Regression Trees) in 1984, with Gini impurity and pruning; Ross Quinlan developed ID3 (1986) and C4.5 (1993) with information gain. Breiman introduced random forests in 2001, and Friedman gradient boosting the same year.
Connections
Further reading
- Decision Trees (MLU-Explain)An interactive tree that grows split by split, with the impurity behind each choice.
- Decision and Classification Trees, Clearly Explained (StatQuest)Gini impurity and the weighted split score, worked through by hand.
- Decision trees (Google Machine Learning: Decision Forests)How trees are grown, and how limits like a maximum depth keep them from overfitting.