Decision tree example with a simple worked example

Decision tree example with a simple worked example

A decision tree example is one of the cleanest ways to see how a classifier works on real numbers. Instead of learning a single formula, a decision tree asks a sequence of yes or no questions, then uses the answers to reach a prediction.

In this article, I will build a tiny tree by hand. We will use eight training examples, calculate impurity, choose a split, and follow one input all the way to a class label. The model will be small enough to fit on a page, which makes the maths easy to check.

Our tiny dataset

Suppose we want to predict whether a student passes an exam. We use two features: whether the student studied for at least 3 hours, and whether they revised with practice questions. The label is pass or fail.

StudentStudied 3+ hoursPractice questionsResult
1NoNoFail
2NoYesFail
3NoNoFail
4NoYesFail
5YesNoPass
6YesYesPass
7YesNoPass
8YesYesFail

There are 4 fails and 4 passes, so the root node is mixed. A tree needs a way to measure how mixed a node is. One common choice is Gini impurity:

\[G = 1 - \sum_k p_k^2\]

For two classes, pass and fail, this becomes:

\[G = 1 - p_{pass}^2 - p_{fail}^2\]

At the root, \(p_{pass}=4/8=0.5\) and \(p_{fail}=4/8=0.5\), so:

\[G = 1 - 0.5^2 - 0.5^2 = 0.5\]

How the tree chooses a split in a decision tree example

The tree tries possible questions and picks the one that lowers impurity the most. Let us test the question Studied 3+ hours.

The left branch, No, contains students 1 to 4. All four failed, so its impurity is:

\[G_{left} = 1 - 0^2 - 1^2 = 0\]

The right branch, Yes, contains students 5 to 8. There are 3 passes and 1 fail, so:

\[G_{right} = 1 - (3/4)^2 - (1/4)^2 = 1 - 9/16 - 1/16 = 6/16 = 0.375\]

The weighted impurity after the split is:

\[G_{split} = \frac{4}{8} \cdot 0 + \frac{4}{8} \cdot 0.375 = 0.1875\]

This is much lower than the root impurity of 0.5, so the tree keeps this split. In plain English, the question about study time does a good job of separating the classes.

Splitting the mixed branch

The right branch is still mixed, so the tree asks another question there. Now we only look at students 5 to 8. A natural second question is Practice questions.

Among the students with 3+ study hours, the data look like this:

  • Practice questions = No: students 5 and 7, both pass.
  • Practice questions = Yes: students 6 and 8, one pass and one fail.

The first child is pure, so its impurity is 0. The second child has one pass and one fail, so:

\[G = 1 - (1/2)^2 - (1/2)^2 = 0.5\]

The weighted impurity inside this branch is:

\[G_{branch} = \frac{2}{4} \cdot 0 + \frac{2}{4} \cdot 0.5 = 0.25\]

The tree now has a simple shape:

  • If studied fewer than 3 hours, predict fail.
  • If studied 3 or more hours and did practice questions, predict pass for the branch where the training data were mostly pass, and keep the node label as the majority class in that leaf.
  • If studied 3 or more hours and did not do practice questions, predict pass.

Following one path to a prediction

Let us classify a new student who studied 4 hours and did practice questions. The tree follows a full path:

  1. Check Studied 3+ hours? The answer is yes, so go right.
  2. Check Practice questions? The answer is yes, so go to that child node.
  3. Use the label at that leaf. In the training data, that leaf is split between one pass and one fail, so the tree predicts the majority class seen at that leaf or, in an implementation, stores class probabilities such as 0.5 and 0.5.

If we instead classify a student who studied 2 hours, the path is shorter. The first question sends them left, and that leaf is pure fail, so the prediction is fail. That is the appeal of trees: the path is easy to read, and each step has a clear reason.

What this simple model is doing under the hood

A decision tree searches over many possible splits and picks the one that reduces impurity. In classification, the usual targets are Gini impurity or entropy. In regression, trees use different split scores, often based on variance. The central idea is the same: make each child node more homogeneous than its parent.

Two small details matter in practice. First, a tree can keep splitting until every leaf is pure, but that often fits noise in the training set. Second, limits such as maximum depth, minimum samples per leaf, or minimum impurity decrease help stop the tree from growing too far. That is how real trees avoid becoming a memorised list of training examples.

Where decision trees fit in machine learning

Decision trees are a good first model because they show how classification can work with simple rules, counts, and thresholds. They also appear inside larger methods such as random forests and gradient boosted trees. Those methods combine many trees, often with better accuracy than one tree on its own.

If you want to connect this article to the wider course, the matching unit is Classifiers and Decision Trees. For related reading, you can also compare this with how a spam filter decides, see why a positive test for a rare disease is usually wrong, or review ten equations every AI student should know.

For source material, the basic implementation ideas are described in the scikit-learn decision tree guide, and the library’s broader model overview is documented in the tree module reference. A short industry overview is also available from IBM on decision trees, which gives a plain-language summary of how the method works.

Frequently asked questions

Why do decision trees use impurity?

Impurity gives the tree a numeric way to measure how mixed a node is. A good split lowers that number, so the children become easier to classify.

Why did we use Gini impurity instead of entropy?

Either one can work for classification. Gini impurity is slightly easier to compute by hand, which makes it useful for a first worked example.

Can a tree memorise the training data?

Yes. A very deep tree can keep splitting until most leaves are pure, which often fits the training set too closely. That is why real training usually limits tree size.

Learn how machines learn, properly

The AI Masterclass: weekly units, quizzes, a final exam, and a certificate.
About the program