v9.1.1
Menu

Gradient Boosting Residual Step

hardFREEEnsemble MethodsMachine Learning 1

Implement one step of Gradient Boosting for regression.

Given current predictions y^\hat{y} and true values yy:

  1. Compute residuals: ri=yi−y^ir_i = y_i - \hat{y}_i
  2. Fit a simple model to the residuals: compute the mean residual for each group defined by a feature threshold. Given the split threshold, predict the mean residual for left (≤\leq threshold) and right (>> threshold) groups.
  3. Update predictions: y^i′=y^i+η⋅h(xi)\hat{y}_i' = \hat{y}_i + \eta \cdot h(x_i) where η\eta is the learning rate and h(xi)h(x_i) is the predicted residual.

Return the updated predictions, rounded to 4 decimal places.

Example

Input
y_true = [3, 6, 4, 8]
y_pred = [2, 5, 3, 7]
feature_values = [1, 3, 2, 4]
threshold = 2.5
learning_rate = 0.1
Output
[2.1, 5.1, 3.1, 7.1]
Reasoning
  • Inputs: four samples with targets y=[3,6,4,8]y = [3, 6, 4, 8] and current predictions y^=[2,5,3,7]\hat{y} = [2, 5, 3, 7]. The feature x=[1,3,2,4]x = [1, 3, 2, 4] only picks each sample's side of the split; η=0.1\eta = 0.1 scales the correction.
  • Residuals: r=[3−2, 6−5, 4−3, 8−7]=[1,1,1,1]r = [3-2,\ 6-5,\ 4-3,\ 8-7] = [1, 1, 1, 1], so every prediction is exactly 1 too low.
  • Split at 2.5: samples 1 and 3 (x=1,2x = 1, 2) fall left; samples 2 and 4 (x=3,4x = 3, 4) fall right. Sides follow the feature value, not list position. Left mean =(1+1)/2=1= (1+1)/2 = 1, right mean =(1+1)/2=1= (1+1)/2 = 1, so h(xi)=1h(x_i) = 1 for everyone.
  • Update: y^i′=y^i+0.1⋅1\hat{y}_i' = \hat{y}_i + 0.1 \cdot 1, giving [2.1, 5.1, 3.1, 7.1].
  • Notice: only 10% of the gap is closed, so each new residual is 0.90.9. With unequal residuals the two sides would get different corrections, which is what the split is for.

Constraints

  • y_true, y_pred: lists of actual and current predicted values
  • feature_values: list of feature values for splitting
  • threshold: split value
  • learning_rate: float > 0
  • Return list of updated predictions rounded to 4 decimal places

Learn this in

More from Machine Learning 1

solution.py

Test results

Sign in to run your code against the test cases.