Decision tree in 3D

A standing board shows a decision tree, with a root question splitting into yes-or-no nodes and four leaf outcomes.Drag to turn

A decision tree is a supervised learning model that predicts an outcome by asking a series of questions about input features, until a leaf gives the answer. The questions are usually yes or no, as in CART, though some algorithms split into more than two branches.

A flowchart learned from data

A decision tree is a flowchart built from examples. Each internal node tests one feature, such as whether age is above 30. Each branch is an outcome of the test, and each leaf holds a prediction.

A tree that predicts a category is a classification tree. A tree that predicts a number is a regression tree, and its leaves usually hold the mean of the training targets that reached them.

How the tree chooses its splits

Training starts with all examples at the root. The algorithm tries candidate splits and keeps the one that makes the groups purest. Purity is often measured by Gini impurity, which is 1 minus the sum of squared class proportions. Entropy is another common measure.

The same step repeats on each child group. This is called recursive splitting. It stops when a group is pure, when a depth limit is reached, or when too few examples remain.

Worked example: scoring one split with Gini

Take 10 emails, 5 spam and 5 not spam. Gini at the root is 1 - (0.5² + 0.5²) = 1 - 0.5 = 0.5.

Suppose a split on the word "offer" sends 4 emails left, all spam, and 6 right, with 1 spam and 5 not spam. Left Gini is 0. Right Gini is 1 - ((1/6)² + (5/6)²) = 1 - (1/36 + 25/36) = 10/36, about 0.278.

Weighted Gini after the split is 0.4 × 0 + 0.6 × 0.278 = 0.167. Impurity falls by 0.5 - 0.167 = 0.333, so the split is a good one.

Common misconceptions

Deeper trees are not better by default. A tree grown until every leaf is pure usually memorizes noise, which is called overfitting. Limiting depth or pruning branches usually improves results on new data.

Trees do not need feature scaling, because each test compares one feature to a threshold. They are not automatically stable, though. Small changes in the training data can produce a very different tree.

Where decision trees are used

Trees suit tabular data such as credit scoring, medical triage rules and customer churn. Their paths can be read as if-then rules, which helps when decisions must be explained.

Random forests and gradient boosted trees combine many trees to gain accuracy. They are widely used on tabular data.

Key terms

Node
A point in the tree where a feature is tested and the data is divided.
Leaf
A final node that holds the prediction and has no further splits.
Gini impurity
A measure of how mixed the classes are in a group, equal to 1 minus the sum of squared class proportions.
Entropy
An alternative impurity measure based on the logarithm of class proportions.
Overfitting
Fitting noise in the training data so that performance on new data is worse.
Pruning
Removing branches that add little predictive value, to reduce overfitting.
Random forest
An ensemble of many trees trained on varied samples, whose predictions are combined.

Common questions

What is the difference between Gini impurity and entropy?

Both measure how mixed a group is, and both are zero for a pure group. In practice they usually choose similar splits.

How do trees handle numeric features?

They test thresholds, such as income below 40000, and try candidate cut points between sorted values.

Why are single trees often replaced by forests?

A single tree can change a lot with small data changes. Averaging many trees reduces that variance.

Learn this with a tutor

Sidechair's AI tutor will teach this topic in 3D. Join the waitlist for early access.

We'll only email you about Sidechair. Read our privacy policy.