Gradient Boosted Trees (GBT): A Complete Deep Dive
Gradient Boosted Trees are one of the most powerful and widely used machine learning techniques today. If you've ever worked with models like XGBoost or LightGBM, you've already used this concept.
๐ Table of Contents
- Introduction
- What is GBT?
- Step-by-Step Example
- Mathematics
- Code Example
- CLI Output
- Key Insights
- Conclusion
Introduction
Instead of building one strong model, Gradient Boosting builds many small weak models (decision trees) and combines them.
What is Gradient Boosting?
Gradient Boosting is an iterative method where each new model learns the residual errors of the previous model.
๐ Expand: What is a Residual?
Residual = Actual − Predicted It tells us how wrong the model is.
Step-by-Step Example
Step 1: Initial Prediction
We start with a simple prediction — the mean:
\[ \hat{y} = \frac{1}{n} \sum y_i \]
If average house price = 300,000 → prediction = 300,000
Step 2: Residuals
\[ r_i = y_i - \hat{y} \]
- House A: 50,000
- House B: -20,000
- House C: 10,000
Step 3: Train Tree
Train tree on residuals (not actual values).
Step 4: Update Prediction
\[ \hat{y}^{(t+1)} = \hat{y}^{(t)} + \eta f_t(x) \]
Where:
- \(\eta\) = learning rate
- \(f_t(x)\) = tree prediction
Step 5: Repeat
Continue until error is minimized.
Mathematics Behind GBT
1. Mean Squared Error
\[ MSE = \frac{1}{n} \sum (y_i - \hat{y}_i)^2 \]
This penalizes large errors more heavily.
2. Gradient Descent Idea
\[ \text{Gradient} = \frac{\partial L}{\partial \hat{y}} \]
We move in direction of negative gradient.
3. Residual = Negative Gradient
\[ r_i = - \frac{\partial L}{\partial \hat{y}_i} \]
This is why it's called Gradient Boosting.
4. Final Model
\[ F(x) = \sum_{t=1}^{T} \eta f_t(x) \]
๐ Expand: Why Learning Rate Matters
Small learning rate → slow but accurate learning Large learning rate → fast but risk of overfitting
Code Example
from sklearn.ensemble import GradientBoostingRegressor
model = GradientBoostingRegressor(
n_estimators=100,
learning_rate=0.1,
max_depth=3
)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
print(predictions)
CLI Output
$ python train_gbt.py Training started... Iteration 1: Loss = 12000 Iteration 10: Loss = 4500 Iteration 50: Loss = 1200 Iteration 100: Loss = 300 Training complete!
๐ฏ Key Takeaways
- GBT builds trees sequentially
- Each tree learns residuals
- Learning rate controls updates
- Highly accurate and flexible
Conclusion
Gradient Boosted Trees are powerful because they learn from mistakes. Instead of trying to build a perfect model in one go, they gradually improve step by step.
Once you understand residuals, loss functions, and learning rates, the algorithm becomes intuitive and incredibly useful.
No comments:
Post a Comment