Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 25

Gradient Boosting Regressor

Add small trees that predict the remaining error, so each new tree corrects the numeric forecast. One example is on this page.

Intermediate12 min
Gradient Boosting Regressor MahiDev

What You Will Learn

  • Idea
  • Example
  • When to use it

Idea

Gradient boosting starts with a simple prediction, often the average target. The next tree is trained on the residual, the gap between that prediction and the true number. A learning rate keeps each correction small so later trees can still help.

Example

python
import numpy as np
from sklearn.ensemble import GradientBoostingRegressor

x = np.array([1, 2, 3, 8, 9], dtype=float).reshape(-1, 1)
y = np.array([2.0, 2.2, 1.8, 9.0, 9.4])
model = GradientBoostingRegressor(
    n_estimators=20, max_depth=1, learning_rate=0.3, random_state=0
)
pred = model.fit(x, y).predict([[8]])[0]
print(f"predict: {pred:.2f}")

Output:

text
predict: 8.99

When to use it

  • Use it when a forest is close but you want each new tree to fix what is still wrong.
  • Keep trees shallow and the learning rate small.
  • Train with early stopping on a holdout so extra trees do not memorize noise.

Loading playground…