Skip to main content

How to Train a Gradient Boosting Regression Model with HistGradientBoostingRegressor

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

HistGradientBoostingRegressor is scikit-learn's fast gradient boosting model for predicting a number: a price, a count, a measurement. It builds many small decision trees, each one correcting the errors of the ones before it, and it copes with missing values on its own. This guide trains one with scikit-learn 1.9.1 on Python 3.15.2.

Syntax​

Syntax
from sklearn.ensemble import HistGradientBoostingRegressor

<model> = HistGradientBoostingRegressor(max_iter=<trees>, learning_rate=<step>, random_state=<seed>)
<model>.fit(<X_train>, <y_train>)
<predictions> = <model>.predict(<X_test>)
PartWhat it means
max_iter=<trees>How many boosting rounds, so how many trees, at most. The default is 100
learning_rate=<step>How much each tree corrects the ones before it. Smaller steps usually need more trees. The default is 0.1
random_state=<seed>A whole number that makes the random parts repeatable
fit and predictLearn from the training rows, then predict new ones

The HistGradientBoostingRegressor reference calls it a "Histogram-based Gradient Boosting Regression Tree" that is "much faster than GradientBoostingRegressor for big datasets (n_samples >= 10 000)".

The Example​

scikit-learn's built-in diabetes dataset has 442 patients, ten measurements each, and a number that tracks how the disease progressed a year later. The script trains on 80 % of the patients and measures the error on the other 20 %.

Prerequisites​

  • scikit-learn in your project, for example with uv add scikit-learn. This guide uses scikit-learn 1.9.1 with pandas 3.1.2 on Python 3.15.2.

Step 1: Write the Script​

train_hgbr.py
from sklearn.datasets import load_diabetes
from sklearn.ensemble import HistGradientBoostingRegressor
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import train_test_split

X, y = load_diabetes(return_X_y=True, as_frame=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

model = HistGradientBoostingRegressor(max_iter=100, learning_rate=0.05, random_state=0)
model.fit(X_train, y_train)

print("Rows:", len(X_train), "to train,", len(X_test), "to test")
print("Trees built:", model.n_iter_)
print("Test MAE:", round(mean_absolute_error(y_test, model.predict(X_test)), 2))

The train_hgbr.py script in a macOS-style code window: it loads the diabetes dataset, splits it 80/20, trains a HistGradientBoostingRegressor with max_iter=100, learning_rate=0.05 and random_state=0, and prints the row counts, the trees built and the test MAE.

Step 2: Run It​

Terminal
uv run python train_hgbr.py
Output
Rows: 353 to train, 89 to test
Trees built: 100
Test MAE: 49.21

The terminal after uv run python train_hgbr.py: Rows: 353 to train, 89 to test; Trees built: 100; Test MAE: 49.21.

The model built all 100 trees. On the 89 test patients, its predictions were off by 49.21 on average, in the units of the target. n_iter_ is the number of boosting rounds it really ran. The command works unchanged in PowerShell.

Early Stopping Turns On by Itself​

The default, early_stopping="auto", decides from the data's size. The reference says early stopping "is enabled if the sample size is larger than 10000". Training the same model on 9,000 and then 11,000 generated rows printed:

Output
9000 rows: early stopping False - trees built 300
11000 rows: early stopping True - trees built 300

With 11,000 rows, the model set part of the training data aside to decide when to stop (the validation_fraction, 10 % by default), so the same code trains differently once the data grows past 10,000 rows. Here it still used all 300 trees, because the score kept improving. Set early_stopping=True or False yourself when the behaviour must not depend on the data's size.

Missing Values Need No Filling​

The reference says: "This estimator has native support for missing values (NaNs)." A small model trained on data where every missing value had a target of 30 predicted, for a missing value and for 2.0:

Output
[30. 19.2]

It learned where to send rows with a missing value, without any filling in.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving random_state outResults can change between runs whenever the model uses randomness, such as for early stoppingPass a whole number, such as random_state=0
Assuming early stopping is offAbove 10,000 training rows, "auto" turns it onSet early_stopping explicitly

Troubleshooting​

SymptomLikely causeFix
n_iter_ is smaller than max_iterEarly stopping ended training when the score stopped improvingExpected; set early_stopping=False to always build every tree

FAQ​

What's the difference from GradientBoostingRegressor?​

Before training, HistGradientBoostingRegressor puts each feature's values into a limited number of bins; the reference says the features are "binned into integer-valued bins, which allows for a much faster training stage." It also handles missing values. The reference calls it "much faster than GradientBoostingRegressor for big datasets (n_samples >= 10 000)".

How do I train a classifier instead?​

With HistGradientBoostingClassifier, which works the same way for categories.

Conclusion​

HistGradientBoostingRegressor(max_iter=100, learning_rate=0.05, random_state=0) built 100 trees on the diabetes data and reached a test MAE of 49.21. It needs no filling for missing values, and it turns early stopping on by itself above 10,000 rows, so set that explicitly when it matters.

Additional Resources​