Decision Trees
A decision tree is a supervised machine learning model that learns to make predictions by asking a sequence of yes/no questions about input features, forming a branching tree structure where each leaf node represents a final prediction. Decision trees matter because they are one of the few ML models that are simultaneously powerful enough to solve real problems and transparent enough for a human to read and understand every decision. Remarkably, the same algorithm that classifies whether a patient has a disease can be printed on a single sheet of paper and checked by a doctor — something almost no modern deep learning model can claim.
A decision tree learns to mimic the way humans naturally reason: by asking a series of targeted questions, each narrowing down the possibilities, until only one answer remains. The "aha moment" is realizing that the algorithm's entire job is to figure out which question to ask first — because the best first question is the one that splits your messy, mixed-up data into the cleanest, most separated groups possible. Once you understand that idea (called impurity reduction), everything else in the algorithm follows naturally. Every split, every branch, every stopping rule is just a systematic way of chasing that cleaner separation.
How It Works
At its core, a decision tree repeatedly splits the training data into two groups by testing one feature at a time. To decide which feature to split on and where to split it, the algorithm measures how "impure" or "mixed" each resulting group would be. The most common impurity measure for classification is Gini Impurity.
Explanation: Gini Impurity measures how often a randomly chosen element from set S would be incorrectly labelled — zero means perfectly pure (all one class), and higher values mean more mixing.
Symbols:
- — the Gini impurity of a set
- — the total number of classes
- — the proportion of examples in that belong to class
At every candidate split, the algorithm computes the weighted average impurity of the two child nodes that would result:
Explanation: This weights each child's impurity by how large it is — a very impure but tiny child group is less concerning than a very impure and large one.
Symbols:
- , — the left and right subsets produced by the split
- — number of examples in parent set; , — sizes of each child
The algorithm picks whichever feature and threshold minimises equation [2]. This process repeats recursively at each node until a stopping condition is met — for example, a maximum depth, a minimum number of samples per leaf, or until all leaves are pure. Here is the simplest possible working example in Python: ```python from sklearn.tree import DecisionTreeClassifier, export_text from sklearn.datasets import load_iris
# Load a classic dataset X, y = load_iris(return_X_y=True)
# Train a shallow tree (max_depth prevents overfitting) clf = DecisionTreeClassifier(max_depth=3, random_state=42) clf.fit(X, y)
# Print the human-readable rules print(export_text(clf, feature_names=load_iris().feature_names)) ``` Running this prints something like: ``` |--- petal width (cm) <= 0.80 | |--- class: 0 |--- petal width (cm) > 0.80 | |--- petal width (cm) <= 1.75 | | |--- class: 1 | |--- petal width (cm) > 1.75 | | |--- class: 2 ``` Every line is a human-readable rule. You can hand this to a botanist and they can classify flowers with a ruler — no computer required. For regression problems (predicting a number rather than a class), the impurity measure switches from Gini to Mean Squared Error within each node:
Explanation: A regression leaf is "pure" when all the target values in it are close to their mean — meaning the tree's constant prediction of at that leaf would have low error.
Symbols:
- — the true target value for example
- — the mean target value of all examples in set
```python from sklearn.tree import DecisionTreeRegressor import numpy as np
# Toy regression example X = np.array([[1],[2],[3],[4],[5],[6],[7],[8]]) y = np.array([1.2, 1.9, 3.1, 3.8, 5.2, 5.9, 7.0, 8.1]) reg = DecisionTreeRegressor(max_depth=2) reg.fit(X, y) print(reg.predict(4.5)) # Predicts a number, not a class ```
Leo Breiman was a statistician at UC Berkeley who became one of the most influential figures in applied machine learning, fundamentally shaping how decision trees are built and used. In 1984, he co-authored the landmark book Classification and Regression Trees (CART) with Friedman, Olshen, and Stone, which introduced the systematic binary splitting and pruning procedures that underpin nearly every modern tree implementation including scikit-learn. Remarkably, Breiman spent years as a consultant and freelance statistician living in a cabin in Bolinas, California before returning to academia — and it was this outsider perspective that led him to later invent Random Forests, recognising that trees worked best when combined rather than perfected.
Intuition
Imagine you are playing the game 20 Questions. A good player does not start with "Is it a golden retriever?" — that is too specific and wastes a question if wrong. Instead they ask "Is it an animal?" — a question that cleanly splits everything in the universe into two large, useful groups. A decision tree is an algorithm that plays 20 Questions automatically, on your data. It tries every possible question ("Is petal length > 3cm?", "Is petal length > 3.1cm?", "Is sepal width > 2.5cm?", ...) and picks whichever one produces the two most sorted groups — groups where one type of thing clusters together as much as possible. Think of your training data as a jar of mixed jellybeans. Each question is like putting a divider in the jar. A great divider mostly puts red jellybeans on one side and yellow ones on the other. A terrible divider leaves both sides equally mixed. The algorithm is simply searching for the best dividers, one at a time. The danger of growing the tree too deep is like putting in so many dividers that you eventually have one jellybean per compartment — you have perfectly sorted this specific jar, but you have learned nothing useful about jellybeans in general.
Historical Development
Origins · 1 minhow this was discovered
Why It Matters
Prerequisites
What This Leads To
See Also
Random Forests · Gradient Boosting · Gini Impurity · Bias-Variance Tradeoff · Feature Importance