Gradient Boosting Explained With a Delivery-Time Example

Delivery route maps and a green stopwatch on a sunlit dispatch desk.

A dispatch team predicts 45 minutes for every delivery. That estimate is reasonable on average, but it overestimates short routes and underestimates long ones. Gradient boosting starts with a simple prediction like this, then adds small models that correct patterns in the remaining errors.

Most gradient boosting implementations use decision trees for those corrections. The prediction is the starting value plus the scaled contributions of the fitted trees. The loss function defines what counts as an improvement.

Start with a prediction you can beat

Suppose four training deliveries took 30, 40, 50, and 60 minutes. With squared-error loss, the best constant prediction is their mean, 45 minutes. Subtract 45 from each observed delivery time to get its residual. A negative residual means the estimate was too high.

RouteActual timeStarting estimateResidual
Short A30 min45 min−15 min
Short B40 min45 min−5 min
Long A50 min45 min+5 min
Long B60 min45 min+15 min

Let one tree make the first correction

Imagine a one-split tree that separates the routes into short and long groups. The average residual is −10 minutes on short routes and +10 minutes on long routes. Those averages become the two leaf values for this squared-error example.

Use a learning rate of 0.5 to apply half of each correction. Short routes move from 45 to 40 minutes. Long routes move from 45 to 50 minutes. The mean squared error on these four training deliveries falls from 125 to 50 square minutes.

At a learning rate of 0.5, the first tree moves a 45-minute estimate down to 40 minutes for short routes and up to 50 minutes for long routes.
The first tree shifts the two groups in opposite directions by five minutes.

The second tree trains on the updated residuals: −10, 0, 0, and +10 minutes. If it makes the same split, its leaf values are −5 and +5. After scaling, the predictions become 37.5 minutes for short routes and 52.5 minutes for long routes. With more features, the next tree may choose a different split.

Why the loss function changes the correction

With squared error, the negative gradient is proportional to the ordinary residual. More generally, each round uses the direction in which changing the current predictions would reduce the loss. The exact leaf update can also depend on the loss.

For a yes-or-no question such as whether a delivery will be late, a boosting classifier can optimize log loss. The trees add to an underlying score, which the model can convert to a probability. Scikit-learn’s gradient boosting reference.

How to choose a learning rate and stop adding trees

A smaller learning rate makes each tree contribute less, so the model typically needs more trees. Tree depth determines how detailed a single correction can be. Try shallow trees first, then compare validation scores as you increase depth or the number of iterations.

Early stopping monitors a validation metric and ends training when additional rounds stop improving it enough for a chosen number of rounds. The validation data should match the intended use. For delivery estimates, use later dates to check how well the model handles new orders. How to choose a cross-validation split.

Side-by-side training and validation error plots compare gradient boosting with and without early stopping.
Training and validation errors across boosting iterations in a California housing example. Scikit-learn developers.

Keep a separate test set for the final report. Measure a quantity your team can interpret, such as mean absolute error in minutes, and check errors on long routes as well as the overall average. If training error keeps falling while validation error rises, inspect the split and the model’s complexity. Signs of overfitting in machine learning.

Gradient boosting vs. random forest

A random forest fits trees on randomized samples and combines their predictions, typically by averaging for regression. A gradient boosting model builds an additive sequence in which each stage depends on the current predictions. Compare both on the same held-out data. Training speed and accuracy depend on the implementation, settings, and dataset. How random forests combine trees.

Common questions

Is XGBoost the same as gradient boosting?

XGBoost is a library that implements gradient boosting and provides options for regularization, efficient fitting, and other training choices. Gradient boosting is the broader method.

Can adding a tree make test performance worse?

Yes. A later tree may improve the training fit while learning an error pattern that does not recur on new data. Use validation to choose how many rounds to keep, then evaluate once on separate test data.

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top