Boosting: AdaBoost, Gradient Boosting & XGBoost
667 words
3 min read
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:
- Initialize all sample weights wi=1/m
- For t=1,2,…,T:
- Train a weak learner ht(x) using current weights
- Compute weighted error ϵt=∑i:ht(xi)=yiwi/∑wi
- Compute learner weight αt=0.5ln((1−ϵt)/ϵt)
- Update sample weights: wi←wi⋅exp(−αtyiht(xi))
- Normalize weights to sum to 1
- Final prediction: y^=sign(∑αtht(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:
- Initialize F0(x)=yˉ (constant prediction)
- For t=1,2,…,T:
- Compute residuals: ri=yi−Ft−1(xi) (for MSE loss)
- Fit a tree ht(x) to predict ri
- Update: Ft(x)=Ft−1(x)+ν⋅ht(x) Where ν (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
| Parameter | Range | Effect |
|---|---|---|
| n_estimators | 50-1000 | More trees = better but risk overfitting |
| learning_rate | 0.01-0.3 | Lower = more trees needed, more robust |
| max_depth | 2-8 | Deeper trees = more complex, risk overfitting |
| subsample | 0.5-1.0 | Lower = more randomness, less overfitting |
| min_samples_leaf | 5-50 | Higher = simpler trees |
| reg_alpha (L1) | 0-10 | Feature selection |
| reg_lambda (L2) | 0-10 | Shrinkage |
📝 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