Trees, Ensembles & Boosting
GradientBoostingRegressor
Stagewise gradient boosted regression trees supporting squared_error, absolute_error, and Huber losses with staged_predict.
GradientBoostingRegressor builds an additive sequence of shallow regression trees, each trained on the negative gradient (pseudo-residuals) of a differentiable regression loss (squared_error, absolute_error, or huber). (^▽^)
Backend & Hardware Acceleration
- CPU Rayon Parallelism: Boosting iterations execute sequentially while tree split finding, residual updates, and predictions are parallelized across CPU threads with Rust Rayon.
- Accelerator Notes: Uses OchreML's
N2/N6primitives where applicable and runs branch-heavy tree stages on CPU Rayon (including automatic CPU Rayon fallback on TPU). For large tabular datasets that benefit from fullu8-binned GPU histogram acceleration, seeHistGradientBoostingRegressor.
Import & Signature
from ochreml.ensemble import GradientBoostingRegressor
# or: from ochreml import GradientBoostingRegressor
GradientBoostingRegressor(
loss: str = "squared_error",
learning_rate: float = 0.1,
n_estimators: int = 100,
subsample: float = 1.0,
min_samples_split: int = 2,
min_samples_leaf: int = 1,
max_depth: int = 3,
alpha: float = 0.9,
random_state: Optional[int] = None,
device: Optional[str] = None,
)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
loss | str | "squared_error" | Loss function to optimize: "squared_error" (L2), "absolute_error" (L1), or "huber" (hybrid L2/L1). |
learning_rate | float | 0.1 | Shrinkage factor applied to each tree's contribution. |
n_estimators | int | 100 | Number of boosting stages to perform. |
subsample | float | 1.0 | Fraction of samples used to fit each base learner (< 1.0 enables Stochastic Gradient Boosting). |
min_samples_split | int | 2 | Minimum number of samples required to split an internal node. |
min_samples_leaf | int | 1 | Minimum number of samples required at a leaf node. |
max_depth | int | 3 | Maximum depth of each individual regression tree. |
alpha | float | 0.9 | Quantile threshold used by the "huber" loss function. |
random_state | int | None | None | Random seed controlling subsampling and tree randomness at each stage. |
device | str | None | None | Execution device ("auto", "cpu", "cuda:0", "wgpu", "tpu"). Defaults to ochreml.get_default_device(). |
Attributes
| Attribute | Type | Description |
|---|---|---|
feature_importances_ | np.ndarray | None | Normalized impurity-based feature importances of shape (n_features,). |
n_features_in_ | int | None | Number of features seen during fit. |
device_ | str | None | Resolved hardware device string used by the underlying Rust estimator. |
Methods
| Method | Signature | Description |
|---|---|---|
fit | fit(X, y, sample_weight=None) -> GradientBoostingRegressor | Fit the gradient boosting regressor on (X, y) with optional sample weights. |
predict | predict(X) -> np.ndarray | Predict continuous target values for samples in X. |
staged_predict | staged_predict(X) -> Iterator[np.ndarray] | Yield predicted target arrays after each boosting stage 1..n_estimators for monitoring convergence. |
score | score(X, y) -> float | Return the coefficient of determination R^2 on (X, y). |
save | save(path: str) -> None | Save the fitted boosting model to a binary .dit file. |
load | GradientBoostingRegressor.load(path: str, device: str | None = None) | Load a fitted regressor from a .dit file. |
to | to(device: str) -> GradientBoostingRegressor | Move the estimator execution target to device. |
Example
import numpy as np
from ochreml.ensemble import GradientBoostingRegressor
rng = np.random.default_rng(10)
X = rng.uniform(-3.0, 3.0, size=(120, 2))
y = np.cos(X[:, 0]) + 0.5 * X[:, 1] ** 2
reg = GradientBoostingRegressor(
loss="huber",
alpha=0.9,
n_estimators=60,
learning_rate=0.1,
max_depth=3,
random_state=10,
device="auto",
)
reg.fit(X, y)
staged = list(reg.staged_predict(X[:3]))
print("R^2 score:", reg.score(X, y))
print("Stages yielded:", len(staged))
print("Final predictions (first 3):", staged[-1])