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)



Example:


Pic courtesy: geeksforgeeks

๐Ÿ”น 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

  1. Start with the full dataset
  2. Choose the best feature to split the data
  3. Divide dataset into subsets
  4. Repeat recursively for each subset
  5. 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.

H(S)=i=1cpilog2piH(S) = - \sum_{i=1}^{c} p_i \log_2 p_i

Interpretation:

  • Entropy = 0 → Pure (all same class)
  • High entropy → Mixed classes

Given Dataset

X={a,a,a,b,b,b,b,b}X = \{a, a, a, b, b, b, b, b\}
  • Total instances = 8
  • Instances of aa = 3
  • Instances of bb = 5

Step 1: Compute Probabilities

P(a)=38=0.375P(a) = \frac{3}{8} = 0.375P(b)=58=0.625P(b) = \frac{5}{8} = 0.625

Step 2: Entropy Formula

H(X)=[(38)log2(38)+(58)log2(58)]H(X) = - \left[ \left(\frac{3}{8}\right) \log_2 \left(\frac{3}{8}\right) + \left(\frac{5}{8}\right) \log_2 \left(\frac{5}{8}\right) \right]


Step 3: Substitute Values

H(X)=[0.375log2(0.375)+0.625log2(0.625)]=0.954H(X) = - \left[ 0.375 \cdot \log_2(0.375) + 0.625 \cdot \log_2(0.625) \right]

๐Ÿ“ˆ Information Gain

This tells us how good a split is.

Information Gain=Entropy before splitEntropy after split\text{Information Gain} = \text{Entropy before split} - \text{Entropy after split}
  • 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 SS

Treat the entire dataset as the root node.


2. Compute Entropy of SS

Use entropy formula to measure impurity.


3. For Each Attribute AA

  • Split dataset based on values of AA
  • Compute weighted entropy
Remainder(S,A)=vValues(A)SvSH(Sv)\text{Remainder}(S, A) = \sum_{v \in \text{Values}(A)} \frac{|S_v|}{|S|} \cdot H(S_v)

4. Compute Information Gain

Gain(S,A)=H(S)Remainder(S,A)\text{Gain}(S, A) = H(S) - \text{Remainder}(S, A)

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 H(S)

  • Total = 5
  • Yes = 2
  • No = 3
P(Yes)=25=0.4,P(No)=35=0.6P(Yes)=\frac{2}{5}=0.4,\quad P(No)=\frac{3}{5}=0.6

H(S)=[0.4log2(0.4)+0.6log2(0.6)]H(S) = -[0.4\log_2(0.4) + 0.6\log_2(0.6)]

H(S)0.971H(S) \approx 0.971

๐Ÿ”น Step 2: Try Split on Outlook

Subsets:

๐ŸŒž Sunny → (No, No)

Entropy = 0 (pure)

☁️ Overcast → (Yes)

Entropy = 0 (pure)

๐ŸŒง️ Rain → (Yes, No)

H(Rain)=[0.5log2(0.5)+0.5log2(0.5)]=1H(Rain) = -[0.5\log_2(0.5) + 0.5\log_2(0.5)] = 1

๐Ÿ”น Weighted Entropy (Outlook)

Remainder=25(0)+15(0)+25(1)=0.4\text{Remainder} = \frac{2}{5}(0) + \frac{1}{5}(0) + \frac{2}{5}(1) = 0.4

๐Ÿ”น Information Gain (Outlook)

Gain(Outlook)=0.9710.4=0.571Gain(Outlook) = 0.971 - 0.4 = 0.571

๐Ÿ”น Step 3: Try Split on Wind

Subsets:

Weak → (No, Yes, Yes)

  • Yes = 2, No = 1
H(Weak)=[1/3log2(1/3)+2/3log2(2/3)]H(Weak) = -[1/3\log_2(1/3) + 2/3\log_2(2/3)] 
H(Weak)0.918H(Weak) \approx 0.918

Strong → (No, No)

Entropy = 0


๐Ÿ”น Weighted Entropy (Wind)

Remainder=35(0.918)+25(0)=0.551\text{Remainder} = \frac{3}{5}(0.918) + \frac{2}{5}(0) = 0.551

๐Ÿ”น Information Gain (Wind)

Gain(Wind)=0.9710.551=0.420Gain(Wind) = 0.971 - 0.551 = 0.420

✅ Step 4: Choose Best Attribute

Attribute        Gain
Outlook        0.571 ✅
Wind        0.420

๐Ÿ‘‰ Choose Outlook as root node


๐ŸŒณ Step 5: Split Tree on Outlook

Outlook / | \ Sunny Rain Overcast | | | No ? Yes
  • 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

Outlook / | \ Sunny Rain Overcast | | | No Wind Yes / \ Weak Strong | | Yes No

๐ŸŽฏ 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

AlgorithmKey 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

Gini(S)=1i=1cpi2Gini(S) = 1 - \sum_{i=1}^{c} p_i^2

  • pip_i: Probability of class ii
  • cc: 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:

  • P(Yes)=0.5P(Yes) = 0.5
  • P(No)=0.5P(No) = 0.5
Gini=1(0.52+0.52)=1(0.25+0.25)=0.5Gini = 1 - (0.5^2 + 0.5^2) = 1 - (0.25 + 0.25) = 0.5

⚖️ Gini vs Entropy

Measure    Formula StyleRange    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 usesEntropy + Information Gain
  • Gini Index is used inCART (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

Learn the complete example : visit https://iq.opengenus.org/id3-algorithm/

Comments

Popular posts from this blog

Machine Learning PCCST503 Semester5 KTU CS 2024 Scheme - Dr Binu V P

Introduction to Machine Learning (ML)

Distinguishing Machine Learning from Traditional Programming