WRITE · TRACE · UNDERSTAND

Fit residual trees in a tiny gradient-boosted regressor

Implement fit_boost(x,y,rounds,rate) and predict_boost(model,x).

Read lesson: Fit residual trees in a tiny gradient-boosted regressor →

Your experiment

Implement fit_boost(x,y,rounds,rate) and predict_boost(model,x). Fit a mean baseline, then squared-error optimal residual stumps at midpoints of distinct x; ties choose smallest cut. Model is dict base, rate, stumps (cut,left,right). Constant x uses a constant residual stump with cut=inf. Validate nonempty equal finite data, rounds>=0 integer, 0<rate<=1.

Python runs in your browser. Your draft stays on this device.

What to look for

A trainable baseline makes every later gain measurable. This tiny learner demonstrates gradient boosting correctly, while production libraries add numerical, categorical, performance and distributed engineering that must be evaluated separately.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Fit residual trees in a tiny gradient-boosted regressor works

The idea

Gradient boosting builds a strong model by adding up many weak ones, each trained to fix what the sum so far gets wrong. fit_boost starts with the mean of y. Each round it computes the residuals, fits a stump to them (one cut, with a value for each side), and adds rate times that stump to the predictions.

For squared error, the residual is the negative gradient of the loss with respect to the current prediction. Fitting a tree to residuals is therefore a gradient descent step taken in prediction space, with the tree as the step direction. The rate, between 0 and 1, makes each step partial, so later rounds can correct earlier ones.

How it works

  1. Set base to the mean of y; every prediction starts there.
  2. Each round, compute the residuals y - prediction.
  3. For each midpoint between distinct x values, split the residuals at x <= cut and take each side's mean as its leaf value.
  4. Keep the cut with the lowest squared error; ties go to the smallest cut.
  5. Store (cut, left, right) and add rate times the stump's output to every prediction.
  6. predict_boost returns base plus rate times the sum of each stump's leaf value for the input.

What it costs

Fit
O(rounds·n²) worst case
Predict
O(stumps) per value
Memory
O(n + rounds)

Each round tries up to n - 1 cuts and rescans all n residuals for each one; prediction walks every stored stump once per input value.

Where it is used

  • XGBoost, LightGBM and CatBoost are gradient-boosted tree libraries widely used on tabular data.
  • scikit-learn's GradientBoostingRegressor follows this recipe; its learning_rate is the Lab's rate.
  • LambdaMART, a ranking method built on boosted regression trees, won the 2010 Yahoo Learning to Rank Challenge.

Watch out for

  • Fitting each stump to y instead of the residuals, so every round learns the same thing.
  • Applying rate when updating training predictions but not in predict_boost, or the reverse, so the two disagree.
  • Running many rounds with a high rate: the model starts fitting noise in the training data.