#248Gradient Boosting: One Round (Stump)Medium

Gradient Boosting: One Round (Stump)

Background

Gradient Boosting fits a sequence of weak learners on the pseudo-residuals of the running prediction. For mean-squared-error regression, the pseudo-residual is simply yF(x)y - F(x) — fit a stump to the residuals, scale by the learning rate, add to FF. One round.

Problem statement

Implement gbm_one_round(X, y, F, lr=0.1) for regression. Steps:

  1. Compute residuals r=yFr = y - F.
  2. Find the best single-threshold split on X[:, 0] (the only feature) that minimises the sum of squared residuals across the two halves.
  3. For each side, predict the mean of r on that side.
  4. Update FF+lrstump(X)F \leftarrow F + \text{lr} \cdot \text{stump}(X).

Input

  • Xnp.ndarray shape (n,1)(n, 1), single-feature input matrix.
  • y — 1-D array of length nn, target.
  • F — 1-D array of length nn, current model output.
  • lrfloat, learning rate (default 0.1).

Output

Returns np.ndarray of length nn, the new FF.

Examples

Example 1 — reduces MSE on a separable problem

Input:  X = bimodal feature, y = signs of X, F = zeros
Output: MSE(F_new) < MSE(F)

Example 2 — lr = 0 leaves F unchanged

Input:  any X, y, F
Output: F_new = F

Constraints

  • XX has shape (n,1)(n, 1).
  • Try every unique value in X[:, 0] as a candidate threshold; pick the one minimising squared residual.
  • Returns np.ndarray of length nn.

Notes

  • Why stumps. Depth-1 trees are the simplest possible weak learner; gradient boosting compensates for their weakness with many rounds.
  • Multi-feature extension. Production GBMs sweep all features, picking the best (feature, threshold) pair per round. This kernel isolates the per-feature mechanic.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Reference example: three-point single-threshold split
  • Sample: two-point split predicts exact values
  • Sample: four points, threshold-split step