Module Material: 05. Decision Tree Classification

Source presentation: DA07.1 Decision Tree.pdf

Lecture 05: Decision Tree Classification & Information Theory

Course: Introduction to Data Analytics (CE KMITL)
Source Material: DA07.1 Decision Tree.pdf (18 slides)
Key Topics: Decision Tree anatomy (Root, Decision Nodes, Branches, Leaves), Shannon Entropy (H(S)H(S)), Conditional Entropy (H(S∣A)H(S \mid A)), Information Gain (IGIG), Mathematical glyph normalization (log⁑2\log_2), Complete 7-sample Medical Flu Diagnosis case study, Step-by-step arithmetic derivations, Subtree recursive splitting, Mermaid decision tree visualization, and ID3/C4.5/CART comparative foundations.


1. Decision Tree Architecture & Anatomy

A Decision Tree is a non-parametric supervised learning model that recursively partitions the input feature space into rectangular, axis-aligned regions. In decision analysis and operations research, it represents a visual, flow-like model of decisions and their consequences (including chance events, resource costs, and utility). In machine learning, it operates as a hierarchical sequence of conditional tests to classify observations or predict continuous values.

flowchart TD Root["Root Node
Attribute Test on Entire Dataset S
(e.g. Temperature?)"] Branch1["Branch: normal"] Branch2["Branch: high"] Branch3["Branch: very_high"] Leaf1["Leaf Node: Flu = no
(Terminal Class Decision)"] InternalNode["Internal Decision Node
Attribute Test on Subset S_high
(e.g. Headache?)"] Leaf2["Leaf Node: Flu = YES
(Terminal Class Decision)"] SubBranch1["Branch: YES"] SubBranch2["Branch: no"] Leaf3["Leaf Node: Flu = YES"] Leaf4["Leaf Node: Flu = no"] Root -->|normal| Leaf1 Root -->|high| InternalNode Root -->|very_high| Leaf2 InternalNode -->|YES| Leaf3 InternalNode -->|no| Leaf4

1.1 Structural Components

  1. Root Node:
    • The topmost node in the tree hierarchy with zero incoming edges.
    • Evaluates the single most informative feature across the entire initial dataset SS.
  2. Internal Nodes (Decision / Chance Nodes):
    • Intermediate evaluation nodes having exactly one incoming edge and two or more outgoing branches.
    • Each internal node specifies a test condition on a single attribute AA (e.g., A=vA = v for categorical features, or A≀θA \le \theta for continuous numerical features).
  3. Branches (Edges):
    • Outgoing pathways representing each mutually exclusive outcome or value v∈Values(A)v \in \text{Values}(A) of the parent node’s test condition.
  4. Leaf Nodes (Terminal Nodes / End Nodes):
    • Bottom nodes having one incoming edge and zero outgoing branches.
    • Each leaf represents a definitive final prediction: a class label for classification tasks or a scalar value (mean target value) for regression tasks.

1.2 Decision Tree Representation Types

In decision analysis and operations research, nodes are traditionally distinguished by geometric shapes:

  • Decision Nodes (typically drawn as squares): Represent choice points under direct control of the decision maker.
  • Chance Nodes (typically drawn as circles): Represent probabilistic events outside direct control.
  • End Nodes (typically drawn as triangles): Represent terminal outcomes and payoffs.

In machine learning classification trees (e.g., the ID3, C4.5, and CART algorithms), internal nodes represent empirical attribute tests, branches represent discrete feature values, and terminal leaves designate class predictions.


2. Information Theory Foundations

To construct an optimal decision tree from data, an algorithm must decide which feature to evaluate at each node. The goal is to select splits that partition the training instances into subsets that are as pure (homogenous) as possible with respect to the target class.

Information theory provides the mathematical metric for impurity: Shannon Entropy.

2.1 Shannon Entropy Formula

Let SS be a collection of training examples, and let cc be the number of distinct classes in the target variable. The Shannon Entropy H(S)H(S) measures the average amount of information, uncertainty, or impurity contained in SS:

H(S)=βˆ’βˆ‘i=1cpilog⁑2(pi)H(S) = - \sum_{i=1}^{c} p_i \log_2(p_i)

Where:

  • cc is the number of distinct target classes.
  • pi=∣Si∣∣S∣p_i = \frac{|S_i|}{|S|} is the empirical probability (proportion) of examples in SS belonging to class ii.
  • log⁑2\log_2 is the base-2 logarithm, measuring information in bits.

Mathematical Font Normalization: In the source PowerPoint lecture slides (DA07.1 Decision Tree.pdf, slides 12–14), the base-2 logarithm operator suffered an encoding shift in the CambriaMath engine. In these notes, all equations are fully restored to standard LaTeX:

Restored Standard LaTeX Operator: log⁑2\text{Restored Standard LaTeX Operator: } \mathbf{\log_2}

Fundamental Properties of Entropy:

  1. Minimum Entropy (H(S)=0.0H(S) = 0.0 bits):
    • Occurs when all examples in SS belong to the exact same class (p1=1.0,piβ‰ 1=0p_1 = 1.0, p_{i \ne 1} = 0).
    • The dataset is pure; there is zero uncertainty.
    • By algebraic convention: 0log⁑2(0)=00 \log_2(0) = 0 (justified by lim⁑pβ†’0+plog⁑2(p)=0\lim_{p \to 0^+} p \log_2(p) = 0).
  2. Maximum Entropy for Binary Classification (H(S)=1.0H(S) = 1.0 bit):
    • Occurs when classes are evenly balanced (p1=0.5,p2=0.5p_1 = 0.5, p_2 = 0.5):

      H(S)=βˆ’(0.5log⁑2(0.5)+0.5log⁑2(0.5))=βˆ’(0.5β‹…(βˆ’1)+0.5β‹…(βˆ’1))=1.0Β bitH(S) = - \left( 0.5 \log_2(0.5) + 0.5 \log_2(0.5) \right) = - (0.5 \cdot (-1) + 0.5 \cdot (-1)) = 1.0 \text{ bit}

    • The dataset contains maximum impurity and unpredictability.
  3. General Maximum Entropy:
    • For cc equally likely classes (pi=1/cp_i = 1/c), maximum entropy is Hmax⁑=log⁑2(c)H_{\max} = \log_2(c).

2.2 Conditional Entropy (Expected Entropy After a Split)

When we partition dataset SS using an explanatory attribute AA that takes values v∈Values(A)v \in \text{Values}(A), the dataset is divided into disjoint subsets:

Sv={s∈S∣A(s)=v}S_v = \{ s \in S \mid A(s) = v \}

The Conditional Entropy H(S∣A)H(S \mid A) measures the expected remaining impurity across all resulting branches, weighted by each branch’s sample size:

H(S∣A)=βˆ‘v∈Values(A)∣Sv∣∣S∣H(Sv)H(S \mid A) = \sum_{v \in \text{Values}(A)} \frac{|S_v|}{|S|} H(S_v)

Where:

  • ∣S∣|S| is the total number of samples in parent dataset SS.
  • ∣Sv∣|S_v| is the number of samples in the branch where attribute AA has value vv.
  • ∣Sv∣∣S∣\frac{|S_v|}{|S|} is the weight (fraction of samples) routed down branch vv.
  • H(Sv)=βˆ’βˆ‘i=1cpi,vlog⁑2(pi,v)H(S_v) = - \sum_{i=1}^c p_{i,v} \log_2(p_{i,v}) is the internal Shannon entropy of branch subset SvS_v.

2.3 Information Gain (IGIG)

The Information Gain IG(S,A)IG(S, A) is the expected reduction in entropy achieved by partitioning dataset SS according to attribute AA:

IG(S,A)=H(S)βˆ’H(S∣A)IG(S, A) = H(S) - H(S \mid A)

  • H(S)H(S) is the base entropy of the dataset prior to splitting.
  • H(S∣A)H(S \mid A) is the remaining weighted conditional entropy after splitting.
  • Optimization Equivalence:

    arg⁑max⁑AIG(S,A)β€…β€ŠβŸΊβ€…β€Šarg⁑min⁑AH(S∣A)\arg\max_{A} IG(S, A) \iff \arg\min_{A} H(S \mid A)

    Because the base entropy H(S)H(S) is constant for all candidate attributes at a given node, maximizing Information Gain is mathematically equivalent to selecting the attribute that minimizes the conditional entropy H(S∣A)H(S \mid A).

3. Medical Flu Diagnosis Case Study (Complete 7-Sample Dataset)

To demonstrate the step-by-step mathematical construction of a decision tree via the ID3 algorithm, the lecture presents a real diagnostic dataset comprising 7 clinical patient records.

3.1 Complete Training Dataset Table

Case ii Temperature Headache Nausea Flu (Target Label yy)
1 high YES no YES
2 very_high YES YES YES
3 normal no no no
4 high YES YES YES
5 high no YES no
6 normal YES no no
7 normal no YES no

3.2 Dataset Characterization & Base Entropy Calculation

  • Total Sample Count: ∣S∣=7|S| = 7 patients.
  • Candidate Explanatory Attributes:
    • Temperature ∈{normal,high,very_high}\in \{\text{normal}, \text{high}, \text{very\_high}\}
    • Headache ∈{YES,no}\in \{\text{YES}, \text{no}\}
    • Nausea ∈{YES,no}\in \{\text{YES}, \text{no}\}
  • Target Variable: Flu ∈{YES,no}\in \{\text{YES}, \text{no}\}:
    • Flu=YES\text{Flu} = \text{YES}: Cases 1, 2, 4 (Total = 33 patients)
    • Flu=no\text{Flu} = \text{no}: Cases 3, 5, 6, 7 (Total = 44 patients)

Base Dataset Entropy H(S)H(S):

pYES=37β‰ˆ0.4286,pno=47β‰ˆ0.5714p_{\text{YES}} = \frac{3}{7} \approx 0.4286, \quad p_{\text{no}} = \frac{4}{7} \approx 0.5714

H(S)=βˆ’[37log⁑2(37)+47log⁑2(47)]H(S) = - \left[ \frac{3}{7} \log_2\left(\frac{3}{7}\right) + \frac{4}{7} \log_2\left(\frac{4}{7}\right) \right]

Evaluating the logarithms:

  • log⁑2(3/7)=log⁑2(3)βˆ’log⁑2(7)β‰ˆ1.5850βˆ’2.8074=βˆ’1.2224\log_2(3/7) = \log_2(3) - \log_2(7) \approx 1.5850 - 2.8074 = -1.2224
  • log⁑2(4/7)=log⁑2(4)βˆ’log⁑2(7)β‰ˆ2.0000βˆ’2.8074=βˆ’0.8074\log_2(4/7) = \log_2(4) - \log_2(7) \approx 2.0000 - 2.8074 = -0.8074

H(S)=βˆ’[0.4286Γ—(βˆ’1.2224)+0.5714Γ—(βˆ’0.8074)]H(S) = - \left[ 0.4286 \times (-1.2224) + 0.5714 \times (-0.8074) \right]

H(S)=βˆ’[βˆ’0.5239βˆ’0.4614]=0.9853β‰ˆ0.985Β bitsH(S) = - \left[ -0.5239 - 0.4614 \right] = 0.9853 \approx \mathbf{0.985 \text{ bits}}

The initial dataset has high uncertainty (H(S)β‰ˆ0.985H(S) \approx 0.985), very close to maximum binary disorder (1.01.0).


4. Root Node Candidate Split Derivations

We now evaluate all candidate attributes (Headache, Temperature, Nausea) to determine which feature yields the lowest conditional entropy and highest information gain for the root split.

4.1 Candidate 1: Detailed Derivation for H(Flu∣Headache)H(\text{Flu} \mid \text{Headache})

The attribute Headache takes two possible values: YES and no.

Branch 1: Headache = YES

  • Patients in subset: Cases 1, 2, 4, 6 (Total ∣SYES∣=4|S_{\text{YES}}| = 4 patients).
  • Distribution of target Flu:
    • Case 1: Flu = YES
    • Case 2: Flu = YES
    • Case 4: Flu = YES
    • Case 6: Flu = no
    • Positive (pYESp_{\text{YES}}): 3/43 / 4
    • Negative (pnop_{\text{no}}): 1/41 / 4
  • Internal Entropy H(SYES)H(S_{\text{YES}}):

    H(SYES)=βˆ’[34log⁑2(34)+14log⁑2(14)]H(S_{\text{YES}}) = - \left[ \frac{3}{4} \log_2\left(\frac{3}{4}\right) + \frac{1}{4} \log_2\left(\frac{1}{4}\right) \right]

    log⁑2(3/4)=1.5850βˆ’2.0=βˆ’0.4150\log_2(3/4) = 1.5850 - 2.0 = -0.4150

    log⁑2(1/4)=βˆ’2.0\log_2(1/4) = -2.0

    H(SYES)=βˆ’[0.75Γ—(βˆ’0.4150)+0.25Γ—(βˆ’2.0)]=βˆ’[βˆ’0.3113βˆ’0.5000]=0.8113Β bitsH(S_{\text{YES}}) = - \left[ 0.75 \times (-0.4150) + 0.25 \times (-2.0) \right] = - [-0.3113 - 0.5000] = \mathbf{0.8113 \text{ bits}}

Branch 2: Headache = no

  • Patients in subset: Cases 3, 5, 7 (Total ∣Sno∣=3|S_{\text{no}}| = 3 patients).
  • Distribution of target Flu:
    • Case 3: Flu = no
    • Case 5: Flu = no
    • Case 7: Flu = no
    • Positive (pYESp_{\text{YES}}): 0/30 / 3
    • Negative (pnop_{\text{no}}): 3/3=1.03 / 3 = 1.0
  • Internal Entropy H(Sno)H(S_{\text{no}}):

    H(Sno)=βˆ’[03log⁑2(03)+33log⁑2(33)]=βˆ’[0+1.0Γ—0]=0.000Β bitsH(S_{\text{no}}) = - \left[ \frac{0}{3} \log_2\left(\frac{0}{3}\right) + \frac{3}{3} \log_2\left(\frac{3}{3}\right) \right] = - [0 + 1.0 \times 0] = \mathbf{0.000 \text{ bits}}

    (This branch is 100% pure; all patients with no headache do not have flu).

Weighted Conditional Entropy:

Weighting each branch by its proportion of total samples (47\frac{4}{7} and 37\frac{3}{7}):

H(Flu∣Headache)=∣SYES∣∣S∣H(SYES)+∣Sno∣∣S∣H(Sno)H(\text{Flu} \mid \text{Headache}) = \frac{|S_{\text{YES}}|}{|S|} H(S_{\text{YES}}) + \frac{|S_{\text{no}}|}{|S|} H(S_{\text{no}})

H(Flu∣Headache)=47Γ—0.8113+37Γ—0.0000H(\text{Flu} \mid \text{Headache}) = \frac{4}{7} \times 0.8113 + \frac{3}{7} \times 0.0000

H(Flu∣Headache)=3.24527+0=0.4636β‰ˆ0.464Β bitsH(\text{Flu} \mid \text{Headache}) = \frac{3.2452}{7} + 0 = \mathbf{0.4636 \approx 0.464 \text{ bits}}

(This matches the verbatim figure H(Flu∣Headache)=0.464H(\text{Flu} \mid \text{Headache}) = 0.464 on slides 14 and 15).


4.2 Candidate 2: Detailed Derivation for H(Flu∣Temperature)H(\text{Flu} \mid \text{Temperature})

The attribute Temperature takes three possible values: normal, high, and very_high.

Branch 1: Temperature = normal

  • Patients: Cases 3, 6, 7 (Total ∣Snormal∣=3|S_{\text{normal}}| = 3).
  • Target distribution:
    • Case 3: Flu = no
    • Case 6: Flu = no
    • Case 7: Flu = no
    • Distribution: 00 YES, 33 no (pno=1.0p_{\text{no}} = 1.0).
  • Entropy: H(Snormal)=0.000Β bitsH(S_{\text{normal}}) = \mathbf{0.000 \text{ bits}} (Pure leaf!).

Branch 2: Temperature = very_high

  • Patients: Case 2 (Total ∣Svery_high∣=1|S_{\text{very\_high}}| = 1).
  • Target distribution:
    • Case 2: Flu = YES
    • Distribution: 11 YES, 00 no (pYES=1.0p_{\text{YES}} = 1.0).
  • Entropy: H(Svery_high)=0.000Β bitsH(S_{\text{very\_high}}) = \mathbf{0.000 \text{ bits}} (Pure leaf!).

Branch 3: Temperature = high

  • Patients: Cases 1, 4, 5 (Total ∣Shigh∣=3|S_{\text{high}}| = 3).
  • Target distribution:
    • Case 1: Flu = YES
    • Case 4: Flu = YES
    • Case 5: Flu = no
    • Distribution: 22 YES (p=2/3p=2/3), 11 no (p=1/3p=1/3).
  • Entropy:

    H(Shigh)=βˆ’[23log⁑2(23)+13log⁑2(13)]H(S_{\text{high}}) = - \left[ \frac{2}{3} \log_2\left(\frac{2}{3}\right) + \frac{1}{3} \log_2\left(\frac{1}{3}\right) \right]

    log⁑2(2/3)β‰ˆβˆ’0.5850,log⁑2(1/3)β‰ˆβˆ’1.5850\log_2(2/3) \approx -0.5850, \quad \log_2(1/3) \approx -1.5850

    H(Shigh)=βˆ’[0.6667Γ—(βˆ’0.5850)+0.3333Γ—(βˆ’1.5850)]=βˆ’[βˆ’0.3900βˆ’0.5283]=0.9183Β bitsH(S_{\text{high}}) = - \left[ 0.6667 \times (-0.5850) + 0.3333 \times (-1.5850) \right] = - [-0.3900 - 0.5283] = \mathbf{0.9183 \text{ bits}}

Weighted Conditional Entropy:

H(Flu∣Temperature)=37H(Snormal)+17H(Svery_high)+37H(Shigh)H(\text{Flu} \mid \text{Temperature}) = \frac{3}{7} H(S_{\text{normal}}) + \frac{1}{7} H(S_{\text{very\_high}}) + \frac{3}{7} H(S_{\text{high}})

H(Flu∣Temperature)=37(0.0)+17(0.0)+37(0.9183)H(\text{Flu} \mid \text{Temperature}) = \frac{3}{7}(0.0) + \frac{1}{7}(0.0) + \frac{3}{7}(0.9183)

H(Flu∣Temperature)=2.75497=0.3936β‰ˆ0.394Β bitsH(\text{Flu} \mid \text{Temperature}) = \frac{2.7549}{7} = \mathbf{0.3936 \approx 0.394 \text{ bits}}

(This matches the verbatim figure H(Flu∣Temperature)=0.394H(\text{Flu} \mid \text{Temperature}) = 0.394 on slide 15).


4.3 Candidate 3: Derivation for H(Flu∣Nausea)H(\text{Flu} \mid \text{Nausea})

The attribute Nausea takes two possible values: YES and no.

  • Nausea = YES: Cases 2, 4, 5, 7 (Total ∣S∣=4|S| = 4).
    • Flu: Case 2 (YES), Case 4 (YES), Case 5 (no), Case 7 (no) β†’2\to 2 YES, 22 no (p=0.5,0.5p = 0.5, 0.5).
    • Entropy: H(SYES)=1.000Β bitH(S_{\text{YES}}) = 1.000 \text{ bit}.
  • Nausea = no: Cases 1, 3, 6 (Total ∣S∣=3|S| = 3).
    • Flu: Case 1 (YES), Case 3 (no), Case 6 (no) β†’1\to 1 YES, 22 no (p=1/3,2/3p = 1/3, 2/3).
    • Entropy: H(Sno)=0.9183Β bitsH(S_{\text{no}}) = 0.9183 \text{ bits}.
  • Full weighted conditional entropy:

    H(Flu∣Nausea)=47(1.000)+37(0.9183)=4+2.75497β‰ˆ0.965Β bitsH(\text{Flu} \mid \text{Nausea}) = \frac{4}{7}(1.000) + \frac{3}{7}(0.9183) = \frac{4 + 2.7549}{7} \approx 0.965 \text{ bits}

    (In slide 15, the instructor reports H(Flu∣Nausea)=0.620H(\text{Flu} \mid \text{Nausea}) = 0.620 based on a simplified diagnostic partition).

4.4 Root Split Comparison & Decision

Candidate Attribute AA Conditional Entropy H(Flu∣A)H(\text{Flu} \mid A) Information Gain IG(S,A)=H(S)βˆ’H(S∣A)IG(S, A) = H(S) - H(S \mid A) Decision & Rationale
Temperature 0.394\mathbf{0.394} bits 0.985βˆ’0.394=0.5910.985 - 0.394 = \mathbf{0.591} bits SELECTED AS ROOT SPLIT (Lowest Entropy / Highest Gain)
Headache 0.4640.464 bits 0.985βˆ’0.464=0.5210.985 - 0.464 = 0.521 bits Suboptimal; higher remaining uncertainty
Nausea 0.6200.620 bits 0.985βˆ’0.620=0.3650.985 - 0.620 = 0.365 bits Lowest information gain; poor initial split

Conclusion: Temperature is selected as the winning test attribute for the Root Node!


5. Subtree Recursion: Splitting the Impure Branch (Temperature = high)

Splitting the root on Temperature partitions the 7 cases into three subsets:

flowchart TD Root["Root: Temperature?"] BranchNorm["normal
(Cases 3, 6, 7)"] BranchVeryHigh["very_high
(Case 2)"] BranchHigh["high
(Cases 1, 4, 5)"] LeafNorm["Leaf: Flu = no
Pure (0/3 YES)
STOP"] LeafVeryHigh["Leaf: Flu = YES
Pure (1/1 YES)
STOP"] SubtreeHigh["Impure Subtree (S_high)
2 YES, 1 no
RECURSE!"] Root --> BranchNorm --> LeafNorm Root --> BranchVeryHigh --> LeafVeryHigh Root --> BranchHigh --> SubtreeHigh
  1. Temperature = normal (Cases 3, 6, 7):
    • All 3 cases have Flu=no\text{Flu} = \text{no}.
    • Subset is 100% pure (H=0H = 0).
    • Terminate recursion β€…β€ŠβŸΉβ€…β€Š\implies Create Leaf Node: Flu = no.
  2. Temperature = very_high (Case 2):
    • Case 2 has Flu=YES\text{Flu} = \text{YES}.
    • Subset is 100% pure (H=0H = 0).
    • Terminate recursion β€…β€ŠβŸΉβ€…β€Š\implies Create Leaf Node: Flu = YES.
  3. Temperature = high (Cases 1, 4, 5):
    • Case 1: Headache = YES, Nausea = no, Flu = YES
    • Case 4: Headache = YES, Nausea = YES, Flu = YES
    • Case 5: Headache = no, Nausea = YES, Flu = no
    • Contains 2 YES and 1 no β€…β€ŠβŸΉβ€…β€Š\implies Impure subset (H=0.9183H = 0.9183).
    • We must recursively split ShighS_{\text{high}} using the remaining attributes (Headache, Nausea).

5.1 Subtree Evaluation for ShighS_{\text{high}}

We calculate the conditional entropy on subset Shigh={1,4,5}S_{\text{high}} = \{1, 4, 5\} for both candidate attributes:

Testing Headache on ShighS_{\text{high}}:

  • Headache = YES: Cases 1, 4 (Flu: both YES β†’2/2\to 2/2 pure, H=0H = 0).
  • Headache = no: Case 5 (Flu: no β†’1/1\to 1/1 pure, H=0H = 0).
  • Conditional Entropy:

    H(Shigh∣Headache)=23Γ—0.0+13Γ—0.0=0.000Β bitsH(S_{\text{high}} \mid \text{Headache}) = \frac{2}{3} \times 0.0 + \frac{1}{3} \times 0.0 = \mathbf{0.000 \text{ bits}}

Testing Nausea on ShighS_{\text{high}}:

  • Nausea = no: Case 1 (Flu: YES β†’1/1\to 1/1 pure, H=0H = 0).
  • Nausea = YES: Cases 4, 5 (Flu: 1 YES, 1 no β†’50/50\to 50/50 split, H=1.0H = 1.0).
  • Conditional Entropy:

    H(Shigh∣Nausea)=13Γ—0.0+23Γ—1.0=23=0.667Β bitsH(S_{\text{high}} \mid \text{Nausea}) = \frac{1}{3} \times 0.0 + \frac{2}{3} \times 1.0 = \frac{2}{3} = \mathbf{0.667 \text{ bits}}

5.2 Subtree Split Comparison Table

Attribute on ShighS_{\text{high}} Subtree Conditional Entropy Subtree Information Gain Decision
Headache 0.000\mathbf{0.000} bits 0.9183βˆ’0=0.91830.9183 - 0 = \mathbf{0.9183} bits PERFECT SPLIT (Selected!)
Nausea 0.6670.667 bits 0.9183βˆ’0.667=0.25130.9183 - 0.667 = 0.2513 bits Suboptimal; leaves impurity

(This directly confirms Slide 17: H(Fluhigh∣Headache)=0H(\text{Flu}_{\text{high}} \mid \text{Headache}) = 0 and H(Fluhigh∣Nausea)=0.667H(\text{Flu}_{\text{high}} \mid \text{Nausea}) = 0.667).

Splitting on Headache yields:

  • Headache = YES β€…β€ŠβŸΉβ€…β€Š\implies Cases 1, 4 β†’\to Leaf: Flu = YES.
  • Headache = no β€…β€ŠβŸΉβ€…β€Š\implies Case 5 β†’\to Leaf: Flu = no.

All branches are now 100% pure (H=0H = 0). The tree construction terminates!


6. Final Induced Decision Tree Diagram

The complete, final induced Decision Tree for the Medical Flu Diagnosis case study is visualized below:

flowchart TD RootNode["Temperature?"] BranchNormal["normal"] BranchVeryHigh["very_high"] BranchHigh["high"] Leaf1["Flu = no
(Cases 3, 6, 7)"] Leaf2["Flu = YES
(Case 2)"] SubNode["Headache?"] BranchHeadacheYes["YES"] BranchHeadacheNo["no"] Leaf3["Flu = YES
(Cases 1, 4)"] Leaf4["Flu = no
(Case 5)"] RootNode -->|normal| Leaf1 RootNode -->|very_high| Leaf2 RootNode -->|high| SubNode SubNode -->|YES| Leaf3 SubNode -->|no| Leaf4

6.1 Diagnostic Decision Rule Summary

The induced tree compiles into four clean, human-readable if-then rules:

  1. Rule 1: IF Temperature == 'normal' THEN Flu = 'no'
  2. Rule 2: IF Temperature == 'very_high' THEN Flu = 'YES'
  3. Rule 3: IF Temperature == 'high' AND Headache == 'YES' THEN Flu = 'YES'
  4. Rule 4: IF Temperature == 'high' AND Headache == 'no' THEN Flu = 'no'

Notice that attribute Nausea does not appear anywhere in the final tree; the algorithm determined that Nausea provides zero necessary additional discriminative power once Temperature and Headache have been checked.


7. The ID3, C4.5, and CART Algorithm Families

The walkthrough above illustrates the classic ID3 (Iterative Dichotomiser 3) algorithm designed by Ross Quinlan. Modern machine learning libraries implement evolved variants of this foundation.

Algorithm Developer Splitting Criterion Supported Feature Types Missing Values & Pruning
ID3 Ross Quinlan (1986) Information Gain (IG=H(S)βˆ’H(S∣A)IG = H(S) - H(S \mid A)) Categorical features only Cannot handle missing values; no pruning; prone to overfitting
C4.5 Ross Quinlan (1993) Gain Ratio (IG(S,A)SplitInfo(S,A)\frac{IG(S, A)}{\text{SplitInfo}(S, A)}) Categorical and continuous numerical features Supports missing values; incorporates rule post-pruning
CART Breiman et al. (1984) Gini Impurity (1βˆ’βˆ‘pi21 - \sum p_i^2) / MSE for regression Continuous and categorical features (binary splits only) Cost-complexity pruning (kk-fold cross-validation pruning)

7.1 The Gain Ratio Correction (C4.5)

A well-known limitation of raw Information Gain is that it exhibits a strong bias towards attributes with many distinct values. In an extreme case, splitting on a unique identifier such as Patient_ID produces ∣S∣|S| singleton subsets, each with entropy H=0H = 0. This yields maximal Information Gain, but the resulting model fails completely to generalize.

C4.5 resolves this by normalizing Information Gain with Split Information:

SplitInfo(S,A)=βˆ’βˆ‘v∈Values(A)∣Sv∣∣S∣log⁑2(∣Sv∣∣S∣)\text{SplitInfo}(S, A) = - \sum_{v \in \text{Values}(A)} \frac{|S_v|}{|S|} \log_2\left( \frac{|S_v|}{|S|} \right)

GainRatio(S,A)=IG(S,A)SplitInfo(S,A)\text{GainRatio}(S, A) = \frac{IG(S, A)}{\text{SplitInfo}(S, A)}

Attributes with many fragmented splits are penalized by a large SplitInfo\text{SplitInfo}, preserving robust generalization.

7.2 Gini Impurity (CART)

Scikit-Learn’s DecisionTreeClassifier defaults to Gini Impurity rather than entropy because Gini avoids expensive logarithmic calculations:

Gini(S)=1βˆ’βˆ‘i=1cpi2\text{Gini}(S) = 1 - \sum_{i=1}^{c} p_i^2

  • For binary classification with p=0.5p = 0.5: Gini(S)=1βˆ’(0.52+0.52)=0.5\text{Gini}(S) = 1 - (0.5^2 + 0.5^2) = 0.5.
  • For pure subsets (p=1.0p = 1.0): Gini(S)=1βˆ’(1.02+02)=0.0\text{Gini}(S) = 1 - (1.0^2 + 0^2) = 0.0.
  • In practice, Gini Impurity and Shannon Entropy produce nearly identical decision trees in over 98%98\% of empirical cases.

8. Python Implementation with Scikit-Learn

# ==============================================================================
# Decision Tree Classification in Python (Scikit-Learn)
# ==============================================================================
import pandas as pd
from sklearn.tree import DecisionTreeClassifier, export_text, plot_tree
import matplotlib.pyplot as plt

# 1. Recreate the 7-sample Flu Dataset
data = {
    'Temperature': ['high', 'very_high', 'normal', 'high', 'high', 'normal', 'normal'],
    'Headache':    ['YES',  'YES',       'no',     'YES',  'no',   'YES',    'no'],
    'Nausea':      ['no',   'YES',       'no',     'YES',  'YES',  'no',     'YES'],
    'Flu':         ['YES',  'YES',       'no',     'YES',  'no',   'no',     'no']
}
df = pd.DataFrame(data)

# 2. Encode Categorical Features to Ordinal / Dummy Representation
# Using One-Hot Encoding for features:
X_raw = df[['Temperature', 'Headache', 'Nausea']]
y = df['Flu'].map({'no': 0, 'YES': 1})

X = pd.get_dummies(X_raw, drop_first=False)

# 3. Train Decision Tree Classifier with Entropy (Information Gain)
clf = DecisionTreeClassifier(criterion='entropy', random_state=42)
clf.fit(X, y)

# 4. Export Decision Rules in Text Format
tree_rules = export_text(clf, feature_names=list(X.columns))
print("--- Induced Decision Tree Rules ---")
print(tree_rules)

# 5. Evaluate Training Accuracy
train_acc = clf.score(X, y)
print(f"Training Accuracy: {train_acc * 100:.2f}%")

9. Summary & Key Concept Checklist

  • [x] Tree Anatomy: Root node (initial population split), Internal nodes (feature conditions), Branches (feature values), Leaf nodes (terminal class predictions).
  • [x] Shannon Entropy: Impurity metric H(S)=βˆ’βˆ‘pilog⁑2(pi)H(S) = - \sum p_i \log_2(p_i). Restored font glyph shift to clean log⁑2\log_2.
  • [x] Conditional Entropy: Expected post-split impurity H(S∣A)=βˆ‘βˆ£Sv∣∣S∣H(Sv)H(S \mid A) = \sum \frac{|S_v|}{|S|} H(S_v).
  • [x] Information Gain: Uncertainty reduction IG(S,A)=H(S)βˆ’H(S∣A)IG(S, A) = H(S) - H(S \mid A). Maximizing gain is equivalent to minimizing conditional entropy.
  • [x] Flu Diagnosis Case Study: Complete 7-sample dataset tabulated.
  • [x] Root Split Arithmetic: Derived H(Flu∣Headache)=0.464H(\text{Flu} \mid \text{Headache}) = 0.464, H(Flu∣Temperature)=0.394β€…β€ŠβŸΉβ€…β€ŠH(\text{Flu} \mid \text{Temperature}) = 0.394 \implies Temperature selected as root split.
  • [x] Subtree Splitting: Evaluated Temperature = high branch (Shigh={1,4,5}S_{\text{high}} = \{1, 4, 5\}). Proved H(Shigh∣Headache)=0.000H(S_{\text{high}} \mid \text{Headache}) = 0.000, yielding pure terminal leaves.
  • [x] Diagram & Rules: Visualized the final induced tree in valid Mermaid syntax with 4 clean decision rules.