Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 58

Building a Two-Level Decision Tree by Hand

Recommended resources

Welcome back. In our previous lesson, you mastered the critical step of selecting the single best split for a node by comparing purity metrics like the Gini Impurity. You found that for your bra-fitting business, Customer Age was the most informative first question, while for your relationship dynamics research, Total Hours Spent provided the best initial separation.

Today, we will build upon that foundation to achieve this lesson's objective: to construct a simple two-level decision tree by hand for a small dataset. We will take the single-split method and apply it recursively, creating a flowchart of decisions that provides a more nuanced classification. This process of repeatedly splitting the data is the core of how algorithms like C4.5 and CART build powerful predictive models from scratch.

Let's begin by looking at the goal. We start with a table of data and, by the end of this lesson, you will have created a tree structure like the one on the right.

On the left is a raw dataset containing customer attributes and their purchase decisions. On the right is the final decision tree, which transforms the data into a set of sequential rules to predict whether a customer will buy a computer.

The Recursive Algorithm for Building a Tree

Constructing a decision tree is a top-down, greedy process. "Top-down" means we start at the root node with all the data, and "greedy" means that at each step, we make the decision that looks best at that moment (i.e., the split with the highest purity gain) without looking ahead to see if it leads to a better overall tree. This step-by-step approach is elegantly described by the ID3 algorithm. While ID3 uses Information Gain, the recursive structure is identical to what we will use with the Gini Impurity.

A brief guide from Udacity provides an excellent overview of this recursive logic.

[PDF] ID3 Algorithm for Decision Trees - Amazon S3

This document introduces the ID3 algorithm, which formalizes the recursive process of building a tree. While it uses a metric called Information Gain, the core steps of splitting the data and recursively building sub-trees are exactly what we'll be doing.

First, read the Summary which outlines the 5-step process. Pay close attention to step 5, which introduces the idea of recursion. Then, review the Pseudocode, which shows how the base cases (the stopping conditions) are checked before the algorithm calls itself on the smaller data subsets.

As you read, the key idea to internalize is the concept of recursion. After we make the first split (which you learned to do in the last lesson), we are left with several smaller datasets. For each of these, we simply ask the same question again: "What is the best attribute to split this subset of data?" We repeat this until we can't split anymore.

A split is considered "finished" and becomes a leaf node (a final decision) if it meets one of these stopping conditions:

  1. All data points in the node belong to the same class (the node is pure).
  2. There are no more attributes left to split on.
  3. The data subset at that node is empty.

If a leaf node is not pure (due to stopping condition #2), the classification assigned to that leaf is simply the majority class of the data points that ended up there.

Now, let's apply this recursive logic to our two scenarios.


Application 1: Constructing the Bra-Fitting Recommendation Tree

In the last lesson, we analyzed your client data and found the best initial split was Customer Age < 41.5. This split resulted in a weighted Gini Impurity of 0.1875 and became the root of our tree.

The Root Split:

  • Root Node: Is Customer Age < 41.5?
  • This split creates two branches and two new data subsets.

Branch 1 (True): Customer Age < 41.5
This group consists of 4 customers. The data is:

Customer ID Bra Style Repeat Customer
22 Plunge No
25 Balconette Yes
35 Plunge Yes
38 Plunge Yes

The outcome is mixed (1 'No', 3 'Yes'), so this node is not pure. We must split it again. The only remaining attribute is Bra Style.

  • Gini of this node:
  • Evaluate split on Bra Style:
    • Child Plunge (3 customers): 1 'No', 2 'Yes'. This node is not pure.
    • Child Balconette (1 customer): 0 'No', 1 'Yes'. This node is pure.

Since this is our only attribute, it is automatically the best split for this node.

Branch 2 (False): Customer Age >= 41.5
This group consists of the other 4 customers. The data is:

Customer ID Bra Style Repeat Customer
45 Balconette No
52 Balconette No
55 Plunge No
60 Balconette No

The outcome is uniform (0 'Yes', 4 'No'). This node is pure. It becomes a leaf node with the classification 'No'.

The Final Two-Level Tree

By putting these pieces together, we construct our two-level decision tree.

  1. Level 1 Question (Root): Is the customer's age less than 41.5?
    • If NO: The customer is predicted to be a 'No' (not a repeat customer). This is a leaf node.
    • If YES: Proceed to the Level 2 question.
  2. Level 2 Question: Is the bra style a 'Balconette'?
    • If YES: The customer is predicted to be a 'Yes' (a repeat customer). This is a leaf node.
    • If NO (i.e., 'Plunge'): We reach a leaf node with 2 'Yes' and 1 'No'. We classify by the majority. The prediction is 'Yes'.

This tree gives you a practical decision process: for older customers, the model predicts they won't return. For younger customers, the prediction depends on their style preference, with 'Balconette' wearers being certain repeat customers and 'Plunge' wearers being likely repeat customers.


Application 2: Constructing the Relationship Conflict Tree

For your research, the best root split was Total Hours Spent < 11. This split gave a weighted Gini Impurity of 0.178.

The Root Split:

  • Root Node: Was Total Hours Spent < 11?
  • This creates two branches.

Branch 1 (True): Total Hours Spent < 11
This group contains 5 interaction days. The data is:

Day Primary Partner Present Conflict Next Day
1 Yes Yes
2 No No
3 Yes No
4 No No
5 Yes No

The outcome is mixed (1 'Yes', 4 'No'), so this node is not pure. The only remaining attribute is Primary Partner Present.

  • Gini of this node:
  • Evaluate split on Primary Partner Present:
    • Child Yes (3 days): 1 'Yes', 2 'No'. This node is not pure.
    • Child No (2 days): 0 'Yes', 2 'No'. This node is pure.

This is the only possible split, so we use it.

Branch 2 (False): Total Hours Spent >= 11
This group contains the remaining 4 days. The data is:

Day Primary Partner Present Conflict Next Day
6 Yes Yes
7 No Yes
8 Yes Yes
9 No Yes

The outcome is uniform (4 'Yes', 0 'No'). This node is pure. It becomes a leaf node with the classification 'Yes'.

The Final Two-Level Tree

This gives us the following set of rules for predicting conflict.

  1. Level 1 Question (Root): Was the total time spent together less than 11 hours?
    • If NO: The prediction is 'Yes' (conflict will occur). This is a leaf node.
    • If YES: Proceed to the Level 2 question.
  2. Level 2 Question: Was the primary partner absent ('No')?
    • If YES: The prediction is 'No' (no conflict will occur). This is a leaf node.
    • If NO (i.e., primary partner was present): We reach a leaf with 1 'Yes' and 2 'No'. By majority vote, the prediction is 'No'.

The resulting model suggests a clear boundary: spending a large amount of time together (>= 11 hours) always leads to conflict in this dataset. For shorter interactions, conflict is avoided, especially when the primary partner is not present.


Conclusion

In this lesson, you have taken the mechanics of finding an optimal split and applied them recursively to build a complete, albeit simple, decision tree. You now understand how a sequence of greedy, locally optimal decisions combines to form a useful classification model that can be easily interpreted as a set of if-then rules.

Key Takeaways:

  • Decision trees are built using a top-down, greedy, recursive algorithm.
  • The process starts at the root node and recursively finds the best split for each resulting data subset.
  • The recursion stops at a node when it becomes pure, when there are no more attributes, or when it's empty. This node is then called a leaf.
  • The final prediction for any path through the tree is the classification of its terminal leaf node, often determined by a majority vote.

This lesson concludes our module on Supervised Classification Algorithms. You have now seen how to classify data points using distance (KNN), probability (Naive Bayes), and rule-based logic (Decision Trees).

In our next module, we will shift focus from prediction to discovery. The first topic will be Association Rule Mining, where you will learn to calculate metrics like support, confidence, and lift to uncover hidden patterns and relationships within your data, such as "customers who buy X also tend to buy Y."