Decision Tree ID3: A Complete Solved Numerical Example

Scenario: Bank Loan Approval

The Objective: Predict whether a loan application should be approved based on applicant profile.

Step 1: The Training Data

Before calculating recursive splits, we must define the dataset. The ID3 algorithm evaluates each feature to predict the target class (Approved).

Data PointCredit_HistoryEmploymentIncome_LevelOwns_PropertyApproved
P1GoodStableHighYesYes
P2GoodStableMediumNoYes
P3PoorUnstableLowNoNo
P4PoorStableMediumYesNo
P5GoodUnstableHighYesYes
P6PoorUnstableHighNoNo
P7GoodStableLowNoYes
P8PoorStableLowYesNo
P9GoodUnstableLowNoNo
TargetGoodUnstableMediumNo?

Step 2: Recursive Splitting (Entropy & Gain)

To build the tree, we recursively calculate the Entropy of the system and find the Information Gain for every available feature. The feature with the highest gain becomes the splitting node.

Iteration 1

Context: Root

1. Entropy of Target Class, Entropy(S)
Formula: Entropy(S)=PP+Nlog2(PP+N)NP+Nlog2(NP+N)\text{Entropy}(S) = - \dfrac{P}{P+N} \log_2\left(\dfrac{P}{P+N}\right) - \dfrac{N}{P+N} \log_2\left(\dfrac{N}{P+N}\right)

Positives (P) for 'Yes' = 4

Negatives (N) for 'No' = 5

Entropy(S)=0.9911\text{Entropy}(S) = 0.9911

2. Subset Information Required
Formula: Entropy(Pi,Ni)=PiPi+Nilog2(PiPi+Ni)NiPi+Nilog2(NiPi+Ni)\text{Entropy}(P_i, N_i) = - \dfrac{P_i}{P_i+N_i} \log_2\left(\dfrac{P_i}{P_i+N_i}\right) - \dfrac{N_i}{P_i+N_i} \log_2\left(\dfrac{N_i}{P_i+N_i}\right)
Evaluating Feature: Credit_History
ValuePiNiI (Pi, Ni)
Good410.7219
Poor040.0000
Evaluating Feature: Employment
ValuePiNiI (Pi, Ni)
Stable320.9710
Unstable130.8113
Evaluating Feature: Income_Level
ValuePiNiI (Pi, Ni)
High210.9183
Medium111.0000
Low130.8113
Evaluating Feature: Owns_Property
ValuePiNiI (Pi, Ni)
Yes221.0000
No230.9710
3. Weighted Feature Entropy
Formula: Entropy(A)=[pi+niP+N]×Entropy(Pi,Ni)\text{Entropy}(A) = \sum \left[ \dfrac{p_i + n_i}{P + N} \right] \times \text{Entropy}(P_i, N_i)
Credit_HistoryEntropy = 0.4011
EmploymentEntropy = 0.9000
Income_LevelEntropy = 0.8889
Owns_PropertyEntropy = 0.9839
4. Feature Information Gain
Formula: Gain(S,A)=Entropy(S)Entropy(A)\text{Gain}(S, A) = \text{Entropy}(S) - \text{Entropy}(A)
Credit_History0.9911 - 0.4011 =Gain:0.5900
Employment0.9911 - 0.9000 =Gain:0.0911
Income_Level0.9911 - 0.8889 =Gain:0.1022
Owns_Property0.9911 - 0.9839 =Gain:0.0072
5. Feature Selection Decision

Credit_History generated the highest Information Gain (0.5900). It is selected as the optimal splitting node for this subset.

Resulting Split
Credit_History ?
Good
Class: ?
Poor
Class: No

Iteration 2

Context: Credit_History = Good

Current Data: Filtered by Credit_History = Good5 Rows
Data PointCredit_HistoryEmploymentIncome_LevelOwns_PropertyApproved
P1GoodStableHighYesYes
P2GoodStableMediumNoYes
P5GoodUnstableHighYesYes
P7GoodStableLowNoYes
P9GoodUnstableLowNoNo
1. Entropy of Target Class, Entropy(S)
Formula: Entropy(S)=PP+Nlog2(PP+N)NP+Nlog2(NP+N)\text{Entropy}(S) = - \dfrac{P}{P+N} \log_2\left(\dfrac{P}{P+N}\right) - \dfrac{N}{P+N} \log_2\left(\dfrac{N}{P+N}\right)

Positives (P) for 'Yes' = 4

Negatives (N) for 'No' = 1

Entropy(S)=0.7219\text{Entropy}(S) = 0.7219

2. Subset Information Required
Formula: Entropy(Pi,Ni)=PiPi+Nilog2(PiPi+Ni)NiPi+Nilog2(NiPi+Ni)\text{Entropy}(P_i, N_i) = - \dfrac{P_i}{P_i+N_i} \log_2\left(\dfrac{P_i}{P_i+N_i}\right) - \dfrac{N_i}{P_i+N_i} \log_2\left(\dfrac{N_i}{P_i+N_i}\right)
Evaluating Feature: Employment
ValuePiNiI (Pi, Ni)
Stable300.0000
Unstable111.0000
Evaluating Feature: Income_Level
ValuePiNiI (Pi, Ni)
High200.0000
Medium100.0000
Low111.0000
Evaluating Feature: Owns_Property
ValuePiNiI (Pi, Ni)
Yes200.0000
No210.9183
3. Weighted Feature Entropy
Formula: Entropy(A)=[pi+niP+N]×Entropy(Pi,Ni)\text{Entropy}(A) = \sum \left[ \dfrac{p_i + n_i}{P + N} \right] \times \text{Entropy}(P_i, N_i)
EmploymentEntropy = 0.4000
Income_LevelEntropy = 0.4000
Owns_PropertyEntropy = 0.5510
4. Feature Information Gain
Formula: Gain(S,A)=Entropy(S)Entropy(A)\text{Gain}(S, A) = \text{Entropy}(S) - \text{Entropy}(A)
Employment0.7219 - 0.4000 =Gain:0.3219
Income_Level0.7219 - 0.4000 =Gain:0.3219
Owns_Property0.7219 - 0.5510 =Gain:0.1710
5. Feature Selection Decision

Employment generated the highest Information Gain (0.3219). It is selected as the optimal splitting node for this subset.

Resulting Split
Employment ?
Stable
Class: Yes
Unstable
Class: ?

Iteration 3

Context: Employment = Unstable

Current Data: Filtered by Employment = Unstable2 Rows
Data PointCredit_HistoryEmploymentIncome_LevelOwns_PropertyApproved
P5GoodUnstableHighYesYes
P9GoodUnstableLowNoNo
1. Entropy of Target Class, Entropy(S)
Formula: Entropy(S)=PP+Nlog2(PP+N)NP+Nlog2(NP+N)\text{Entropy}(S) = - \dfrac{P}{P+N} \log_2\left(\dfrac{P}{P+N}\right) - \dfrac{N}{P+N} \log_2\left(\dfrac{N}{P+N}\right)

Positives (P) for 'Yes' = 1

Negatives (N) for 'No' = 1

Entropy(S)=1.0000\text{Entropy}(S) = 1.0000

2. Subset Information Required
Formula: Entropy(Pi,Ni)=PiPi+Nilog2(PiPi+Ni)NiPi+Nilog2(NiPi+Ni)\text{Entropy}(P_i, N_i) = - \dfrac{P_i}{P_i+N_i} \log_2\left(\dfrac{P_i}{P_i+N_i}\right) - \dfrac{N_i}{P_i+N_i} \log_2\left(\dfrac{N_i}{P_i+N_i}\right)
Evaluating Feature: Income_Level
ValuePiNiI (Pi, Ni)
High100.0000
Low010.0000
Evaluating Feature: Owns_Property
ValuePiNiI (Pi, Ni)
Yes100.0000
No010.0000
3. Weighted Feature Entropy
Formula: Entropy(A)=[pi+niP+N]×Entropy(Pi,Ni)\text{Entropy}(A) = \sum \left[ \dfrac{p_i + n_i}{P + N} \right] \times \text{Entropy}(P_i, N_i)
Income_LevelEntropy = 0.0000
Owns_PropertyEntropy = 0.0000
4. Feature Information Gain
Formula: Gain(S,A)=Entropy(S)Entropy(A)\text{Gain}(S, A) = \text{Entropy}(S) - \text{Entropy}(A)
Income_Level1.0000 - 0.0000 =Gain:1.0000
Owns_Property1.0000 - 0.0000 =Gain:1.0000
5. Feature Selection Decision

Income_Level generated the highest Information Gain (1.0000). It is selected as the optimal splitting node for this subset.

Resulting Split
Income_Level ?
High
Class: Yes
Low
Class: No

Step 3: Final Computed Decision Tree

Combining all the recursive splits from Step 2 yields the final classification tree.

Credit_History ?
Good
Employment ?
Stable
Class: Yes
Unstable
Income_Level ?
High
Class: Yes
Low
Class: No
Poor
Class: No

Final Takeaway

This tree mathematically proves which variables have the highest predictive power for determining loan approvals based on historical data.