Decision Tree in Machine Learning -ID3 Algorithm
๐ณ Decision Tree in Machine Learning
A Decision Tree is a supervised learning algorithm used for classification and regression. It works like a flowchart:
- Each internal node → a test on a feature (e.g., “Is age > 30?”)
- Each branch → outcome of the test
- Each leaf node → final prediction (class label or value)
๐น Simple Intuition
Think of it like asking questions:
“Is it raining?”
→ Yes → “Carry umbrella”
→ No → “No umbrella”
The model keeps splitting data based on questions until it reaches a decision.
๐น Key Terms
- Root Node: Topmost node (entire dataset)
- Splitting: Dividing data into subsets
- Decision Node: A node where a split happens
- Leaf Node: Final output
- Depth: Length of the longest path from root to leaf
๐น How Decision Trees Work
- Start with the full dataset
- Choose the best feature to split the data
- Divide dataset into subsets
- Repeat recursively for each subset
-
Stop when:
- All data in a node belongs to one class, OR
- No features remain, OR
- Stopping condition is met (like max depth)
๐ Entropy (Measure of Impurity)
Decision trees need a way to measure how “mixed” the data is.
Interpretation:
- Entropy = 0 → Pure (all same class)
- High entropy → Mixed classes
Given Dataset
- Total instances = 8
- Instances of = 3
- Instances of = 5
Step 1: Compute Probabilities
Step 2: Entropy Formula
Step 3: Substitute Values
๐ Information Gain
This tells us how good a split is.
- Higher gain → Better split
- Goal → Reduce uncertainty
๐ฒ ID3 Algorithm (Iterative Dichotomiser 3)
The ID3 algorithm is one of the earliest methods to build decision trees. It uses Information Gain to decide splits.
๐น Steps of ID3
1. Start with Dataset
Treat the entire dataset as the root node.
2. Compute Entropy of
Use entropy formula to measure impurity.
3. For Each Attribute
-
Split dataset based on values of
- Compute weighted entropy
4. Compute Information Gain
5. Choose Best Attribute
- Select attribute with maximum Information Gain
- Make it the decision node
6. Split the Dataset
Create branches for each value of the chosen attribute.
7. Repeat Recursively
Apply the same process to each subset.
8. Stopping Conditions
Stop when:
- All examples belong to same class
- No attributes left
- Dataset is empty
๐ Example (Simple)
๐ณ Dataset
We want to predict Play (Yes/No) using two features:
| Outlook | Wind | Play |
|---|---|---|
| Sunny | Weak | No |
| Sunny | Strong | No |
| Overcast | Weak | Yes |
| Rain | Weak | Yes |
| Rain | Strong | No |
๐น Step 1: Compute Parent Entropy
- Total = 5
- Yes = 2
- No = 3
๐น Step 2: Try Split on Outlook
Subsets:
๐ Sunny → (No, No)
Entropy = 0 (pure)
☁️ Overcast → (Yes)
Entropy = 0 (pure)
๐ง️ Rain → (Yes, No)
๐น Weighted Entropy (Outlook)
๐น Information Gain (Outlook)
๐น Step 3: Try Split on Wind
Subsets:
Weak → (No, Yes, Yes)
- Yes = 2, No = 1
Strong → (No, No)
Entropy = 0
๐น Weighted Entropy (Wind)
๐น Information Gain (Wind)
✅ Step 4: Choose Best Attribute
| Attribute | Gain |
|---|---|
| Outlook | 0.571 ✅ |
| Wind | 0.420 |
๐ Choose Outlook as root node
๐ณ Step 5: Split Tree on Outlook
- Sunny → No (pure)
- Overcast → Yes (pure)
- Rain → needs further split
๐น Step 6: Split Rain Subset using Wind
Rain subset:
| Wind | Play |
|---|---|
| Weak | Yes |
| Strong | No |
Perfect split:
- Weak → Yes
- Strong → No
๐ณ Final Decision Tree
๐ฏ Final Rules
- If Outlook = Sunny → No
- If Outlook = Overcast → Yes
- If Outlook = Rain AND Wind = Weak → Yes
- If Outlook = Rain AND Wind = Strong → No
๐ง Key Takeaways
- ID3 always picks highest Information Gain
- First split may not fully classify → recursive splitting
- Tree grows until all nodes are pure
⚖️ Advantages of Decision Trees
- Easy to understand and visualize
- No need for data normalization
- Works with both categorical & numerical data
⚠️ Limitations
- Can overfit easily
- Sensitive to small data changes
- ID3 handles only categorical features
๐ ID3 vs Other Algorithms
| Algorithm | Key Idea |
|---|---|
| ID3 | Uses Information Gain |
| C4.5 | Handles continuous data, pruning |
| CART | Uses Gini Index |
๐ง Key Insight
Decision Trees (and ID3) work by:
๐ Asking the best question at each step to reduce uncertainty as quickly as possible
Appendix
๐ณ What is the Gini Index?
The Gini Index (or Gini Impurity) is a measure of how impure or mixed a dataset is. It is commonly used to decide the best split in decision trees.
๐ Formula
-
: Probability of class
- : Number of classes
๐ Intuition
- Gini = 0 → Perfectly pure (all samples belong to one class)
- Higher Gini → More mixed classes
๐ It tells: “How likely are we to misclassify a random sample?”
๐ Example
If a dataset has:
⚖️ Gini vs Entropy
| Measure | Formula Style | Range | Used In |
|---|---|---|---|
| Entropy | Uses log | 0 to 1 | ID3 |
| Gini Index | No log (faster) | 0 to 0.5 (binary) | CART |
❗ Is Gini Index used in ID3?
๐ No — Gini Index is NOT used in ID3.
- ID3 uses → Entropy + Information Gain
- Gini Index is used in → CART (Classification and Regression Trees)
๐ง Key Difference
- ID3: Chooses split that maximizes Information Gain
- CART: Chooses split that minimizes Gini Index
Big Example:
Compute the Gain and find the best attribute for splitting( root node )
So the root node is outlook




Comments
Post a Comment