Quiz 2

Boosting: AdaBoost, Gradient Boosting & XGBoost

667 words
3 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# Boosting: AdaBoost, Gradient Boosting & XGBoost ## 🎯 Learning Objectives - Explain how boosting converts weak learners to strong ensembles - Implement AdaBoost and Gradient Boosting with sklearn - Tune hyperparameters for gradient boosting - Understand when boosting outperforms bagging ## 📖 Core Content ### 14.1...

Boosting: AdaBoost, Gradient Boosting & XGBoost

🎯 Learning Objectives

  • Explain how boosting converts weak learners to strong ensembles
  • Implement AdaBoost and Gradient Boosting with sklearn
  • Tune hyperparameters for gradient boosting
  • Understand when boosting outperforms bagging

📖 Core Content

14.1 Intuition: Learning from Mistakes

Instead of training models independently (bagging), boosting trains them sequentially — each new model focuses on the mistakes of the previous ones. It's like a student who reviews their wrong answers on a practice test, then takes another test focused on those weak areas. (Diagram)

14.2 AdaBoost

Algorithm:
  1. Initialize all sample weights wi=1/mw_i = 1/m
  2. For t=1,2,,Tt = 1, 2, \dots, T:
    • Train a weak learner ht(x)h_t(x) using current weights
    • Compute weighted error ϵt=i:ht(xi)yiwi/wi\epsilon_t = \sum_{i: h_t(x_i) \neq y_i} w_i / \sum w_i
    • Compute learner weight αt=0.5ln((1ϵt)/ϵt)\alpha_t = 0.5 \ln((1-\epsilon_t)/\epsilon_t)
    • Update sample weights: wiwiexp(αtyiht(xi))w_i \leftarrow w_i \cdot \exp(-\alpha_t y_i h_t(x_i))
    • Normalize weights to sum to 1
  3. Final prediction: y^=sign(αtht(x))\hat{y} = \text{sign}(\sum \alpha_t h_t(x))
python
# runnable
from sklearn.ensemble import AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import make_classification
X, y = make_classification(n_samples=500, random_state=42)
ada = AdaBoostClassifier(
    estimator=DecisionTreeClassifier(max_depth=1),  # Stump = weak learner
    n_estimators=100,
    learning_rate=1.0,
    random_state=42
)
ada.fit(X, y)
print(f"AdaBoost accuracy: {ada.score(X, y):.3f}")

14.3 Gradient Boosting (GBM)

Instead of reweighting samples, gradient boosting fits each new model to the residuals (negative gradient) of the previous ensemble:
  1. Initialize F0(x)=yˉF_0(x) = \bar{y} (constant prediction)
  2. For t=1,2,,Tt = 1, 2, \dots, T:
    • Compute residuals: ri=yiFt1(xi)r_i = y_i - F_{t-1}(x_i) (for MSE loss)
    • Fit a tree ht(x)h_t(x) to predict rir_i
    • Update: Ft(x)=Ft1(x)+νht(x)F_t(x) = F_{t-1}(x) + \nu \cdot h_t(x) Where ν\nu (learning_rate) controls how much each tree contributes.
python
# runnable
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
X, y = make_classification(n_samples=1000, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
gb = GradientBoostingClassifier(
    n_estimators=200,
    learning_rate=0.1,
    max_depth=3,
    subsample=0.8,
    min_samples_leaf=10,
    random_state=42
)
gb.fit(X_train, y_train)
print(f"GB test accuracy: {gb.score(X_test, y_test):.3f}")

14.4 XGBoost: Extreme Gradient Boosting

XGBoost is an optimized version with:
  • Regularization (L1 and L2 on tree weights)
  • Parallel processing (tree construction is parallelized)
  • Handling missing values (learns default direction)
  • Weighted quantile sketch (efficient approximate split finding)
  • Cross-validation during training
python
# Note: xgboost may not be installed - this is reference code
# import xgboost as xgb
# model = xgb.XGBClassifier(n_estimators=100, learning_rate=0.1, max_depth=3,
#                           reg_alpha=0.1, reg_lambda=1.0, subsample=0.8)
# model.fit(X_train, y_train)

14.5 Hyperparameter Tuning

ParameterRangeEffect
n_estimators50-1000More trees = better but risk overfitting
learning_rate0.01-0.3Lower = more trees needed, more robust
max_depth2-8Deeper trees = more complex, risk overfitting
subsample0.5-1.0Lower = more randomness, less overfitting
min_samples_leaf5-50Higher = simpler trees
reg_alpha (L1)0-10Feature selection
reg_lambda (L2)0-10Shrinkage

📝 Practice Questions

Q1: How does AdaBoost assign weights to training examples?
Initially all weights are equal (1/m). After each round, misclassified examples have their weights increased (multiplied by e^α), and correctly classified ones have weights decreased. This forces the next learner to focus on hard examples. Q2: What's the difference between AdaBoost and Gradient Boosting?
AdaBoost reweights training examples; Gradient Boosting fits new models to the residuals of previous models. AdaBoost can use any weak learner; Gradient Boosting typically uses shallow trees. Gradient Boosting generalizes to any differentiable loss function. Q3: Why use shallow trees (max_depth=2-4) in gradient boosting?
Boosting relies on weak learners — models slightly better than random. Deep trees would overfit the residuals of each step, making the ensemble unstable. Shallow trees ensure each tree captures just a bit of the remaining pattern, building up complexity gradually. Q4: What does the learning rate do in gradient boosting?
It shrinks each tree's contribution. Learning rate = 1.0 means each tree fully corrects residuals. Learning rate = 0.1 means each tree corrects only 10% — so you need more trees but get a smoother, more robust model. Lower learning rate + more trees = better generalization. Join Discord PreviousBagging & Random ForestNextSupport Vector Machines
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.