Skip to main content

HNBM

License: MIT scikit-learn

Heterogeneous Newton Boosting Machine (HNBM) — a scikit-learn-compatible gradient boosting framework that stochastically mixes heterogeneous base learners at each iteration.

Unlike standard gradient boosting libraries that use a single learner type (typically decision trees), HNBM lets you define a pool of base learners with selection probabilities. At each boosting round, a learner is drawn from that pool and fit to the Newton step (gradient divided by Hessian, weighted by the Hessian).

This is the core framework behind SnapBoost, inspired by SnapBoost: A Heterogeneous Boosting Machine (Parnell et al., NeurIPS 2020).


Table of Contents


Installation

From PyPI:

pip install hnbm

From source:

git clone https://github.com/qiancapital-dev/hnbm.git
cd hnbm
pip install .

Requirements: Python ≥ 3.8, NumPy, scikit-learn, tqdm.


Quick Start

Subclass HNBMClassifier or HNBMRegressor and configure your base learner pool before training:

Classification

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor
from hnbm import HNBMClassifier

X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)


class TreeClassifier(HNBMClassifier):
    def __init__(self, max_depth=5, **kwargs):
        super().__init__(**kwargs)
        self.base_learners_ = [DecisionTreeRegressor(max_depth=max_depth)]
        self.probabilities_ = [1.0]


model = TreeClassifier(
    num_iterations=100,
    learning_rate=0.1,
    random_state=42,
)
model.fit(X_train, y_train)

print("Accuracy:", model.score(X_test, y_test))
print("Probabilities shape:", model.predict_proba(X_test).shape)  # (n_samples, 2)
model.evaluate(X_test, y_test)  # prints log loss

Regression

from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor
from hnbm import HNBMRegressor

X, y = load_diabetes(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)


class TreeRegressor(HNBMRegressor):
    def __init__(self, max_depth=5, **kwargs):
        super().__init__(**kwargs)
        self.base_learners_ = [DecisionTreeRegressor(max_depth=max_depth)]
        self.probabilities_ = [1.0]


model = TreeRegressor(
    num_iterations=100,
    learning_rate=0.1,
    random_state=42,
)
model.fit(X_train, y_train)

print("R²:", model.score(X_test, y_test))
model.evaluate(X_test, y_test)  # prints RMSE

API Reference

HNBMClassifier / HNBMRegressor

The recommended entry points (similar to XGBClassifier / XGBRegressor). Subclass one of these and set base_learners_ (list of unfitted sklearn regressors) and probabilities_ (list summing to 1) before calling fit.

Methods

Method Classifier Regressor Description
fit(X, y) Train the ensemble
predict(X) Class labels (0/1) or continuous values
predict_proba(X) Probabilities, shape (n_samples, 2)
decision_function(X) Raw logits
score(X, y) Accuracy or R²
evaluate(X, y) Prints and returns log loss or RMSE

HNBM

Legacy base class that accepts a mode parameter ("classification" or "regression"). Prefer HNBMClassifier or HNBMRegressor for new code.

from sklearn.tree import DecisionTreeRegressor
from hnbm import HNBM


class TreeBoost(HNBM):
    def __init__(self, max_depth=5, **kwargs):
        super().__init__(**kwargs)
        self.base_learners_ = [DecisionTreeRegressor(max_depth=max_depth)]
        self.probabilities_ = [1.0]


model = TreeBoost(
    num_iterations=100,
    learning_rate=0.1,
    mode="classification",  # or "regression"
    random_state=42,
)
model.fit(X_train, y_train)

Loss functions

hnbm.losses provides Logistic (classification) and MeanSquaredError (regression), each with a compute_derivatives(y, f) method returning gradient and Hessian vectors.


Parameters

Shared (HNBMClassifier / HNBMRegressor)

Parameter Type Default Description
num_iterations int 100 Number of boosting rounds
learning_rate float 0.1 Shrinkage per learner
random_state int or None None Seed for learner selection
verbose bool True Show tqdm progress bar

The legacy HNBM class also accepts a mode parameter ("classification" or "regression").

Label conventions (classification): accepts 0/1 or -1/+1. Predictions are returned as 0/1.


Docker

docker build -t hnbm .
docker run --rm hnbm

Development

git clone https://github.com/qiancapital-dev/hnbm.git
cd hnbm
pip install -r requirements.txt
pip install -e .

Related projects

  • snapboost — a concrete HNBM using decision trees and RFF ridge regressors

License

MIT — See LICENSE for full text.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hnbm-0.1.1.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hnbm-0.1.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file hnbm-0.1.1.tar.gz.

File metadata

  • Download URL: hnbm-0.1.1.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for hnbm-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7d980e06cc3d6e7f18b79527b95fe158072f23f487026df9b436267fd6b8df9e
MD5 433fbca10932bec812288f42f6818fea
BLAKE2b-256 06268f15c7a348eb09996285f14219d7f970179c9d3dfcaad44de8623a62f000

See more details on using hashes here.

Provenance

The following attestation bundles were made for hnbm-0.1.1.tar.gz:

Publisher: python-publish.yml on QianCapital/hnbm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hnbm-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: hnbm-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for hnbm-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c1a71e456dae2e3361f9b67c2866c792ec8a0bbe1f7e12d030bf0a2e34d32ef4
MD5 871d2b0dbeebedc0af017033a706659a
BLAKE2b-256 ee1a1e1510c0b0e82a1a4ac60bea36003a6c548ef1a61db7d556dfa59c5d05f6

See more details on using hashes here.

Provenance

The following attestation bundles were made for hnbm-0.1.1-py3-none-any.whl:

Publisher: python-publish.yml on QianCapital/hnbm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page