Day 20-Machine Learning
🌟 Concept Overview
Gradient Boosting is a sequential ensemble technique — unlike Random Forest (parallel bagging), each model learns from the errors (residuals) of the previous one.
You’re using simple decision stumps (tiny trees) as weak learners, gradually improving predictions
🧮 Key Formulas
Loss function:
Gradient (negative direction):
Model update rule:
🧩 Step 0 — Initial Model F0
Start with a constant prediction F0=mean(y)=154.
Compute residuals r1=y−F0.
These residuals become the targets for the first stump.
Dataset:
Residuals r1=y−F0:
These residuals become the targets for the first stump.
🧮 Step 1 — Candidate Splits for h1(x)
Candidate splits tested on Size and Beds.
For each threshold, compute mean residuals on left/right and SSE (sum of squared errors).
The best split (lowest SSE = 916.66) is Size < 1050.
Left mean = −20.66, Right mean = 31.
Update model: F1(x)=F0+η⋅h1(x)
where learning rate η=0.1.
🌳 Candidate Splits — Definition
When training a tree (or stump in boosting), the algorithm must decide where to split the data.
A candidate split is simply a possible threshold on a feature.
Example:
Feature = Size
Candidate splits = 850, 900, 950, 1000, 1050, etc.
Each split divides the dataset into Left group (values ≤ threshold) and Right group (values > threshold).
The algorithm evaluates all candidate splits across all features, then chooses the one that minimizes error (variance, SSE, Gini, entropy, depending on the task).
we calculate for all the mid points i.e. for both the features size and beds i.e. 850, 950, 1050, 1150 and for Beds it will be 2.5 and 3.5
Below is the calculation for the same
Let us take the Threshold of 1050 and break up of all the calculation as follows and the same calculation applies for all other threshold to that 950,1150,850,2.5, 3.5 and we select the lowest SSE
Example: Size < 1050
Left = [1, 2, 3], Right = [4, 5]
Left residuals: [−34, −24, −4]
Right residuals: [16, 46]
Now compute SSE for each side:
Formula
Left (A)
with each residual value we are multiplying the Mean of A
Right (B)
with each residual value we are multiplying the Mean of B
✅ Lowest SSE = 916.5 → Best split: Size < 1050
🌳 Step 2 — First Stump h1(x)
We started with the initial model prediction
F0=154.
Residuals r1=y−F0:
Now, from the above steps we understood the Best split: Size < 1050
Candidate Split: Size < 1050
This split divides the data into:
Left group (Size < 1050): Id’s 1, 2, 3 → residuals [−34, −24, −4]
Right group (Size ≥ 1050): Id’s 4, 5 → residuals [+16, +46]
🔢 Left Group Mean
🔢 Right Group Mean
📌 Interpretation
For any house with Size < 1050, the stump predicts the average residual of that group: −20.66.
For any house with Size ≥ 1050, the stump predicts the average residual of that group: +31.
That’s why the stump is written as:
✅ Why This Split Was Chosen
We tested multiple candidate splits (Size < 850, Size < 950, Beds < 3, etc.).
For each, we computed the Sum of Squared Errors (SSE).
Size < 1050 gave the lowest total SSE (≈ 916.5).
That’s why it was selected as the first stump.
👉 So the “−20.66” and “31” are simply the average residuals of the two groups formed by the split at 1050.
Learning rate η=0.1
Update model:
F0 =154 from previous steps
🧮 Step 3 — Second Stump h2(x)
Now fit a new stump on residuals r2.
Candidate splits again tested on Size and Beds.
Best split still Size < 1050 (lowest SSE = 916.6).
Left residuals = [−31.93, −21.93, −1.93] → mean = −18.6
Right residuals = [12.9, 42.9] → mean = 27.9
Calculation Break up
After the first stump update, the new predictions F1(x) gave residuals r2:
🧮 Candidate Split: Size < 1050
Divide data into two groups:
Left group (Size < 1050): ID 1, 2, 3 → residuals [−31.93, −21.93, −1.93]
Right group (Size ≥ 1050): ID 4, 5 → residuals [+12.9, +42.9]
🔢 Left Group Mean
🔢 Right Group Mean
📌 Interpretation
So the second stump is:
🧮 SSE Check (to confirm best split)
Now compute squared errors relative to group means:
Left Group SSE
Right Group SSE
✅ Why This Split Was Chosen Again
Among all candidate splits (Size thresholds, Beds thresholds), Size < 1050 still gave the lowest SSE.
That’s why the second stump also used the same threshold, but with updated group means (−18.6 and 27.9).
🔁 Step 4 — Third Stump h3(x)
Residuals r3 show the same sign pattern (IDs 1–3 negative, 4–5 positive).
So again, Size < 1050 remains the best split.
Each iteration reduces residual magnitude and total SSE.
Eventually, residuals shrink toward zero, meaning the model has learned the pattern.
🧠 Final Model Expression
⚡ What Is XGBoost?
XGBoost (Extreme Gradient Boosting) is an advanced version of Gradient Boosting — it’s faster, more regularized, and mathematically refined.
Think of it as Gradient Boosting + Optimization + Regularization + Parallelization.
Here’s how it differs conceptually:
So, XGBoost improves Gradient Boosting by:
Using both gradient and Hessian (Newton optimization).
Adding L1 (γ) and L2 (λ) regularization to prevent overfitting.
Computing gain to choose the best split mathematically.
Allowing parallel tree building for speed.
🧩 Iteration 1 — Full Calculation Breakdown
1️⃣ Initial Setup
Step 0 — Base Model
Initial prediction for all rows:
F0=mean(y)=154
Loss function: L=1/2(y−y^)2
Gradient: g=y^−y
Hessian: h=1 (for squared error)
Regularization parameters:
λ=1 (L2)
γ=0 (L1 penalty, default off)
🧮 Iteration 2 — Full Calculation
After Iteration 1, your predictions were:
Candidate Splits
Here GI And Gr means Gradients Left(I) and Right (r) and Same goes H Hessians
if you are wondering H Left and H Right for split point 850 is 1 and 4 when size is less than 850, the values get split accordingly
✅ Best split again: Size < 950 (Gain = 1962.33)
Since 950 turned out to be our best split, let's break down exactly how we got that gain value. Normally, we just look at all the midpoints and pick the one with the highest gain. Checking every single point by hand would take forever, so we’ll just walk through the math for 950. The exact same logic applies to all the other points anyway!
🧩 Step 1 —Recall Gradients & Hessians (Iteration 1)
From the base model F0=154:
🧩 Step 2 — Candidate Split at Size < 950
Left group (ID’s, 1, 2):
GL=34+24=58, HL=1+1=2
Right group (ID’s 3, 4, 5):
GR=4+(−16)+(−46)=−58, HR = 1+1+1=3
Parent totals:
GP = GL+GR =58+(−58)=0, HP = 2+3=5
🧩 Step 3 — Gain Formula
Parameters:
λ=1 (L2 regularization)
γ=0 (L1 penalty off)
🧩 Step 4 — Plugging Values
Left term:
Right term:
Parent term:
🧩 Step 5 — Final Gain
Omit the ½ factor when presenting Gain, so they report the raw sum as 1962.33.
Now the split is fixed that is 950
🧩 Step 1 — Fix the Split
The dataset is divided into two groups:
Left node (Size < 950): ID’s ——> 1, 2
Right node (Size ≥ 950): ID’s ——> 3, 4, 5
This becomes the first stump (tree with one split).
🧩 Step 2 — Compute Leaf Weights
Formula for leaf output: Leaf Weights
GL and GR we got from previous steps
🧩 Step 3 — Update Predictions
Now we add the leaf weight to the base prediction F0=154. Formula:
with learning rate η=1.
🧩 Step 4 — Compute New Residuals
Residuals after Iteration
🔁 Iteration 3 (Conceptually)
You’d now compute new gradients g3=y^2−y and Hessians h3=1, then repeat the same gain and leaf‑weight process.
Each iteration refines predictions and reduces residuals further.
🧠 Intuitive Summary
XGBoost starts like Gradient Boosting but adds mathematical rigor:
Gradient → direction of correction
Hessian → step size control
Regularization → stability
Gain → objective measure of split quality
The model keeps building trees until residuals are minimized.



















