Trees, Ensembles & Boosting
GradientBoostingClassifier
Stagewise gradient boosted decision trees for binary and multiclass classification with log-loss optimization.
GradientBoostingClassifier builds an additive ensemble of shallow regression trees in a forward stagewise fashion, fitting each new tree to the negative gradient of the binomial or multinomial deviance (log_loss). (^▽^)
Backend & Hardware Acceleration
- Sequential Boosting with Parallel Split Search: Boosting stages are inherently sequential, while feature split evaluation, negative-gradient computation, and leaf updates run in Rust with Rayon parallelism.
- Accelerator Integration: Uses OchreML's
N2(elementwise/softmax) andN6histogram primitives where beneficial, with automatic CPU Rayon execution for branch-heavy tree stages and TPU fallback. For large datasets where full GPU histogram acceleration is desired, considerHistGradientBoostingClassifier.
Import & Signature
from ochreml.ensemble import GradientBoostingClassifier
# or: from ochreml import GradientBoostingClassifier
GradientBoostingClassifier(
loss: str = "log_loss",
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,
random_state: Optional[int] = None,
device: Optional[str] = None,
)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
loss | str | "log_loss" | Loss function to optimize ("log_loss" for binary and multiclass logistic deviance). |
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 training samples used to fit each individual 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 base learner. |
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 |
|---|---|---|
classes_ | np.ndarray | None | 1D float64 array of unique class labels discovered during fit. |
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) -> GradientBoostingClassifier | Fit the gradient boosting classifier on (X, y) with optional sample weights. |
predict | predict(X) -> np.ndarray | Predict class labels for samples in X. |
predict_proba | predict_proba(X) -> np.ndarray | Predict class probabilities of shape (n_samples, n_classes). |
decision_function | decision_function(X) -> np.ndarray | Compute raw log-odds decision scores of shape (n_samples,) for binary or (n_samples, n_classes) for multiclass. |
score | score(X, y) -> float | Return mean classification accuracy on (X, y). |
save | save(path: str) -> None | Save the fitted boosting model to a binary .dit file. |
load | GradientBoostingClassifier.load(path: str, device: str | None = None) | Load a fitted classifier from a .dit file. |
to | to(device: str) -> GradientBoostingClassifier | Move the estimator execution target to device. |
Example
import numpy as np
from ochreml.ensemble import GradientBoostingClassifier
rng = np.random.default_rng(42)
X0 = rng.normal(loc=[-1.2, 0.0], scale=0.6, size=(60, 2))
X1 = rng.normal(loc=[1.2, 0.0], scale=0.6, size=(60, 2))
X = np.vstack([X0, X1])
y = np.array([0.0] * 60 + [1.0] * 60)
gb = GradientBoostingClassifier(
n_estimators=50,
learning_rate=0.1,
max_depth=3,
subsample=0.8,
random_state=42,
device="auto",
)
gb.fit(X, y)
print("Accuracy:", gb.score(X, y))
print("Decision scores (first 4):", gb.decision_function(X[:4]))
print("Probabilities (first 4):\n", gb.predict_proba(X[:4]))