Learn how computers make decisions just like humans.
In nature, a tree starts from a root, grows into branches, and ends with leaves. A Decision Tree follows the same concept.
A Decision Tree is a Supervised Machine Learning Algorithm used for Classification and Regression tasks. It learns patterns by asking questions and making decisions.
Goal: Predict whether a student will order pizza.
| Weather | Pocket Money | Friend Available | Order Pizza |
|---|---|---|---|
| Hot | 500 | Yes | Yes |
| Hot | 400 | Yes | Yes |
| Cold | 100 | No | No |
| Rainy | 200 | No | No |
| Rainy | 500 | Yes | Yes |
| Hot | 600 | No | Yes |
Understanding Root Nodes, Internal Nodes, Branches, Leaf Nodes, Pure Nodes and Impure Nodes
Imagine you want to decide whether to play cricket. You don't randomly decide. You first ask questions.
Decision Trees work exactly the same way. They continuously ask questions until a final decision is reached.
Starting point of the tree
Decision-making question
Path between nodes
Final Prediction
The Root Node is the first question asked by the Decision Tree. It is the top-most node of the tree. Every Decision Tree contains only one Root Node.
Internal Nodes are additional questions asked after the Root Node. They help separate mixed data.
Branches connect nodes together. They represent answers to questions.
| Question | Branches |
|---|---|
| Weather Hot? | Yes / No |
| Money > 300? | Yes / No |
Leaf Nodes contain final predictions. No further questions are asked.
A Pure Node contains only one class.
Impure Nodes contain multiple classes.
| Pure Node | Impure Node |
|---|---|
| Only One Class | Multiple Classes |
| Easy Prediction | Difficult Prediction |
| Impurity = 0 | Impurity > 0 |
| Preferred | Not Preferred |
Goal: Predict whether a cricket match will be played.
| Weather | Ground Wet | Play Match |
|---|---|---|
| Sunny | No | Yes |
| Sunny | No | Yes |
| Rainy | Yes | No |
| Rainy | Yes | No |
| Cloudy | No | Yes |
Understanding how a Decision Tree chooses the first question before learning Gini Impurity.
We want to predict:
To answer this question, we have collected data.
| Weather | Pocket Money | Friend Available | Order Pizza |
|---|---|---|---|
| Hot | 500 | Yes | Yes |
| Hot | 400 | Yes | Yes |
| Cold | 100 | No | No |
| Rainy | 200 | No | No |
| Rainy | 500 | Yes | Yes |
| Hot | 600 | No | Yes |
Initially, the Decision Tree has not asked any questions. All records are inside a single node.
Some students ordered pizza. Some students did not order pizza.
Currently all records are inside one node.
No confusion. All records belong to one class.
Mixed classes. The tree is confused.
Decision Trees hate confusion.
The goal of the tree is:
The tree can choose any feature.
But which one is best?
The tree tests each feature one by one.
The tree creates a temporary split.
| Weather | Pizza Result |
|---|---|
| Hot |
YES
YES YES |
| Cold | NO |
| Rainy |
YES
NO |
Pure Node โ
Pure Node โ
Impure Node โ
Weather created:
This seems good. But can another feature do even better?
The tree now tests Pocket Money. Then Friend Available.
The tree needs a numerical score to compare features.
How do we measure this?
In Part 3B, we will learn exactly how Gini is calculated.
How does a Decision Tree compare features and choose the Root Node?
In Part 3A, we tested Weather. The split looked good.
But how can we prove it is good?
This score is called:
Gini measures confusion.
No confusion. Pure Node.
Confusing. Impure Node.
P(Yes) = Probability of YES
P(No) = Probability of NO
Total Records = 4
P(Yes)=4/4=1
P(No)=0/4=0
Gini = 1 - (1)ยฒ - (0)ยฒ = 1 - 1 - 0 = 0
P(Yes)=1/2=0.5
P(No)=1/2=0.5
Gini = 1 - (0.5)ยฒ - (0.5)ยฒ = 1 - 0.25 - 0.25 = 0.5
P(Yes)=1 P(No)=0 Gini =1-(1ยฒ)-(0ยฒ) =0
P(Yes)=0 P(No)=1 Gini =0
P(Yes)=0.5 P(No)=0.5 Gini =1-(0.5ยฒ)-(0.5ยฒ) =0.5
Not every node contains the same number of records.
| Node | Records | Gini |
|---|---|---|
| Hot | 3 | 0 |
| Cold | 1 | 0 |
| Rainy | 2 | 0.5 |
Weighted Gini = (3/6 ร 0) + (1/6 ร 0) + (2/6 ร 0.5) = 0.167
| Feature | Weighted Gini |
|---|---|
| Weather | 0.167 |
| Pocket Money | 0.25 |
| Friend Available | 0.33 |