Skip to main content

Random Forest Optimization


We input a fruit: Apple (Color = Red, Size = Small). Random Forest predicts its class by sending it through multiple decision trees and taking a majority vote.

How Random Forest Works Internally

Random Forest Decision Trees Diagram Tree 1 Color? Red→Apple Org→Org Tree 2 Size? Sml→Apple Lrg→Org Tree 3 Color? Red→Apple Org→Org Majority Vote → Apple

How Random Forest Differs from General ML Models

  • General ML: Learns complex function from all training data (e.g., neural network weights, linear regression coefficients).
  • Random Forest: Builds multiple simple decision trees on random subsets of data and features.
  • General ML models often have continuous parameters and internal layers/tensors.
  • Random Forest has no layers or tensors; its “learning” is simple rules at each node (splits) and aggregation by majority vote or averaging.
  • Random Forest uses bootstrap sampling and random feature selection to reduce overfitting and improve generalization.
  • Prediction in Random Forest = aggregate of simple tree predictions, while general ML models compute a complex mapping function.

1. Overview

Random Forest is an ensemble learning method that builds multiple decision trees and combines their outputs to improve predictive accuracy and reduce overfitting.

  • For classification: output is the majority vote of the trees.
  • For regression: output is the average of the trees’ predictions.

Mathematically, it can be expressed as aggregating outputs of multiple trees.

2. Decision Tree Basics

A single decision tree partitions the feature space recursively:

  • Let the input vector be 𝑥 = (x₁, x₂, ..., xₚ)
  • Tree splits create regions Rₘ in feature space.
  • Prediction in region Rₘ:
Regression: ŷ = (1 / Nₘ) Σi ∈ Rₘ yᵢ
Classification: ŷ = mode({yᵢ: i ∈ Rₘ})

Where Nₘ is the number of samples in region Rₘ.

3. Random Forest Prediction

Assume we have B trees {T₁, T₂, ..., T_B}:

Regression:
ŷ_RF(x) = (1 / B) Σb=1 to B T_b(x)

Classification:
ŷ_RF(x) = majority_vote { T₁(x), T₂(x), ..., T_B(x) }

Where T_b(x) is the prediction of the b-th tree.

4. Bootstrap Aggregation (Bagging)

  • Each tree is trained on a bootstrap sample (random sample with replacement).
  • Reduces variance by decorrelating trees.
If the training set is D = { (xᵢ, yᵢ) } for i = 1..N
Bootstrap sample for tree b: D_b ~ Uniform sample with replacement from D

5. Random Feature Selection

At each split, instead of using all p features, a random subset of m << p features is considered:

  • Reduces correlation between trees.
  • Split selection:
Choose feature j* = argmax (Information Gain or Gini Reduction) 
from random subset of m features

6. Out-of-Bag (OOB) Error

  • For each sample, some trees did not include it in their bootstrap set.
  • OOB prediction for sample i:
ŷ_OOB,i = (1 / |B_i|) Σb ∈ B_i T_b(x_i)

Where B_i = set of trees where i was not included in training.

OOB error estimates generalization error without a separate validation set.

Random Forest = Bagging + Random Feature Selection

  1. Build B trees on bootstrap samples.
  2. At each split, select the best split from a random subset of features.
  3. Predict by averaging (regression) or majority vote (classification).
  4. OOB samples estimate generalization error.

Random Forest Example with Dummy Dataset

Let’s break Random Forest down with a small, simple dataset for classification.

1. Dummy Dataset

Suppose we have a dataset of fruits with features Color and Size, and we want to predict if the fruit is Apple or Orange.

Fruit Color Size
1RedSmall
2RedLarge
3OrangeLarge
4OrangeSmall
5RedSmall
6OrangeLarge

Notes: Color → Red/Orange, Size → Small/Large, Target → Apple (Red) or Orange

2. Step 1: Bootstrap Sampling

Random Forest trains each tree on a random sample with replacement. Example Tree 1 bootstrap sample:

Fruit Color Size
1RedSmall
2RedLarge
5RedSmall
6OrangeLarge
3OrangeLarge

Some rows may be missed and some may repeat.

3. Step 2: Random Feature Selection

At each split, Random Forest randomly selects a feature instead of using all features.

  • Suppose Tree 1 chooses Color first → split Red vs Orange.
  • Next, Tree 1 might consider Size in each branch.

4. Step 3: Build the Tree

         Color?
       /       \
     Red       Orange
    /   \       /    \
  Small Large Large Small
Apple Apple Orange Orange

Leaves give predicted class based on majority vote.

5. Step 4: Build More Trees

Random Forest builds multiple trees with different bootstrap samples and random features.

Example predictions for new fruit (Red, Small):

Tree Prediction
1Apple
2Apple
3Orange

6. Step 5: Aggregate Predictions

  • Classification: majority vote → Apple
  • Regression: average of tree outputs.

Another Example

Key Points

  1. Random Forest uses multiple decision trees.
  2. Each tree sees a different random sample.
  3. Each tree considers a random subset of features at each split.
  4. Predictions are aggregated: majority vote (classification) or average (regression).
  5. This reduces overfitting compared to a single tree.

Dataset

Fruit Color Size Target
1RedSmallApple
2RedLargeApple
3OrangeLargeOrange
4OrangeSmallOrange
5RedSmallApple
6OrangeLargeOrange

We want to predict the fruit type based on Color and Size.

Step 1: Bootstrap Sampling

  • Tree 1 sample: 1, 2, 5, 6, 3
  • Tree 2 sample: 2, 3, 4, 5, 6
  • Tree 3 sample: 1, 3, 3, 4, 5

Notice some fruits repeat and some are missing.

Step 2: Build Trees with Random Feature Selection

At each split, Random Forest chooses a random subset of features.

Tree 1

         Color?
       /       \
     Red       Orange
    /   \       /    \
  Small Large Large Small
 Apple  Apple Orange Orange

Tree 2

         Size?
       /       \
     Small     Large
    /   \       /    \
  Red Orange Red  Orange
 Apple Orange Apple Orange

Tree 3

         Color?
       /       \
     Orange     Red
    /    \      /   \
 Small Large Small Large
 Orange Orange Apple Apple

Step 3: Predict a New Sample

New fruit: Color = Red, Size = Small

  • Tree 1 predicts: Apple
  • Tree 2 predicts: Apple
  • Tree 3 predicts: Apple

Majority vote → Apple

Step 4: How Randomness Helps

  • Bootstrap: Trees see different samples → reduces overfitting.
  • Random features: Trees are less correlated → improves generalization.

Step 5: Aggregate Prediction

  • Classification: majority vote → Apple
  • Regression: average of tree outputs

Further Reading



Contact Us

Name

Email *

Message *

Popular Posts

Online Simulator for ASK, FSK, and PSK Signal Generation

Interactive Digital Signal Processing (DSP) Tutorial and Simulator for ASK, FSK, and BPSK modulation techniques. Try our new Digital Signal Processing Simulator!   •   Interactive ASK, FSK, and BPSK tools updated for 2025. Start Now Digital Modulation Visualizer: ASK, FSK, & BPSK Simulator Learn and visualize binary modulation techniques (ASK, FSK, BPSK) in real-time with adjustable carrier and sampling parameters. Perfect for DSP students and engineers. 📡 ASK Simulator 📶 FSK Simulator 🎚️ BPSK Simulator 📚 More Topics ASK Modulator FSK Modulator BPSK Modulator Demodulation More Topics 1. ASK (Ampli...

UGC NET Electronic Science Previous Year Question Papers with Solutions

Download Papers and Solutions Exam Pattern Preparation Tips FAQs More Home / Engineering & Other Exams / UGC NET 2026 PYQ 📊 Exam Highlights: Electronic Science (88) Feature Details Junior Research Fellowship (JRF) ₹37,000 + HRA per month Eligibility M.Sc/M.Tech in Electronics (55%) Validity of Certificate JRF (3 Years) | Lectureship (Lifetime) 📥 Download UGC NET Electronics PDFs Complete collection of previous year question papers, answer keys and explanations for Subject Code 88. Start Downloading 📂 View All Question Papers June 2025 - Question Paper Download PDF June 2025 - Sol...

Direction of Arrival (DoA) Online Simulator (using MUSIC)

Interactive DOA Simulator X-axis XY angle (deg): 45 XZ angle (deg): 30 Noise: 0.05 Y-axis XY angle (deg): 60 YZ angle (deg): 45 Noise: 0.05 Z-axis XZ angle (deg): 60 YZ angle (deg): 30 Noise: 0.05 Estimated DOA (deg): 0 Simulation Workflow and Mathematical Background This simulator demonstrates Direction of Arrival (DOA) estimation using three-axis sensor signals (X, Y, Z), Maximal Ratio Combining (MRC) , and the MUSIC algorithm . It allows interactive control of signal angles and noise for teaching purposes. 1. Signal Generation A pure sinewave signal of frequency f is projected onto three axes using user-defined angles in different planes: X-axis: θ XY , θ XZ Y-axis: θ XY , θ YZ Z-axis: θ XZ , θ YZ Mathematically, for each time sample t : x(t) = s(t) * cos(θ_xy_x) * cos(θ_xz_x) + n_x(t) y(t) = s(t) * sin(θ_xy_y) * cos(θ_yz_y) + n_y(t) z(t) = s(t) * sin(θ_xz_z) * sin(θ_yz_z) + n_z(t) wh...

Constellation Diagrams of ASK, PSK, and FSK (with MATLAB Code + Simulator)

Constellation Diagrams: ASK, FSK, and PSK Comprehensive guide to signal space representation, including interactive simulators and MATLAB implementations. 📘 Overview 🧮 Simulator ⚖️ Theory 📈 Q-function 📚 Resources BASK Modulation Transmits one of two signals: 0 or $\sqrt{E_b}$, representing binary 0 and 1. Simple but sensitive to noise. BFSK Modulation Transmits one of two signals: $\sqrt{E_b}$ on the Y-axis or $\sqrt{E_b}$ on the X-axis. These are orthogonal signals. BPSK Modulation Transmits $+\sqrt{E_b}$ or $-\sqrt{E_b}$ (antipodal signaling). Most efficient binary scheme. ...

OFDM Symbols and Subcarriers Explained

This article explains how OFDM (Orthogonal Frequency Division Multiplexing) symbols and subcarriers work. It covers modulation, mapping symbols to subcarriers, subcarrier frequency spacing, IFFT synthesis, cyclic prefix, and transmission. Step 1: Modulation First, modulate the input bitstream. For example, with 16-QAM , each group of 4 bits maps to one QAM symbol. Suppose we generate a sequence of QAM symbols: s0, s1, s2, s3, s4, s5, …, s63 Step 2: Mapping Symbols to Subcarriers Assume N sub = 8 subcarriers. Each OFDM symbol in the frequency domain contains 8 QAM symbols (one per subcarrier): Mapping (example) OFDM symbol 1 → s0, s1, s2, s3, s4, s5, s6, s7 OFDM symbol 2 → s8, s9, s10, s11, s12, s13, s14, s15 … OFDM sym...

Design of CMOS XOR/XNOR Gates

Design of CMOS XOR/XNOR Gates The semiconductor industry has experienced rapid integration of multimedia applications into mobile electronics, leading to very high integration density in CMOS VLSI. As operating frequencies increase, power consumption, speed, silicon area, and reliability become critical considerations. The XOR-XNOR circuits are fundamental building blocks in arithmetic circuits (Full Adders, Multipliers), compressors, comparators, parity checkers, code converters, error-detecting/correcting codes, and phase detectors. Their performance directly impacts the complex circuits they are used in. Design goals include full output voltage swing, low power consumption, reduced transistor count, minimal delay, and simultaneous non-skewed outputs. Static Logic (Static CMOS) Stat...

DSB-SC Modulation and Demodulation

📘 Overview 🧮 DSB-SC Modulator 🧮 DSB-SC Detector 🧮 Comparisons 🧮 Q & A Summary 📚 Further Reading Double-sideband suppressed-carrier transmission (DSB-SC) is transmission in which frequencies produced by amplitude modulation (AM) are symmetrically spaced above and below the carrier frequency and the carrier level is reduced to the lowest practical level, ideally being completely suppressed. In the DSB-SC modulation, unlike in AM, the wave carrier is not transmitted; thus, much of the power is distributed between the sidebands, which implies an increase of the cover in DSB-SC, compared to AM, for the same power use. DSB-SC transmission is a special case of double-sideband reduced carrier transmission. It is used for radio data systems. This model is frequently used in Amateur radio voice communications, especially on High-Frequency bands. Spectrum DSB-SC i...