Problem 1.3
Pruning the Decision Tree
In this problem, we are asked to prune a decision tree to minimize the following criterion:
[ C(T') = \sum_{\tau=1}^{|T'|} Q(\tau) + \lambda \times (\text{number of leaves in } T') ]
where:
- ( Q(\tau) ) is the entropy (measure of impurity) at leaf node ( \tau ).
- ( \lambda ) is a regularization parameter controlling the penalty for the number of leaves.
- ( T' ) is a subtree of the original tree ( T ).
Objective:
- Find a subtree ( T' ) that minimizes ( C(T') ).
- Decide whether pruning is necessary based on the criterion.
Step 1: Reconstruct the Decision Tree
Before we can prune the tree, we need to understand its structure. Let's reconstruct the decision tree built in the earlier steps.
Given Data:
| Day | Weather | Temperature | Humidity | Wind | Play |
|---|
| 1 | Sunny | Hot | High | Weak | No |
| 2 | Cloudy | Hot | High | Weak | Yes |
| 3 | Sunny | Mild | Normal | Strong | Yes |
| 4 | Cloudy | Mild | High | Strong | Yes |
| 5 | Rainy | Mild | High | Strong | No |
| 6 | Rainy | Cool | Normal | Strong | No |
| 7 | Rainy | Mild | High | Weak | Yes |
| 8 | Sunny | Hot | High | Strong | No |
| 9 | Cloudy | Hot | Normal | Weak | Yes |
| 10 | Rainy | Mild | High | Strong | No |
Building the Tree:
-
Root Node: The feature with the highest information gain is Weather.
-
First Split (Weather):
-
Sunny
- Days: 1, 3, 8
- Play: No, Yes, No
- Entropy: Not pure (needs further splitting).
-
Cloudy
- Days: 2, 4, 9
- Play: Yes, Yes, Yes
- Entropy: Pure (leaf node with class Yes).
-
Rainy
- Days: 5, 6, 7, 10
- Play: No, No, Yes, No
- Entropy: Not pure (needs further splitting).
-
Second Split (Sunny Node):
-
Second Split (Rainy Node):
Final Tree Structure:
-
Root: Weather
-
Sunny
-
Humidity
- High: No (leaf node)
- Normal: Yes (leaf node)
-
Cloudy: Yes (leaf node)
-
Rainy
-
Wind
- Weak: Yes (leaf node)
- Strong: No (leaf node)
Step 2: Compute the Criterion ( C(T') ) for the Original Tree
The original tree has 5 leaf nodes. Since all leaf nodes are pure (entropy ( Q(\tau) = 0 )), the criterion simplifies to:
[ C(T) = \sum_{\tau=1}^{5} Q(\tau) + \lambda \times 5 = 0 + 5\lambda = 5\lambda ]
Step 3: Consider Possible Prunings
We need to evaluate whether pruning any subtrees can reduce ( C(T') ).
Option 1: Prune the "Rainy" Subtree
-
Instead of splitting "Rainy" on "Wind", make "Rainy" a leaf node.
-
Compute entropy ( Q(\tau) ) for "Rainy":
-
Play outcomes: Yes (1), No (3)
-
( p(\text{Yes}) = \frac{1}{4} ), ( p(\text{No}) = \frac{3}{4} )
-
Entropy:
[
Q(\text{Rainy}) = -\left( \frac{1}{4} \log_2 \frac{1}{4} + \frac{3}{4} \log_2 \frac{3}{4} \right) = 0.8113
]
-
New number of leaf nodes: 4 (Sunny-Humidity branches, Cloudy, Rainy)
-
Criterion:
[
C(T') = 0 + 0 + 0 + 0.8113 + \lambda \times 4 = 0.8113 + 4\lambda
]
Option 2: Prune the "Sunny" Subtree
-
Instead of splitting "Sunny" on "Humidity", make "Sunny" a leaf node.
-
Compute entropy ( Q(\tau) ) for "Sunny":
-
Play outcomes: Yes (1), No (2)
-
( p(\text{Yes}) = \frac{1}{3} ), ( p(\text{No}) = \frac{2}{3} )
-
Entropy:
[
Q(\text{Sunny}) = -\left( \frac{1}{3} \log_2 \frac{1}{3} + \frac{2}{3} \log_2 \frac{2}{3} \right) = 0.9183
]
-
New number of leaf nodes: 4 (Sunny, Cloudy, Rainy-Wind branches)
-
Criterion:
[
C(T') = 0.9183 + 0 + 0 + 0 + \lambda \times 4 = 0.9183 + 4\lambda
]
Option 3: Prune Both "Sunny" and "Rainy" Subtrees
-
Make both "Sunny" and "Rainy" nodes leaf nodes.
-
Entropy at "Sunny" and "Rainy" already computed.
-
New number of leaf nodes: 3 (Sunny, Cloudy, Rainy)
-
Criterion:
[
C(T') = 0.9183 + 0 + 0.8113 + \lambda \times 3 = 1.7296 + 3\lambda
]
Step 4: Compare the Criteria for Each Option
We need to find which option minimizes ( C(T') ). We can compare the criteria for different values of ( \lambda ).
Option Summaries:
| Option | ( C(T') ) |
|---|
| No Pruning (Original Tree) | ( 0 + 5\lambda ) |
| Prune "Rainy" Subtree | ( 0.8113 + 4\lambda ) |
| Prune "Sunny" Subtree | ( 0.9183 + 4\lambda ) |
| Prune "Sunny" and "Rainy" Subtrees | ( 1.7296 + 3\lambda ) |
Analysis:
-
Compare No Pruning vs. Prune "Rainy":
- Difference: ( (5\lambda) - (0.8113 + 4\lambda) = \lambda - 0.8113 )
- The pruned tree has a lower ( C(T') ) when ( \lambda > 0.8113 ).
-
Compare Prune "Rainy" vs. Prune "Sunny":
- Difference: ( (0.8113 + 4\lambda) - (0.9183 + 4\lambda) = -0.1070 )
- Pruning "Rainy" subtree yields a lower ( C(T') ).
-
Compare Prune "Rainy" vs. Prune Both Subtrees:
- Difference: ( (0.8113 + 4\lambda) - (1.7296 + 3\lambda) = -0.9183 + \lambda )
- Pruning both subtrees yields a lower ( C(T') ) when ( \lambda > 0.9183 ).
Step 5: Determine the Best Option Based on ( \lambda )
- When ( \lambda < 0.8113 ): The original tree (no pruning) has the lowest ( C(T') ).
- When ( 0.8113 \leq \lambda < 0.9183 ): Pruning the "Rainy" subtree minimizes ( C(T') ).
- When ( \lambda \geq 0.9183 ): Pruning both "Sunny" and "Rainy" subtrees yields the lowest ( C(T') ).
Conclusion:
Based on the criterion computation, whether we need to prune the tree depends on the value of ( \lambda ):
-
If ( \lambda ) is small (( \lambda < 0.8113 )), the penalty for having more leaves is low, so the original tree (without pruning) is preferable as it has the smallest ( C(T') ) despite having more leaves.
-
If ( \lambda ) is moderate (( 0.8113 \leq \lambda < 0.9183 )), pruning the "Rainy" subtree reduces ( C(T') ), making the tree simpler without significantly increasing impurity.
-
If ( \lambda ) is large (( \lambda \geq 0.9183 )), the penalty for having extra leaves outweighs the benefit of reduced impurity, so pruning both "Sunny" and "Rainy" subtrees minimizes ( C(T') ).
Recommendation:
-
Assess the value of ( \lambda ): If it is known or can be estimated based on domain knowledge or validation performance, choose the pruning strategy accordingly.
-
In absence of a specific ( \lambda ): It may be prudent to avoid pruning unless there is a strong reason to simplify the tree (e.g., improving generalization by reducing overfitting).
Additional Notes for Calculations:
Final Answer:
We may need to prune the tree found in the earlier steps depending on the value of ( \lambda ). If ( \lambda ) is greater than ( 0.8113 ), pruning reduces the criterion ( C(T') ), suggesting pruning is beneficial. If ( \lambda ) is less than ( 0.8113 ), the original tree without pruning is preferable.