Skip to main content

GBRF — Gradient Boosted Random Forest

PyPI version Python 3.8+ License: MIT

A novel hybrid machine learning algorithm combining Random Forest and Gradient Boosting. At each boosting iteration, a full Random Forest ensemble learns the pseudo-residuals, providing superior variance reduction compared to standard Gradient Boosting.


What Makes GBRF Different?

Algorithm Variance Control Bias Reduction Overfitting Risk
Random Forest ✅ High (bagging) ❌ Low Low
Gradient Boosting ❌ Low ✅ High High
GBRF (Ours) ✅ High (RF per iteration) ✅ High (boosting) Moderate

The Innovation: Instead of a single decision tree learning residuals at each boosting step (standard GB), GBRF uses a Random Forest ensemble per iteration. This gives you:

  • Better generalization than Gradient Boosting alone
  • Faster convergence than Random Forest alone
  • Built-in early stopping via out-of-bag monitoring
  • Aggregated feature importance across all boosting rounds

Installation

pip install hybridgbrf

Requirements

  • Python >= 3.8
  • numpy >= 1.21.0
  • scikit-learn >= 1.0.0
  • pandas >= 1.3.0
  • matplotlib >= 3.4.0
  • joblib >= 1.1.0

Quick Start

Regression

from hybridgbrf import GRFRegressor
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split

# Create data
X, y = make_regression(n_samples=1000, n_features=20, noise=0.1, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Train GBRF
model = GRFRegressor(
    n_iterations=50,
    n_estimators_per_iteration=10,
    learning_rate=0.1,
    max_depth=3,
    early_stopping_rounds=5,
    random_state=42
)

model.fit(X_train, y_train)

# Evaluate
print(f"R^2 Score: {model.score(X_test, y_test):.4f}")
print(f"MSE: {model.mse(X_test, y_test):.4f}")

# Feature importance
model.plot_feature_importances(top_n=10, save_path="importance.png")

# Training history
model.plot_training_history(save_path="history.png")

Classification

from hybridgbrf import GRFClassifier
from sklearn.datasets import make_classification

X, y = make_classification(n_samples=1000, n_features=20, n_classes=2, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

model = GRFClassifier(
    n_iterations=50,
    n_estimators_per_iteration=10,
    learning_rate=0.1,
    early_stopping_rounds=5,
    random_state=42
)

model.fit(X_train, y_train)

# Predict
y_pred = model.predict(X_test)
y_proba = model.predict_proba(X_test)

# Evaluate
print(f"Accuracy: {model.score(X_test, y_test):.4f}")
print(model.classification_report(X_test, y_test))

API Reference

GRFRegressor

Parameter Type Default Description
n_iterations int 100 Boosting iterations (rounds)
n_estimators_per_iteration int 10 Trees in each Random Forest
learning_rate float 0.1 Shrinkage factor
max_depth int 3 Max tree depth
min_samples_split int 2 Min samples to split
min_samples_leaf int 1 Min samples at leaf
max_features str/int/float 'sqrt' Features per split
subsample float 0.8 Sample fraction per iteration
early_stopping_rounds int 10 Stop if no improvement
validation_fraction float 0.1 Validation split size
tol float 1e-4 Improvement threshold
random_state int None Reproducibility seed
verbose int 0 Verbosity (0-2)
n_jobs int -1 Parallel jobs (-1 = all CPUs)

Methods

Method Description
fit(X, y) Train the model
predict(X) Predict targets
score(X, y) R^2 score (regression) / Accuracy (classification)
mse(X, y) Mean Squared Error
mae(X, y) Mean Absolute Error
cross_validate(X, y, cv) Cross-validation
grid_search(X, y, param_grid) Hyperparameter tuning
plot_feature_importances(...) Plot aggregated importances
plot_training_history(...) Plot loss curves
save_model(filepath) Save to disk
load_model(filepath) Load from disk (class method)

How It Works

Standard Gradient Boosting:          GBRF (Our Hybrid):
+--------------------------------+   +--------------------------------+
|  Iteration 1                   |   |  Iteration 1                   |
|  Tree -> Residual Prediction   |   |  RF Ensemble -> Residuals      |
+--------------------------------+   +--------------------------------+
              |                                    |
              v                                    v
+--------------------------------+   +--------------------------------+
|  Iteration 2                   |   |  Iteration 2                   |
|  Tree -> Residual Prediction   |   |  RF Ensemble -> Residuals      |
+--------------------------------+   +--------------------------------+
              |                                    |
              v                                    v
       ... continues ...                    ... continues ...

Key Difference: Single Tree vs Random Forest per iteration

Performance Comparison

from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from hybridgbrf import GRFRegressor
from sklearn.datasets import fetch_california_housing

X, y = fetch_california_housing(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Random Forest
rf = RandomForestRegressor(n_estimators=100, random_state=42)
rf.fit(X_train, y_train)
print(f"RF R^2: {rf.score(X_test, y_test):.4f}")

# Gradient Boosting
gb = GradientBoostingRegressor(n_estimators=100, random_state=42)
gb.fit(X_train, y_train)
print(f"GB R^2: {gb.score(X_test, y_test):.4f}")

# GBRF (Ours)
hgbrf = GRFRegressor(n_iterations=50, n_estimators_per_iteration=10, random_state=42)
hgbrf.fit(X_train, y_train)
print(f"GBRF R^2: {hgbrf.score(X_test, y_test):.4f}")

Saving and Loading

# Save
model.save_model("my_gbrf_model.pkl")

# Load
from gbrf import GRFRegressor
model = GRFRegressor.load_model("my_gbrf_model.pkl")

License

MIT License -- see LICENSE for details.


Built with ❤️ by Abhishek Singh

Metadata

Release files for hybridgbrf 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hybridgbrf 1.0.0
File Size Uploaded Provenance Actions
hybridgbrf-1.0.0.tar.gz 14.0 kB Without provenance Details for hybridgbrf-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hybridgbrf 1.0.0
File Interpreter ABI Platform Actions
hybridgbrf-1.0.0-py3-none-any.whl Python 3 none any Details for hybridgbrf-1.0.0-py3-none-any.whl

Total release size: 25.4 kB

Release files / hybridgbrf-1.0.0.tar.gz

Download URL hybridgbrf-1.0.0.tar.gz
Size 14.0 kB
Tags Source
SHA-256 checksum
How to use checksums
30e56eb39d728162262ca2b7d7516fb2bdb0b30161377ed7b6c4e21982e3e59d
BLAKE2b-256 checksum
How to use checksums
c8ad7c1894dc1908d1b8cfb48403ba5e3efee276e12cad957c42695635150885
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release files / hybridgbrf-1.0.0-py3-none-any.whl

Download URL hybridgbrf-1.0.0-py3-none-any.whl
Size 11.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8731639f948ead909a2a6783795754033022bfae4313f6fff1d38d403af36757
BLAKE2b-256 checksum
How to use checksums
0fc8ea4d2b86d831c46cc7775a33388b57560aa752fb95fae825c19655f898b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Security for hybridgbrf 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page