翻篇收录说明
翻篇网页快照

What is gradient boosting? How GBMs and boosted trees work

https://telnyx.com/learn-ai/gradient-boosting-machines

最近抓取:2026-12-05 02:40:55(北京时间)

最近内容变化:2026-12-02 14:08:44(北京时间)

这是最近一次有效抓取的文本快照,不含网页脚本、图片和动态交互内容。实际网页可能已更新。

访问原网页

页面简介

Gradient boosting builds a model from many small trees, each fixing the errors of the last. How GBMs work, XGBoost vs random forest, and Python code.

正文摘录

Back to GlossaryWhat is gradient boosting? How GBMs and boosted trees workGradient boosting builds a model from many small trees, each fixing the errors of the last. How GBMs work, XGBoost vs random forest, and Python code.Editor: Emily BowenUpdated October 2026Gradient boosting is a machine learning technique that builds a prediction model one step at a time. Each step adds a small model, usually a decision tree, trained to correct the errors left by the steps before it. The finished model adds up the predictions of every step. In machine learning, GBM stands for gradient boosting machine, a model trained this way.Each tree in a GBM is too simple to predict well on its own, yet a few hundred of them added in sequence make one of the most accurate models for tables of data.What is gradient boosting?Gradient boosting is a form of boosting, the family of methods that train simple models in sequence. Each new model concentrates on what the combined model still gets wrong.In gradient boosting, each new model learns the negative gradient of a loss function: the direction the predictions should move to lower the error. Jerome Friedman of Stanford introduced the method in 2001 in Greedy function approximation.The loss is a choice, so the same algorithm covers regression with squared or absolute error and classification with log loss.How does gradient boosting work?Gradient boosting starts from one constant prediction, then repeats a single step: fit a small tree to the current errors, and add a fraction of that tree's prediction to the model.For regression with squared error, each round runs like this:The model starts by predicting the average of the target for every row.Each row's residual is its actual value minus the current prediction.A small decision tree learns to predict those residuals from the features.The tree's predictions are added to the model, scaled down by the learning rate.Steps 2 to 4 repeat for a set number of trees, or until a validation score stops improving.In a toy set of six support calls, the one feature is how many issues each caller raised: 1, 1, 2, 3, 3, and 4. The target is each call's length in minutes: 5, 6, 7, 10, 11, and 15.The starting prediction is the average, 9 minutes, for every call. The residuals run from -4 to +6 minutes. The mean squared error, the average of the squared residuals, is 11.7.A decision tree divides rows on one feature at a time and predicts one constant value for each group it ends with, called a leaf. Each tree in this example makes a single split.The first tree splits the calls at two issues or fewer, the split that most reduces the squared error of the residuals. It predicts a residual of -3 minutes for those three calls and +3 for the other three.A learning rate of 0.5 adds half of each value, a larger step than the usual 0.1, chosen here to keep each round visible.Calls with one or two issues now get 7.5 minutes, calls with three or four get 10.5. The error falls to 4.9. The second tree splits at three issues or fewer and the third at one, cutting the error to 1.9, then 1.0.After 50 trees the model predicts 5.5, 5.5, 7, 10.5, 10.5 and 15 minutes. Calls with the same number of issues get the same prediction, because no tree can split rows that share every feature value.At inference time, a trained GBM runs a new row through every tree and adds the starting average to the sum of the trees' scaled outputs.Gradient boosted decision treesGradient boosted decision trees (GBDT), also called gradient tree boosting, are gradient boosting with small decision trees as the models it adds. XGBoost, LightGBM, CatBoost, and scikit-learn all implement this form.Boosting keeps each tree shallow, so each one underfits on its own. The sequence of trees does the learning.Tree depth sets which feature interactions a GBM can learn. A path through a tree three levels deep tests at most three features, such as a customer's plan, the time of day, and the number of issues.A tree with a single split, called a stump, can only learn each feature's effect on its own. scikit-learn's GradientBoostingRegressor defaults to trees three levels deep; its histogram-based estimators cap each tree at 31 leaves instead.Gradient boosting for regression and classificationGradient boosting runs the same loop for regression and classification with a different loss function, which decides what each new tree learns.For regression with squared error, the negative gradient of the loss is the residual itself: the gap between the true value and the prediction. That is why each tree in the six-call example could fit the residuals directly.Absolute error and Huber loss, which squares small errors and takes the absolute value of large ones, give outliers less pull on the model. Friedman's paper covers both.For classification, a GBM adds its trees on the log-odds scale, the logarithm of p / (1 - p), where 0 means even odds. A sigmoid function turns the sum into a probability between 0 and 1.The loss is log loss, which grows as the predicted probability of the true label falls. Each new tree fits the gap between the true label, 0 or 1, and the current predicted probability.That gap is on the probability scale, while the model adds on the log-odds scale. So before a tree is added, each leaf's value is reset to the log-odds step that most lowers the loss for its rows, the line search in Friedman's algorithm.With more than two classes, each round adds one tree per class.Overfitting and early stoppingA GBM overfits when it keeps adding trees after its loss on held-out data stops falling. Tracking that loss shows where to stop.On scikit-learn's diabetes dataset (442 patients, 10 features), a GradientBoostingRegressor with depth-3 trees and a learning rate of 0.1 trained on 331 rows and was scored on the other 111 (a random split, seed 0).Validation error was lowest at 13 trees, a mean squared error of 3,514, against 4,965 for predicting the average. The same 111 rows chose that count, so 3,514 flatters the model.By 500 trees, training error had fallen to 35 while validation error had climbed to 4,364, 24% above its low.The extra trees had fit noise in the 331 training rows. Early stopping ends training once the validation loss has gone a set number of rounds without improving.Gradient boosting in Pythonscikit-learn's HistGradientBoostingClassifier trains a gradient boosted model in a few lines, with early stopping ending training once the held-out loss stops improving.The example trains it on the breast cancer dataset that ships with scikit-learn (569 tumors, 30 features) and holds out 20% of the rows for testing:Pythonfrom sklearn.datasets import load_breast_cancerfrom sklearn.ensemble import HistGradientBoostingClassifierfrom sklearn.model_selection import train_test_splitX, y = load_breast_cancer(return_X_y=True)X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)model = HistGradientBoostingClassifier( learning_rate=0.1, # how much of each new tree to add max_iter=500, # upper limit on the number of trees max_leaf_nodes=15, # size of each tree early_stopping=True, # hold out 10% of the training data... n_iter_no_change=10, # ...and stop after 10 trees without improvement random_state=0,)model.fit(X_train, y_train)print("trees built:", model.n_iter_)print("test accuracy:", round(model.score(X_test, y_test), 3))The model stopped after 55 trees, 10 rounds after its best one. It classified 111 of the 114 test rows (97.4%) correctly. Four settings control it:learning_rate scales each tree's contribution. scikit-learn's guide recommends 0.1 or lower, with max_iter set high enough that early stopping, not the cap, ends training.max_iter caps the number of trees.max_leaf_nodes (or max_depth) sets the size of each tree, which sets how many features can interact.early_stopping holds out 10% of the training rows and stops after n_iter_no_change rounds without improvement.Early stopping takes care of the tree count, which leaves the learning rate and tree size for hyperparameter tuning.The older GradientBoostingClassifier has a subsample setting that trains each tree on a random fraction of the rows, typically half. Friedman called this stochastic gradient boosting.Combined with a small learning rate, subsampling can further increase accuracy, according to scikit-learn's guide.HistGradientBoostingClassifier bins each feature into at most 255 values before training and handles missing values and categorical features natively.scikit-learn says the histogram version can be orders of magnitude faster than the older class above tens of thousands of rows.XGBoost, LightGBM, and CatBoostXGBoost, LightGBM, and CatBoost are open-source libraries that implement gradient boosted decision trees. They run the same algorithm and differ in how they find splits, sample rows, and handle categories.LibraryPaperWhat it adds to gradient boostingXGBoostChen and Guestrin, 2016A regularized objective, second-order gradient statistics, a learned default branch for missing values, and a system design that scales past billions of rowsLightGBMKe et al., 2017Histogram-based splits, sampling that keeps the rows with large gradients, bundling of features that are rarely nonzero together, and leaf-wise growthCatBoostProkhorenkova et al., 2018Ordered boosting, which computes each row's residual with a model that never saw that row, and an ordered encoding of categorical features; both stop the label leaking into the model's own inputs. Symmetric trees use one split per levelThe LightGBM paper reports training up to more than 20 times faster than conventional GBDT at almost the same accuracy. LightGBM targets data with many rows and many features. CatBoost targets data where many features are categorical.Gradient boosting vs random forestGradient boosting adds shallow trees one after another, while a random forest grows deep trees independently and averages them.In Leo Breiman's random forest, each tree trains on a sample