Practice in Lab: Fit residual trees in a tiny gradient-boosted regressor →
Fit residual trees in a tiny gradient-boosted regressor
Gradient boosting adds weak models that reduce the current residual error. For squared loss, fitting each new stump to target minus current prediction follows the negative gradient direction. Shrinkage scales each update and changes how many rounds are useful.
The exercise fits one-dimensional decision stumps by exhaustive split search. This is a real local training algorithm, with a constant baseline and reproducible tie handling, not a wrapper around supplied predictions. It intentionally omits large-scale histogram algorithms, missing-value routing and early stopping. Select rounds and learning rate on validation data, never by improving final test error.
target=[0.,0.,2.,2.]
base=sum(target)/len(target)
residual=[y-base for y in target]
print(base,residual)Review the design
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.
Your task
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.
The explanation opens here once every check passes.