Machine Learning Lab / Supervised Learning / Gradient Boosting
Ensemble Learning
Algorithm 08 · Supervised learning

Gradient Boosting

Gradient Boosting builds a strong model by adding small models one after another. Each new model tries to fix some of the errors made by the models already built.

Think of it like improving an answer after getting feedback. Each round learns from what is still going wrong.

Many modelsCombined together
Step by stepCorrects earlier errors
FlexibleClassification and regression

What is Gradient Boosting?

It is an ensemble learning method. It combines many small prediction models, often shallow decision trees, into one stronger model.

Unlike Random Forest, where many trees can be built independently, boosting adds models in sequence. A later model learns from the current model's mistakes.

Why is it called boosting?

Each new learner adds strength to the group. A weak learner may be only a little better than a simple guess, but many carefully added learners can make a strong predictor.

“Gradient” refers to using the direction of improvement in a loss function to reduce prediction error.

Interactive playground

Watch a model improve in rounds

This is a simplified teaching simulation. It shows how prediction error can shrink as rounds are added. It is not a trained gradient-boosting library model.

Add more rounds or change the learning rate to explore the simulation.

A smaller learning rate makes each step gentler. It often needs more rounds. More rounds do not always mean a better model: too many can overfit.

How does it work?

  1. Start with a simple prediction.
  2. Measure how wrong the prediction is.
  3. Train a small model to reduce the current errors.
  4. Add that model's contribution to the overall prediction.
  5. Repeat for more rounds.

What is a weak learner?

A weak learner is a simple model that is not very powerful on its own. In gradient boosting, it is often a small decision tree called a decision stump or shallow tree.

Many small corrections can combine into a much stronger model.

Gradient Boosting vs Random Forest

Random ForestGradient Boosting
Builds many trees independently, often in parallel.Builds trees one after another.
Uses averaging or voting to combine trees.Adds each new tree to improve the current model.
Usually less sensitive to tuning.Can be very accurate but often needs careful tuning.
Uses bootstrap samples and random feature choices in common implementations.Focuses each new learner on reducing the current loss.

The loss function

A loss function measures how wrong a model's predictions are.

Loss = measure of prediction error

For regression, one common choice is mean squared error. For classification, a common choice is log loss.

The update idea

Each new tree moves the current model in a direction that reduces the loss.

Fₘ(x) = Fₘ₋₁(x) + η × hₘ(x)

F is the combined model, h is the new small model, and η (eta) is the learning rate.

What does “gradient” mean?

A gradient tells us the direction in which a value changes fastest. Gradient boosting uses the negative gradient of the loss to decide what the next learner should try to correct.

Next learner ≈ fit the negative gradient of the loss

For squared-error regression, this is closely related to fitting the residuals: actual value minus current prediction.

Worked numerical example

Suppose we want to predict a house price. The actual price is ₹50 lakh. Our first simple model predicts ₹40 lakh.

Residual = Actual − Prediction
Residual = 50 − 40 = +10 lakh

A small new tree predicts a correction of +8 lakh for this type of house. If the learning rate is 0.5, we only add half of that correction.

New prediction = Old prediction + (Learning rate × Correction)
= 40 + (0.5 × 8)
= ₹44 lakh

The error has fallen from ₹10 lakh to ₹6 lakh. More rounds may improve the prediction further, but the model must be checked on unseen data.

Popular boosting methods

  • Gradient Boosting: the core sequential method.
  • XGBoost: an optimised implementation with regularisation and efficient training features.
  • LightGBM: a fast gradient-boosting framework designed to handle large datasets efficiently.
  • CatBoost: a boosting library with strong support for categorical features.

Important settings

  • n_estimators: number of boosting stages.
  • learning_rate: size of each model's contribution.
  • max_depth: depth of each small tree.
  • subsample: fraction of training rows used for a stage in supported implementations.
  • loss: the error measure being minimised.

How do we evaluate it?

Use a separate test set or cross-validation.

  • Regression: MAE, MSE, RMSE, and R².
  • Classification: accuracy, precision, recall, F1-score, and ROC-AUC.
  • Compare training and validation results to spot overfitting.

Strengths and limits

Strengths

  • Can model complex patterns.
  • Often performs very well on structured table data.
  • Can be used for both regression and classification.

Limits

  • Training is sequential and can take time.
  • Many settings need careful tuning.
  • Too many rounds or deep trees can overfit.
  • Less simple to explain than one small tree.

Python implementation

This example trains a real Gradient Boosting classifier with scikit-learn.

from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import accuracy_score, classification_report

X, y = make_moons(n_samples=600, noise=0.25, random_state=42)

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.25, random_state=42, stratify=y
)

model = GradientBoostingClassifier(
    n_estimators=100,
    learning_rate=0.1,
    max_depth=2,
    random_state=42
)

model.fit(X_train, y_train)
predictions = model.predict(X_test)

print("Accuracy:", accuracy_score(y_test, predictions))
print(classification_report(y_test, predictions))

Install scikit-learn first if needed: pip install scikit-learn.

Where is Gradient Boosting used?

Credit risk

Predicting the chance that a loan may not be repaid.

Sales forecasting

Estimating future sales from past patterns.

Customer behaviour

Predicting which customers may leave or respond to an offer.

Fraud detection

Finding transactions that look unusual based on learned patterns.

Knowledge check

Test what you learned

Answer one question at a time. Submit your answers on the final question to see your score and explanations.

0 of 10 answered