Gradient Boosting Regressor From Scratch

Quick Overview

Implement a gradient boosting regressor with squared-error loss on top of scikit-learn regression trees, with staged predictions and a per-round training error. It tests whether you understand residual fitting, shrinkage, the starting constant and matching a library implementation exactly.

Gradient Boosting Regressor From Scratch

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement a `GradientBoostingRegressor` class with squared-error loss. `fit(X, y)` starts from the target mean, then for each of `n_estimators` rounds fits a regression tree to the residuals `y - F` and adds `learning_rate * tree.predict(X)` to the running prediction `F`. The class takes `n_estimators`, `learning_rate`, `max_depth` and `random_state` in its constructor and must meet these requirements: - `fit` stores `init_` (the mean of `y` as a float) and `estimators_` (the list of fitted trees), and returns `self`. `n_estimators=0` must work. - Each tree is `DecisionTreeRegressor(criterion="squared_error", max_depth=self.max_depth, random_state=self.random_state)`. - `train_score_` is a NumPy array of shape `(n_estimators,)` holding the training MSE after each round. - `predict(X)` returns `init_ + learning_rate * Σ tree.predict(X)` as a 1-D array. - `staged_predict(X)` yields a copy of the prediction after each tree, so the last value it yields equals `predict(X)`. Results must match scikit-learn's `GradientBoostingRegressor` trained with the same settings. Example: ```python X = [[0.], [1.], [2.], [3.]] y = [1., 1., 3., 5.] m = GradientBoostingRegressor(n_estimators=1, learning_rate=0.5, max_depth=1).fit(X, y) m.predict(X) # [1.75, 1.75, 3.25, 3.25] (init 2.5, stump splits at 1.5) ``` ```hint One running array Keep a single array of current predictions for the training rows. The residuals for the next tree and the training MSE for `train_score_` both come from it, so you never need to re-run the earlier trees during `fit`. ``` ```hint Edge cases to test Check what `train_score_`, `predict` and `staged_predict` return when the loop never runs, and what a caller would see if `staged_predict` handed back the same array object every time. ``` ### Constraints & Assumptions - `X` is array-like with shape `(n_samples, n_features)` and `y` is array-like with `n_samples` values. - `sklearn.tree.DecisionTreeRegressor` is provided as the base learner. - Subsampling and other losses are out of scope. ### What a Strong Answer Covers - Why the residual `y - F` is the negative gradient of squared error, and why the mean of `y` is the right starting constant. - A fit loop that adds the shrunken tree output to the running prediction and records the training MSE after each round. - `predict` and `staged_predict` that agree with each other and with the updates made during `fit`, including the `n_estimators=0` case. - Correct shapes and types: a 1-D prediction array, a float `init_`, and `train_score_` of shape `(n_estimators,)`. - The cost of fitting and predicting in terms of trees, depth, features and samples. ### Follow-up Questions - How would you add early stopping? - What changes for absolute-error loss? - How do `learning_rate` and `n_estimators` interact, and how would you tune them together?

Overview: Implement a gradient boosting regressor with squared-error loss on top of scikit-learn regression trees, with staged predictions and a per-round training error. It tests whether you understand residual fitting, shrinkage, the starting constant and matching a library implementation exactly.

PracHub logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement a GradientBoostingRegressor class with squared-error loss. fit(X, y) starts from the target mean, then for each of n_estimators rounds fits a regression tree to the residuals y - F and adds learning_rate * tree.predict(X) to the running prediction F.

The class takes n_estimators, learning_rate, max_depth and random_state in its constructor and must meet these requirements:

  • fit stores init_ (the mean of y as a float) and estimators_ (the list of fitted trees), and returns self . n_estimators=0 must work.
  • Each tree is DecisionTreeRegressor(criterion="squared_error", max_depth=self.max_depth, random_state=self.random_state) .
  • train_score_ is a NumPy array of shape (n_estimators,) holding the training MSE after each round.
  • predict(X) returns init_ + learning_rate * Σ tree.predict(X) as a 1-D array.
  • staged_predict(X) yields a copy of the prediction after each tree, so the last value it yields equals predict(X) .

Results must match scikit-learn's GradientBoostingRegressor trained with the same settings.

Example:

X = [[0.], [1.], [2.], [3.]]
y = [1., 1., 3., 5.]
m = GradientBoostingRegressor(n_estimators=1, learning_rate=0.5, max_depth=1).fit(X, y)
m.predict(X)  # [1.75, 1.75, 3.25, 3.25]  (init 2.5, stump splits at 1.5)

Constraints & Assumptions

  • X is array-like with shape (n_samples, n_features) and y is array-like with n_samples values.
  • sklearn.tree.DecisionTreeRegressor is provided as the base learner.
  • Subsampling and other losses are out of scope.

What a Strong Answer Covers Guidance

  • Why the residual y - F is the negative gradient of squared error, and why the mean of y is the right starting constant.
  • A fit loop that adds the shrunken tree output to the running prediction and records the training MSE after each round.
  • predict and staged_predict that agree with each other and with the updates made during fit , including the n_estimators=0 case.
  • Correct shapes and types: a 1-D prediction array, a float init_ , and train_score_ of shape (n_estimators,) .
  • The cost of fitting and predicting in terms of trees, depth, features and samples.

Follow-up Questions Guidance

  • How would you add early stopping?
  • What changes for absolute-error loss?
  • How do learning_rate and n_estimators interact, and how would you tune them together?