Data Warehousing and Data MiningUnit 711 min read
Decision Trees & Rule-Based Learning: ID3, Overfitting, Rule Extraction
Unit 7 of Data Warehousing and Data Mining covers decision tree algorithms (ID3, C4.5), rule-based classification, overfitting/underfitting detection, pruning techniques, and real-world applications in predictive modeling. Learn how to build trees from data, extract rules, and evaluate model performance using metrics l
TAKEAWAYS:
- Decision trees split data recursively using attributes (e.g., age, car type) to classify outcomes (e.g., loan approval), with ID3 using information gain to select splits.
- Overfitting occurs when a tree memorizes noise (e.g., a leaf node for a single data point), while underfitting happens when splits are too shallow (e.g., ignoring key attributes like
Car Type). - Rule extraction converts a decision tree into
IF-THENrules (e.g.,IF Age ≤ 30 AND Car Type = Sports THEN Class = High). - Pruning (pre- or post-) fixes overfitting by simplifying the tree (e.g., merging nodes with similar classes).
- Laplace smoothing adjusts probabilities for unseen attribute values (e.g.,
P(Class=High|Age ≤ 30) = (count + 1)/(total + classes)). - Real-world use: Banks use decision trees to approve loans (e.g., NMB Bank’s
IF Credit Score > 700 THEN Approve), while eSewa predicts user churn (e.g.,IF Last Login > 30 Days THEN Flag for Retention).
1. Decision Trees: The Core Idea
Decision trees classify data by splitting it based on attribute values. Each internal node tests an attribute (e.g., Age ≤ 30?), branches represent outcomes, and leaves hold class labels (e.g., High/Low risk).
How ID3 Works: Step-by-Step
ID3 (Iterative Dichotomiser 3) builds trees using information gain, a measure of how much an attribute reduces uncertainty (entropy). The algorithm:
- Selects the best attribute to split on (highest information gain).
- Splits the data into subsets.
- Repeats until all leaves are pure (all samples belong to one class) or no attributes remain.
graph TD
A["Root: Class?"] -->|"Age ≤ 30"| B["Left: High Risk"]
A -->|"Age > 30"| C["Right: Split on Car Type"]
C -->|"Car Type = Family"| D["Leaf: Low Risk"]
C -->|"Car Type = Sports"| E["Leaf: High Risk"]Example: Train ID3 on the dataset below to classify loan approval (High/Low):
| TID | Age | Car Type | Class |
|---|---|---|---|
| 1 | ≤30 | Family | High |
| 2 | ≤30 | Sports | High |
| 3 | >30 | Sports | High |
| 4 | >30 | Family | Low |
Step 1: Calculate Entropy of the Target (Class)
Entropy measures disorder. For this dataset:
High: 3/4 samples,Low: 1/4 samples.- Entropy = .
Step 2: Compute Information Gain for Each Attribute
Attribute
Age:- Split 1:
Age ≤ 30→ 2 samples (High,High) → Entropy = 0. - Split 2:
Age > 30→ 2 samples (High,Low) → Entropy = 1. - Weighted entropy = .
- Information Gain = Entropy before (0.811) – Entropy after (0.5) = 0.311.
- Split 1:
Attribute
Car Type:- Split 1:
Family→ 2 samples (High,Low) → Entropy = 1. - Split 2:
Sports→ 2 samples (High,High) → Entropy = 0. - Weighted entropy = .
- Information Gain = 0.811 – 0.5 = 0.311.
- Tie: Choose
Agefirst (alphabetical order in ID3).
- Split 1:
Step 3: Build the Tree
- Split on
Age ≤ 30:- Left branch: Both samples are
High→ Leaf node:High. - Right branch: Mixed (
High,Low) → Recurse.
- Left branch: Both samples are
- For
Age > 30, split onCar Type:Family→Low(leaf).Sports→High(leaf).
Final Tree:
graph TD
A["Class?"] -->|"Age ≤ 30"| B["High"]
A -->|"Age > 30"| C["Car Type?"]
C -->|"Family"| D["Low"]
C -->|"Sports"| E["High"]2. Overfitting and Underfitting: The Pitfalls
Overfitting: The tree fits training data too closely, capturing noise (e.g., a leaf for a single outlier). Symptom: High training accuracy but poor test performance.
- Example: A tree with 10 levels for 4 samples is overfit.
- Detection: Compare training vs. validation accuracy (large gap = overfitting).
Underfitting: The tree is too simple, ignoring important patterns (e.g., stopping at root node).
- Example: A tree that always predicts
Highfor all samples.
- Example: A tree that always predicts
3. Fixing Overfitting: Pruning Techniques
| Technique | Description | When to Use |
|---|---|---|
| Pre-pruning | Stop splitting if gain < threshold or samples < min. | Early in tree growth. |
| Post-pruning | Remove leaves/nodes after building (e.g., reduced-error pruning). | After full tree construction. |
| Cost-complexity | Merge nodes if error increase < complexity reduction (parameter α). | For optimal bias-variance tradeoff. |
Example: Prune the earlier tree by merging nodes where classes are close (e.g., Age > 30 and Car Type = Family could merge with High if α is small).
4. Extracting Rules from Decision Trees
Convert trees to IF-THEN rules for interpretability:
- Start at the root, traverse to a leaf.
- Write conditions for each split (e.g.,
Age ≤ 30). - Assign the leaf’s class as the conclusion.
From the Tree Above:
- Rule 1:
IF Age ≤ 30 THEN Class = High. - Rule 2:
IF Age > 30 AND Car Type = Family THEN Class = Low. - Rule 3:
IF Age > 30 AND Car Type = Sports THEN Class = High.
5. Handling Missing Values and Laplace Smoothing
ID3 assumes no missing values. For real data:
- Preprocessing: Replace missing values with mode (most frequent) or mean.
- Laplace Smoothing: Adjusts probabilities to avoid zero counts.
- Formula: .
- Example: If
Class=Highappears 3 times forAge ≤ 30and there are 2 classes: .
6. Real-World Applications
In the Real World
NMB Bank (Nepal):
- Use: Decision trees classify loan applications.
- How: Splits on
Credit Score,Income, andLoan Amountto predictApproved/Rejected. - Rule Example:
IF Credit Score > 700 AND Income > 50,000 THEN Approve Loan.
eSewa (Nepal):
- Use: Predicts user churn (whether a user will stop using the app).
- How: Trees split on
Last Login Date,Transaction Frequency, andDevice Type. - Rule Example:
IF Last Login > 30 Days AND Transactions < 3 THEN Flag for Retention Campaign.
Google Play Store (Global):
- Use: Recommends apps based on user history.
- How: Decision trees split on
User Age,Download Frequency, andApp Categoryto suggest new apps. - Rule Example:
IF Age 18-25 AND Downloads > 10 Apps/Month THEN Recommend Gaming Apps.
Worked Example: Daraz Order Fulfillment
Daraz uses decision trees to route orders to warehouses based on:
- Attributes:
Order Value,Customer Location,Product Type. - Class:
Warehouse ID(e.g.,Kathmandu,Lalitpur).
Dataset:
| Order ID | Value (₹) | Location | Product Type | Warehouse |
|---|---|---|---|---|
| 1 | ≤5000 | Kathmandu | Electronics | Kathmandu |
| 2 | >5000 | Lalitpur | Clothing | Kathmandu |
| 3 | ≤5000 | Bhaktapur | Electronics | Lalitpur |
Tree Construction:
- Split on
Location(highest information gain):- Kathmandu → Split on
Value:- ≤5000 →
Kathmandu. 5000 →
Kathmandu(only one sample).
- ≤5000 →
- Bhaktapur →
Lalitpur.
- Kathmandu → Split on
Final Rule:
IF Location = Kathmandu AND Value ≤ 5000 THEN Route to Kathmandu Warehouse.
7. Comparing Decision Trees to Other Methods
| Method | Pros | Cons | Best For |
|---|---|---|---|
| Decision Trees | Interpretable, no need for scaling. | Prone to overfitting, unstable to data. | Small-to-medium datasets. |
| Random Forest | Reduces overfitting, handles noise. | Less interpretable, slower training. | Large datasets with noise. |
| Naive Bayes | Fast, works well with text. | Assumes feature independence. | Spam detection, sentiment analysis. |
| Neural Networks | High accuracy for complex patterns. | Black-box, needs tuning. | Image/audio data. |
8. Exam Tip: How to Score Full Marks
Define Key Terms Precisely:
- Overfitting: "A model fits training data too closely, performing poorly on unseen data."
- Information Gain: "Reduction in entropy after splitting on an attribute."
Show Calculations Step-by-Step:
- For ID3, always:
- Calculate entropy of the target.
- Compute information gain for each attribute.
- Select the attribute with highest gain.
- Example: In the loan dataset, show entropy before/after splits for both
AgeandCar Type.
- For ID3, always:
Draw the Tree:
- Use Mermaid or ASCII art to visualize splits. Label nodes with attributes and leaves with classes.
- Example:
[Age ≤ 30?] / \ [High] [Car Type?] / \ [Family: Low] [Sports: High]
Explain Pruning:
- Mention pre-pruning (early stopping) and post-pruning (reduced-error pruning).
- Link to overfitting: "Pruning reduces variance by simplifying the tree."
Relate to Real-World Scenarios:
- For questions on applications, tie to banks (loan approval), e-commerce (order routing), or telecom (churn prediction).
- Example Answer:
"Ncell uses decision trees to predict customer churn by splitting on
Call Duration,Data Usage, andPayment History. A rule likeIF Call Duration < 30 mins/month THEN Classify as Churn Riskhelps target retention offers."
Avoid Common Mistakes:
- ❌ Using Gini impurity instead of entropy for ID3.
- ❌ Forgetting to normalize information gain by split size.
- ❌ Ignoring missing values in the dataset.
9. Practice Questions (Exam-Style)
Train an ID3 classifier on the following data and draw the tree:
TID Outlook Temperature Humidity Windy Play Tennis 1 Sunny Hot High Weak No 2 Sunny Hot High Strong No 3 Overcast Hot High Weak Yes Explain how Laplace smoothing improves rule accuracy in decision trees. Provide a numerical example.
How would you detect overfitting in a decision tree? Describe two methods and their tradeoffs.
10. Summary Checklist
Before the exam, ensure you can:
- Calculate entropy and information gain for any dataset.
- Build a decision tree from scratch using ID3.
- Identify overfitting/underfitting in a tree.
- Apply pruning techniques (pre/post).
- Extract rules from a decision tree.
- Explain real-world applications (banks, e-commerce, telecom).
Based on the TU BSc CSIT syllabus for Data Warehousing and Data Mining (CSC410), unit 7.
Discussion
Loading…