Skip to Content
OchreML
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) and N6 histogram 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, consider HistGradientBoostingClassifier.

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

ParameterTypeDefaultDescription
lossstr"log_loss"Loss function to optimize ("log_loss" for binary and multiclass logistic deviance).
learning_ratefloat0.1Shrinkage factor applied to each tree's contribution.
n_estimatorsint100Number of boosting stages to perform.
subsamplefloat1.0Fraction of training samples used to fit each individual base learner (< 1.0 enables Stochastic Gradient Boosting).
min_samples_splitint2Minimum number of samples required to split an internal node.
min_samples_leafint1Minimum number of samples required at a leaf node.
max_depthint3Maximum depth of each individual regression tree base learner.
random_stateint | NoneNoneRandom seed controlling subsampling and tree randomness at each stage.
devicestr | NoneNoneExecution device ("auto", "cpu", "cuda:0", "wgpu", "tpu"). Defaults to ochreml.get_default_device().

Attributes

AttributeTypeDescription
classes_np.ndarray | None1D float64 array of unique class labels discovered during fit.
feature_importances_np.ndarray | NoneNormalized impurity-based feature importances of shape (n_features,).
n_features_in_int | NoneNumber of features seen during fit.
device_str | NoneResolved hardware device string used by the underlying Rust estimator.

Methods

MethodSignatureDescription
fitfit(X, y, sample_weight=None) -> GradientBoostingClassifierFit the gradient boosting classifier on (X, y) with optional sample weights.
predictpredict(X) -> np.ndarrayPredict class labels for samples in X.
predict_probapredict_proba(X) -> np.ndarrayPredict class probabilities of shape (n_samples, n_classes).
decision_functiondecision_function(X) -> np.ndarrayCompute raw log-odds decision scores of shape (n_samples,) for binary or (n_samples, n_classes) for multiclass.
scorescore(X, y) -> floatReturn mean classification accuracy on (X, y).
savesave(path: str) -> NoneSave the fitted boosting model to a binary .dit file.
loadGradientBoostingClassifier.load(path: str, device: str | None = None)Load a fitted classifier from a .dit file.
toto(device: str) -> GradientBoostingClassifierMove 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]))

On this page