Building a Random Forest and Classifying by Majority Vote

Last updated

Problem Setup

Scenario:Predict whether a patient will be readmitted by combining the predictions of three decision trees trained on different bootstrap samples of the training data.

Given:A labeled dataset of 8 patient records with four candidate features — Age_Group, Diagnosis_Severity, Num_Medications, and Has_Support — and Readmitted as the target class. The target patient has Age_Group = Senior, Diagnosis_Severity = Mild, Num_Medications = Many, and Has_Support = No.

Task:Build the three decision trees using bootstrap sampling and random feature selection, obtain each tree's prediction for the target patient, and use majority voting across the trees to determine the Random Forest prediction.

Need a quick refresher? Review Random Forest theory

Training Data & Target Patient

The eight labeled records are used to construct the bootstrap samples for each tree. The target patient has no known class label and will be classified using the forest's combined predictions.

Data Point
Age_Group✎
Diagnosis_Severity✎
Num_Medications✎
Has_Support✎
Readmitted
P1
Young
Mild
Few
Yes
No
P2
Young
Severe
Many
No
Yes
P3
Middle
Mild
Few
Yes
No
P4
Middle
Severe
Many
Yes
Yes
P5
Senior
Mild
Many
No
Yes
P6
Senior
Severe
Few
No
Yes
P7
Middle
Mild
Many
No
No
P8
Young
Mild
Few
No
No
Target
Senior
Mild
Many
No
?

Forest Configuration (3 Trees)

Tree 1

Must equal exactly 8 rows.

Tree 2

Must equal exactly 8 rows.

Tree 3

Must equal exactly 8 rows.

Bootstrapped Rows: 1, 1, 3, 4, 5, 6, 6, 8
Tree Prediction: Yes
Feature Selection: m = 2 features per tree, predefined for this worked example
Allowed Features: Age_Group, Diagnosis_Severity
Age_Group ?
Young
Class: No
Middle
Diagnosis_Severity ?
Mild
Class: No
Severe
Class: Yes
Senior
Class: Yes

Tree 1 Math Breakdown

Iteration 1

Context: Root

Available Features:Age_Group, Diagnosis_Severity, Num_Medications, Has_Support
Random Candidate Features:Age_Group, Diagnosis_Severity
Current Data: Full Bootstrap Sample8 Rows
Data PointAge_Group(cand)Diagnosis_Severity(cand)Num_MedicationsHas_SupportReadmitted
P1YoungMildFewYesNo
P1YoungMildFewYesNo
P3MiddleMildFewYesNo
P4MiddleSevereManyYesYes
P5SeniorMildManyNoYes
P6SeniorSevereFewNoYes
P6SeniorSevereFewNoYes
P8YoungMildFewNoNo
1. Entropy of Target Class, Entropy (S)
Entropy(S)=−PP+Nlog⁡2(PP+N)−NP+Nlog⁡2(NP+N)\mathrm{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' = 4

Calculation:

P=N⇒Entropy(S)=1P = N \Rightarrow \mathrm{Entropy}(S) = 1
Entropy(S)=1\mathrm{Entropy}(S) = 1
2. Subset Information Required
Entropy(Pi,Ni)=−PiPi+Nilog⁡2(PiPi+Ni)−NiPi+Nilog⁡2(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: Age_Group
ValuePiNi
Young03
Middle11
Senior30

Detailed Entropy Calculation

Value: Young

Pi=0,Ni=3,Pi+Ni=3P_i = 0,\quad N_i = 3,\quad P_i + N_i = 3
Since Pi=0 or Ni=0,Entropy(Pi,Ni)=0\text{Since } P_i = 0 \text{ or } N_i = 0,\quad \text{Entropy}(P_i,N_i) = 0
I(Pi,Ni)=0I(P_i,N_i) = 0

Value: Middle

Pi=1,Ni=1,Pi+Ni=2P_i = 1,\quad N_i = 1,\quad P_i + N_i = 2
Since Pi=Ni,Entropy(Pi,Ni)=1\text{Since } P_i = N_i,\quad \text{Entropy}(P_i,N_i) = 1
I(Pi,Ni)=1I(P_i,N_i) = 1

Value: Senior

Pi=3,Ni=0,Pi+Ni=3P_i = 3,\quad N_i = 0,\quad P_i + N_i = 3
Since Pi=0 or Ni=0,Entropy(Pi,Ni)=0\text{Since } P_i = 0 \text{ or } N_i = 0,\quad \text{Entropy}(P_i,N_i) = 0
I(Pi,Ni)=0I(P_i,N_i) = 0

Evaluated Feature: Age_Group

ValuePiNiI (Pi, Ni)
Young030
Middle111
Senior300
Evaluating Feature: Diagnosis_Severity
ValuePiNi
Mild14
Severe30

Detailed Entropy Calculation

Value: Mild

I(Pi,Ni)=−15log⁡2(15)−45log⁡2(45)I(P_i,N_i) = -\dfrac{1}{5}\log_2\left(\dfrac{1}{5}\right) - \dfrac{4}{5}\log_2\left(\dfrac{4}{5}\right)
=−(0.2)log⁡2(0.2)−(0.8)log⁡2(0.8)= -(0.2)\log_2(0.2) - (0.8)\log_2(0.8)
=−(0.2)(−2.322)−(0.8)(−0.322)= -(0.2)(-2.322) - (0.8)(-0.322)
=0.464+0.258= 0.464 + 0.258
I(Pi,Ni)=0.722I(P_i,N_i) = 0.722

Value: Severe

Pi=3,Ni=0,Pi+Ni=3P_i = 3,\quad N_i = 0,\quad P_i + N_i = 3
Since Pi=0 or Ni=0,Entropy(Pi,Ni)=0\text{Since } P_i = 0 \text{ or } N_i = 0,\quad \text{Entropy}(P_i,N_i) = 0
I(Pi,Ni)=0I(P_i,N_i) = 0

Evaluated Feature: Diagnosis_Severity

ValuePiNiI (Pi, Ni)
Mild140.722
Severe300
3. Weighted Feature Entropy
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)
Detailed Weighted Entropy: Age_Group

Calculation:

Entropy(AgeGroup)=(38)×0+(28)×1+(38)×0\text{Entropy}(Age_Group) = \left(\dfrac{3}{8}\right) \times 0 + \left(\dfrac{2}{8}\right) \times 1 + \left(\dfrac{3}{8}\right) \times 0
=(0.375)×0+(0.25)×1+(0.375)×0= \left(0.375\right) \times 0 + \left(0.25\right) \times 1 + \left(0.375\right) \times 0
=0+0.25+0= 0 + 0.25 + 0
=0.25= 0.25
Entropy(AgeGroup)=0.25\text{Entropy}(Age_Group) = 0.25
Detailed Weighted Entropy: Diagnosis_Severity

Calculation:

Entropy(DiagnosisSeverity)=(58)×0.722+(38)×0\text{Entropy}(Diagnosis_Severity) = \left(\dfrac{5}{8}\right) \times 0.722 + \left(\dfrac{3}{8}\right) \times 0
=(0.625)×0.722+(0.375)×0= \left(0.625\right) \times 0.722 + \left(0.375\right) \times 0
=0.451+0= 0.451 + 0
=0.451= 0.451
Entropy(DiagnosisSeverity)=0.451\text{Entropy}(Diagnosis_Severity) = 0.451
4. Feature Information Gain
Gain(S,A)=Entropy(S)−Entropy(A)\text{Gain}(S, A) = \text{Entropy}(S) - \text{Entropy}(A)
Age_Group1 - 0.25 =Gain:0.75
Diagnosis_Severity1 - 0.451 =Gain:0.549
5. Feature Selection Decision

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

Resulting Split
Age_Group ?
Young
Class: No
Middle
Class: ?
Senior
Class: Yes

Iteration 2

Context: Age_Group = Middle

Available Features:Diagnosis_Severity, Num_Medications, Has_Support
Random Candidate Features:Diagnosis_Severity
Current Data: Filtered by Age_Group = Middle2 Rows
Data PointAge_GroupDiagnosis_Severity(cand)Num_MedicationsHas_SupportReadmitted
P3MiddleMildFewYesNo
P4MiddleSevereManyYesYes
1. Entropy of Target Class, Entropy (S)
Entropy(S)=−PP+Nlog⁡2(PP+N)−NP+Nlog⁡2(NP+N)\mathrm{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

Calculation:

P=N⇒Entropy(S)=1P = N \Rightarrow \mathrm{Entropy}(S) = 1
Entropy(S)=1\mathrm{Entropy}(S) = 1
2. Subset Information Required
Entropy(Pi,Ni)=−PiPi+Nilog⁡2(PiPi+Ni)−NiPi+Nilog⁡2(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: Diagnosis_Severity
ValuePiNi
Mild01
Severe10

Detailed Entropy Calculation

Value: Mild

Pi=0,Ni=1,Pi+Ni=1P_i = 0,\quad N_i = 1,\quad P_i + N_i = 1
Since Pi=0 or Ni=0,Entropy(Pi,Ni)=0\text{Since } P_i = 0 \text{ or } N_i = 0,\quad \text{Entropy}(P_i,N_i) = 0
I(Pi,Ni)=0I(P_i,N_i) = 0

Value: Severe

Pi=1,Ni=0,Pi+Ni=1P_i = 1,\quad N_i = 0,\quad P_i + N_i = 1
Since Pi=0 or Ni=0,Entropy(Pi,Ni)=0\text{Since } P_i = 0 \text{ or } N_i = 0,\quad \text{Entropy}(P_i,N_i) = 0
I(Pi,Ni)=0I(P_i,N_i) = 0

Evaluated Feature: Diagnosis_Severity

ValuePiNiI (Pi, Ni)
Mild010
Severe100
3. Weighted Feature Entropy
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)
Detailed Weighted Entropy: Diagnosis_Severity

Calculation:

Entropy(DiagnosisSeverity)=(12)×0+(12)×0\text{Entropy}(Diagnosis_Severity) = \left(\dfrac{1}{2}\right) \times 0 + \left(\dfrac{1}{2}\right) \times 0
=(0.5)×0+(0.5)×0= \left(0.5\right) \times 0 + \left(0.5\right) \times 0
=0+0= 0 + 0
=0= 0
Entropy(DiagnosisSeverity)=0\text{Entropy}(Diagnosis_Severity) = 0
4. Feature Information Gain
Gain(S,A)=Entropy(S)−Entropy(A)\text{Gain}(S, A) = \text{Entropy}(S) - \text{Entropy}(A)
Diagnosis_Severity1 - 0 =Gain:1
5. Feature Selection Decision

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

Resulting Split
Diagnosis_Severity ?
Mild
Class: No
Severe
Class: Yes

Final Answer

Tree predictions:Tree 1 = Yes, Tree 2 = No, Tree 3 = Yes

Final prediction:Yes

The trees were constructed using different bootstrap samples and feature restrictions, so they can classify the same patient differently. The forest uses their collective vote rather than relying on one tree's decision.

Exam Takeaway

  • Preserve repeated records in bootstrap samples: Sampling with replacement means a training row can appear more than once. Count every occurrence when calculating class distributions, entropy, and Information Gain for that tree.
  • Evaluate only the permitted features at each split: Do not automatically compare every feature in the dataset. Follow the candidate-feature restrictions given in the question and select the highest-gain feature from those available at the current node.
  • Separate tree predictions from the forest prediction: First follow the target record through every tree, then count the class votes. A majority vote determines the forest's predicted class, but the vote fraction is not necessarily a calibrated prediction probability.