# Decision Trees (Entropy vs Gini), Random Forests & Gradient Boosting (XGBoost)
Exam: GATE Data Science & Artificial Intelligence (DA) (GATE DA)
Subject: Machine Learning & Deep Learning
Canonical URL: https://gateandnet.com/exams/gate-da/machine-learning-and-ai/decision-trees-random-forests-and-gradient-boosting

> Shannon Entropy, Information Gain, Gain Ratio (C4.5), CART Gini Impurity, Bagging out-of-bag variance formula, AdaBoost sample reweighting, and XGBoost second-order Newton boosting.

## 1. Impurity Criteria: ID3 vs C4.5 vs CART

| Tree Algorithm | Splitting Metric | Multiway vs Binary | Handles High-Cardinality IDs? |
| :--- | :--- | :---: | :--- |
| **ID3** | **Information Gain** $\operatorname{IG}(S, A)$ | Multiway | ❌ Biased toward attributes like `Roll_No` |
| **C4.5** | **Gain Ratio** $= \frac{\operatorname{IG}(S, A)}{\operatorname{SplitInfo}(S, A)}$ | Multiway / Binary | ✅ Penalizes large intrinsic split entropy |
| **CART** | **Gini Impurity** $1 - \sum p_c^2$ (or MSE for Regression) | **Strictly Binary** | Uses Cost-Complexity Pruning $R_\alpha(T) = R(T) + \alpha |T|$ |

> [!TIP]
> **Rapid Binary Node Calculation ($p_+ = 0.5, p_- = 0.5$):**
> - **Entropy** $H = -0.5\log_2(0.5) - 0.5\log_2(0.5) = \mathbf{1.0}$ bit (maximum for 2 classes; $\log_2 K$ for $K$ classes).
> - **Gini Impurity** $= 1 - (0.5^2 + 0.5^2) = \mathbf{0.50}$ (maximum for 2 classes; $1 - 1/K$ for $K$ classes).

## 2. Bagging & Random Forests vs Boosting (AdaBoost & Gradient Boosting)

**Bootstrap Aggregating (Bagging):** Draws $B$ bootstrap samples of size $n$ *with replacement*. Probability a specific instance is **never selected** in $n$ draws (**Out-of-Bag / OOB**):

$$
P(\text{OOB}) = \left(1 - \frac{1}{n}\right)^n \xrightarrow{n \to \infty} \frac{1}{e} \approx 0.3679 \; (36.8\%)
$$

Thus each bootstrap sample contains $\approx \mathbf{63.2\%}$ unique training rows!
- **Random Forest:** Combines Bagging + **Random Feature Subspace** ($m \approx \sqrt{d}$ for classification, $d/3$ for regression at *every split*) to decorrelate trees (lowering $\rho$ in $\rho\sigma^2 + \frac{1-\rho}{B}\sigma^2$).
- **Gradient Boosting:** Fits tree $h_m(x)$ sequentially to the **negative gradient (pseudo-residuals)** $r_{im} = -\left[\frac{\partial L(y_i, F(x_i))}{\partial F(x_i)}\right]_{F=F_{m-1}}$. **XGBoost** uses second-order Taylor expansion with gradient $g_i$ and Hessian $h_i$: optimal leaf weight $w_j^* = -\frac{\sum_{i \in I_j} g_i}{\sum_{i \in I_j} h_i + \lambda}$.

> [!TRAP]
> **Bagging vs Boosting Exam Classic:** Deep unpruned trees have *low bias, high variance* $\implies$ **Bagging / Random Forest** reduces **Variance** (cannot overfit solely by increasing $B$). Shallow decision stumps have *high bias, low variance* $\implies$ **Boosting** sequentially reduces **Bias** (can overfit noisy labels if $M$ is too large!).

## Solved Practice Questions & PYQs

### [GATE DA ML | MCQ]
A Decision Tree node $S$ contains $16$ positive and $16$ negative examples. Splitting on binary attribute $A$ produces left child $S_L$ ($12+, 4-$) and right child $S_R$ ($4+, 12-$). What is the **Gini Impurity reduction** ($\Delta \text{Gini}$) achieved by attribute $A$?
**Correct Answer:** A
**Solution:** - Parent $\operatorname{Gini}(S) = 1 - (0.5^2 + 0.5^2) = 0.50$.
- Each child has proportions $(3/4, 1/4)$, so $\operatorname{Gini}(S_L) = \operatorname{Gini}(S_R) = 1 - \left(\frac{9}{16} + \frac{1}{16}\right) = \frac{6}{16} = 0.375$.
- Reduction $\Delta \text{Gini} = 0.50 - \left(\frac{16}{32}(0.375) + \frac{16}{32}(0.375)\right) = 0.50 - 0.375 = \mathbf{0.125}$.

### [GATE DA ML | MCQ]
In an **AdaBoost** iteration, a weak classifier $h_t$ achieves weighted classification error $\epsilon_t = \frac{1}{e^2 + 1} \approx 0.1192$. What classifier weight $\alpha_t = \frac{1}{2}\ln\!\left(\frac{1-\epsilon_t}{\epsilon_t}\right)$ is assigned to $h_t$?
**Correct Answer:** B
**Solution:** Here $\frac{1 - \epsilon_t}{\epsilon_t} = \frac{e^2 / (e^2 + 1)}{1 / (e^2 + 1)} = e^2$. Therefore $\alpha_t = \frac{1}{2}\ln(e^2) = \frac{1}{2}(2) = \mathbf{1.0}$.
