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

UGC NET Electronic Science Previous Year Question Papers with Solutions

Home / Engineering & Other Exams / UGC NET 2026 PYQ ⬇️ Download Papers and Solutions ๐Ÿ“‹ Exam Pattern ๐Ÿ’ก Preparation Tips ❓ FAQs ๐Ÿ“Š 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 - Solved Paper + Explanation ...

MUSIC Algorithm Explained (with MATLAB + Simulator)

Practical Implementation of the MUSIC Algorithm The focus is on how the algorithm works computationally , not just theory, and it explains the denominator (a H E n E n H a) mathematically and intuitively. 1. Introduction The MUSIC (Multiple Signal Classification) algorithm is a high-resolution method used in signal processing and array processing to estimate the Direction of Arrival (DOA) of signals received by a sensor array. Unlike classical beamforming methods, MUSIC uses eigenvector decomposition of the covariance matrix to separate the signal subspace and noise subspace , allowing it to achieve much higher angular resolution. In practical implementations, MUSIC works by: Simulating or collecting array signals Computing the covariance matrix Performing eigenvalue decomposition Separating signal and noise subspaces Scanning possible angles using a steering vector Constructing a pseudo-spectrum where peaks indicate signal directions 2. Signal Mo...

BER vs SNR for M-ary QAM, M-ary PSK, QPSK, BPSK, ...(MATLAB Code + Simulator)

Bit Error Rate (BER) & SNR Guide Analyze communication system performance with our interactive simulators and MATLAB tools. ๐Ÿ“˜ Theory ๐Ÿงฎ Simulators ๐Ÿ’ป MATLAB Code ๐Ÿ“š Resources BER Definition SNR Formula BER Calculator MATLAB Comparison ๐Ÿ“‚ Explore M-ary QAM, PSK, and QPSK Topics ▼ ๐Ÿงฎ Constellation Simulator: M-ary QAM ๐Ÿงฎ Constellation Simulator: M-ary PSK ๐Ÿงฎ BER calculation for ASK, FSK, and PSK ๐Ÿงฎ Approaches to BER vs SNR Calculation What is Bit Error Rate (BER)? The BER indicates how many corrupted bits are received compared to the total number of bits sent. It is the primary figur...

MATLAB Code for ASK, FSK, and PSK (with Online Simulator)

MATLAB Code for ASK, FSK, and PSK Comprehensive implementation of digital modulation and demodulation techniques with simulation results. ๐Ÿ“˜ Theory ๐Ÿ“ก ASK Code ๐Ÿ“ถ FSK Code ๐ŸŽš️ PSK Code ๐Ÿ•น️ Simulator ๐Ÿ“š Further Reading Amplitude Shift Frequency Shift Phase Shift Live Simulator ASK, FSK & PSK HomePage MATLAB Code MATLAB Code for ASK Modulation and Demodulation COPY % The code is written by SalimWireless.Com clc; clear all; close all; % Parameters Tb = 1; fc = 10; N_bits = 10; Fs = 100 * fc; Ts = 1/Fs; samples_per_bit = Fs * Tb; rng(10); binar...

PSD Calculation with FFT: MATLAB Tutorial for Signal Analysis

  Implementation Steps 1. FFT Computes the Frequency Content of a Signal FFT converts a time-domain signal to the frequency domain. If: The signal is sampled at rate $f_s$ You compute an $N_{\text{FFT}}$-point FFT Then each FFT bin corresponds to a frequency resolution of: $$\Delta f = \frac{f_s}{N_{\text{FFT}}}$$ So the FFT gives you accurate frequency content, assuming the signal is stationary and adequately sampled (Nyquist criterion met).  2. Magnitude Squared Gives Power (Not Amplitude) $$P[k] = |X[k]|^2$$ This gives power at each frequency bin, not just amplitude. It represents how much energy is present at each frequency. It's a key step for PSD.  3. Normalization Makes the PSD Physically Meaningful The equation: $$\text{PSD}[k] = \frac{|X[k]|^2}{N_{\text{FFT}} \cdot f_s \cdot U}$$ is derived from first principles and ensures that the u...

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. ...

MATLAB code for BER vs SNR for M-QAM, M-PSK, QPSK, BPSK (with Simulation)

๐Ÿงฎ MATLAB Code for BPSK, M-ary PSK, and M-ary QAM Together ๐Ÿงฎ MATLAB Code for M-ary QAM ๐Ÿงฎ MATLAB Code for M-ary PSK ๐Ÿ“š Further Reading MATLAB Script for BER vs. SNR for M-QAM, M-PSK, QPSK, BPSK % Written by Salim Wireless clc; clear; close all; snr_db = -5:2:25; psk_orders = [2, 4, 8, 16, 32]; qam_orders = [4, 16, 64, 256]; ber_psk_results = zeros(length(psk_orders), length(snr_db)); ber_qam_results = zeros(length(qam_orders), length(snr_db)); for i = 1:length(psk_orders) ber_psk_results(i, :) = berawgn(snr_db, 'psk', psk_orders(i), 'nondiff'); end for i = 1:length(qam_orders) ber_qam_results(i, :) = berawgn(snr_db, 'qam', qam_orders(i)); end figure; semilogy(snr_db, ber_psk_results(1, :), 'o-', 'LineWidth', 1.5, 'DisplayName', 'BPSK'); hold on; for i = 2:length(psk_orders) semilogy(snr_db, ber_psk_results(i, :), 'o-', 'DisplayName', sprintf('%d-PSK', psk_or...

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...