1. Building the Decision Tree
1.1. Calculating Information Gain for Each Feature and Selecting the Root
To determine which feature to use as the root of the decision tree, we'll calculate the Information Gain (IG) for each feature. The feature with the highest IG will be selected as the root.
Step 1: Calculate the Entropy of the Target Variable ( H(Y) )
The target variable is Play, with possible values Yes and No.
- Total Instances (N): 10
- Positive Instances (Yes): 5
- Negative Instances (No): 5
[
H(Y) = -\sum_{i=1}^{c} p_i \log_2 p_i = -\left( \frac{5}{10} \log_2 \frac{5}{10} + \frac{5}{10} \log_2 \frac{5}{10} \right) = 1 \text{ bit}
]
Step 2: Calculate ( H(Y|X_k) ) for Each Feature
We'll compute the conditional entropy ( H(Y|X_k) ) for each feature ( X_k ).
-
Weather
- Possible Values: Sunny, Cloudy, Rainy
| Weather | Yes | No | Total |
|---|
| Sunny | 1 | 2 | 3 |
| Cloudy | 3 | 0 | 3 |
| Rainy | 1 | 3 | 4 |
[
H(Y|Weather) = \frac{3}{10} \times 0.918 + \frac{3}{10} \times 0 + \frac{4}{10} \times 0.811 = 0.599 \text{ bits}
]
-
Temperature
- Possible Values: Hot, Mild, Cool
| Temperature | Yes | No | Total |
|---|
| Hot | 2 | 2 | 4 |
| Mild | 3 | 2 | 5 |
| Cool | 0 | 1 | 1 |
[
H(Y|Temperature) = \frac{4}{10} \times 1 + \frac{5}{10} \times 0.971 + \frac{1}{10} \times 0 = 0.886 \text{ bits}
]
-
Humidity
- Possible Values: High, Normal
| Humidity | Yes | No | Total |
|---|
| High | 3 | 4 | 7 |
| Normal | 2 | 1 | 3 |
[
H(Y|Humidity) = \frac{7}{10} \times 0.983 + \frac{3}{10} \times 0.918 = 0.963 \text{ bits}
]
-
Wind
- Possible Values: Weak, Strong
| Wind | Yes | No | Total |
|---|
| Weak | 3 | 1 | 4 |
| Strong | 2 | 4 | 6 |
[
H(Y|Wind) = \frac{4}{10} \times 0.811 + \frac{6}{10} \times 0.918 = 0.875 \text{ bits}
]
Step 3: Calculate Information Gain ( I(X_k; Y) )
[
I(X_k; Y) = H(Y) - H(Y|X_k)
]
| Feature | ( H(Y|X_k) ) | ( I(X_k; Y) ) |
|--------------|----------------|------------------|
| Weather | 0.599 | 0.401 |
| Temperature | 0.886 | 0.114 |
| Humidity | 0.963 | 0.037 |
| Wind | 0.875 | 0.125 |
Conclusion: Weather has the highest information gain (( IG = 0.401 )), making it the optimal choice for the root node.
Decision Tree Root and Initial Split
-
Root Feature: Weather
-
Branches:
- Weather = Sunny
- Weather = Cloudy
- Weather = Rainy
Visualization:
[Weather]
/ | \
Sunny Cloudy Rainy
/ | \
Instances Instances Instances
1,3,8 2,4,9 5,6,7,10
1.2. Building the Full Decision Tree Until Complete Purity
We will recursively apply the Information Gain calculation to each subset until all leaves are pure (i.e., all instances in a leaf belong to the same class).
1.2.1. Splitting on Weather = Sunny (Instances 1, 3, 8)
- Subset:
- Instances: 1 (No), 3 (Yes), 8 (No)
- Classes: Yes = 1, No = 2
Remaining Features: Temperature, Humidity, Wind
Calculate Information Gain for Remaining Features:
-
Temperature
| Temperature | Yes | No | Total |
|---|
| Hot | 0 | 2 | 2 |
| Mild | 1 | 0 | 1 |
[
H(Y|Temperature=Hot) = 1 \text{ bit} \quad (Yes=0, No=2)
]
[
H(Y|Temperature=Mild) = 0 \text{ bits} \quad (Yes=1, No=0)
]
[
H(Y|Temperature) = \frac{2}{3} \times 1 + \frac{1}{3} \times 0 = 0.667 \text{ bits}
]
[
I(Temperature; Y) = 1 - 0.667 = 0.333 \text{ bits}
]
-
Humidity
| Humidity | Yes | No | Total |
|---|
| High | 0 | 2 | 2 |
| Normal | 1 | 0 | 1 |
[
H(Y|Humidity=High) = 1 \text{ bit} \quad (Yes=0, No=2)
]
[
H(Y|Humidity=Normal) = 0 \text{ bits} \quad (Yes=1, No=0)
]
[
H(Y|Humidity) = \frac{2}{3} \times 1 + \frac{1}{3} \times 0 = 0.667 \text{ bits}
]
[
I(Humidity; Y) = 1 - 0.667 = 0.333 \text{ bits}
]
-
Wind
| Wind | Yes | No | Total |
|---|
| Weak | 1 | 1 | 2 |
| Strong | 0 | 1 | 1 |
[
H(Y|Wind=Weak) = 1 \text{ bit} \quad (Yes=1, No=1)
]
[
H(Y|Wind=Strong) = 0 \text{ bits} \quad (Yes=0, No=1)
]
[
H(Y|Wind) = \frac{2}{3} \times 1 + \frac{1}{3} \times 0 = 0.667 \text{ bits}
]
[
I(Wind; Y) = 1 - 0.667 = 0.333 \text{ bits}
]
All remaining features have the same Information Gain (( 0.333 ) bits). Select any (e.g., Temperature) as the next split.
Splitting on Temperature:
-
Temperature = Hot
- Instances: 1, 8
- Classes: No = 2 → Pure Node: No
-
Temperature = Mild
- Instances: 3
- Classes: Yes = 1 → Pure Node: Yes
Updated Tree:
[Weather]
/ | \
Sunny Cloudy Rainy
/ | \
[Temperature] Instances Instances
/ \ 2,4,9 5,6,7,10
Hot Mild
| |
No Yes
1.2.2. Splitting on Weather = Cloudy (Instances 2, 4, 9)
- Subset:
- Instances: 2 (Yes), 4 (Yes), 9 (Yes)
- Classes: Yes = 3 → Pure Node: Yes
No further splitting needed.
1.2.3. Splitting on Weather = Rainy (Instances 5, 6, 7, 10)
- Subset:
- Instances: 5 (No), 6 (No), 7 (Yes), 10 (No)
- Classes: Yes = 1, No = 3
Remaining Features: Temperature, Humidity, Wind
Calculate Information Gain for Remaining Features:
-
Temperature
| Temperature | Yes | No | Total |
|---|
| Mild | 1 | 2 | 3 |
| Cool | 0 | 1 | 1 |
[
H(Y|Temperature=Mild) = -\left( \frac{1}{3} \log_2 \frac{1}{3} + \frac{2}{3} \log_2 \frac{2}{3} \right) \approx 0.918 \text{ bits}
]
[
H(Y|Temperature=Cool) = 0 \text{ bits} \quad (Yes=0, No=1)
]
[
H(Y|Temperature) = \frac{3}{4} \times 0.918 + \frac{1}{4} \times 0 = 0.689 \text{ bits}
]
[
I(Temperature; Y) = 1 - 0.689 = 0.311 \text{ bits}
]
-
Humidity
| Humidity | Yes | No | Total |
|---|
| High | 1 | 3 | 4 |
| Normal | 0 | 0 | 0 |
Note: All instances have High humidity.
[
H(Y|Humidity=High) = -\left( \frac{1}{4} \log_2 \frac{1}{4} + \frac{3}{4} \log_2 \frac{3}{4} \right) \approx 0.811 \text{ bits}
]
[
H(Y|Humidity) = \frac{4}{4} \times 0.811 = 0.811 \text{ bits}
]
[
I(Humidity; Y) = 1 - 0.811 = 0.189 \text{ bits}
]
-
Wind
| Wind | Yes | No | Total |
|---|
| Weak | 1 | 0 | 1 |
| Strong | 0 | 3 | 3 |
[
H(Y|Wind=Weak) = 0 \text{ bits} \quad (Yes=1, No=0)
]
[
H(Y|Wind=Strong) = 0 \text{ bits} \quad (Yes=0, No=3)
]
[
H(Y|Wind) = \frac{1}{4} \times 0 + \frac{3}{4} \times 0 = 0 \text{ bits}
]
[
I(Wind; Y) = 1 - 0 = 1 \text{ bit}
]
Conclusion: Wind has the highest information gain (( IG = 1 ) bit). Split on Wind.
Splitting on Wind:
-
Wind = Weak
- Instances: 7
- Classes: Yes = 1 → Pure Node: Yes
-
Wind = Strong
- Instances: 5, 6, 10
- Classes: No = 3 → Pure Node: No
Final Decision Tree:
[Weather]
/ | \
Sunny Cloudy Rainy
/ | \
[Temperature] Instances [Wind]
/ \ 2,4,9 / \
Hot Mild Weak Strong
| | | |
No Yes Yes No
Summary of the Tree:
-
Weather = Sunny
- Temperature = Hot: No
- Temperature = Mild: Yes
-
Weather = Cloudy: Yes
-
Weather = Rainy
- Wind = Weak: Yes
- Wind = Strong: No
1.3. Pruning the Decision Tree (Extra Points)
Objective: Find a subtree ( T' ) that minimizes the criterion:
[
C(T') = \sum_{\tau=1}^{T'} Q(\tau) + \lambda \times \text{(number of leaves in } T')
]
where:
- ( Q(\tau) ) is the entropy (impurity) of a leaf.
- ( \lambda ) is a regularization parameter balancing purity and tree complexity.
Assessing Whether Pruning is Needed:
Decision:
Since the pruned subtree maintains pure leaves and reduces the number of leaves without increasing impurity, it minimizes the criterion ( C(T') ).
Conclusion: Pruning is beneficial. The original tree can be pruned by removing the Temperature split under Sunny, resulting in a simpler tree with fewer leaves without compromising purity.
Pruned Decision Tree:
[Weather]
/ | \
Sunny Cloudy Rainy
| | \
No Yes [Wind]
/ \
Weak Strong
| |
Yes No