Large Scale Machine Learning With Python - XGBOOST - P236
Large Scale Machine Learning With Python - XGBOOST - P236
No Free Hunch
(http://blog.kaggle.com/)
(HTTP://BLOG.KAGGLE.COM) A KAGGLE MASTER EXPLAINS GRADIENT BOOSTING
(HTTP://BLOG.KAGGLE.COM/2017/01/26/OPEN-DATA-SPOTLIGHT-THE-GLOBAL-TERRORISM-DATABASE/)
(HTTP://BLOG.KAGGLE.COM/2017/01/12/SANTANDER-PRODUCT-RECOMMENDATION-COMPETITION-2ND-PLACE-
WINNERS-SOLUTION-WRITE-UP-TOM-VAN-DE-WIELE/)
55
A Kaggle Master Explains Gradient Boosting (http://b
Ben Gorman (http://blog.kaggle.com/author/bengorman/) | 01.23.2017
kaggle-
master-
This tutorial was originally posted here (https://gormanalysis.com/gradient-boosting-explained/) on Ben's blog,
GormAnalysis (https://gormanalysis.com/). explains
gradien
If linear regression was a Toyota Camry, then gradient boosting would be a UH-60 Blackhawk
Helicopter. A particular implementation of gradient boosting, XGBoost boostin
(https://github.com/dmlc/xgboost), is consistently used to win machine learning competitions on Kaggle
(https://www.kaggle.com/). Unfortunately many practitioners (including my former self) use it as a black
box. It’s also been butchered to death by a host of drive-by data scientists’ blogs. As such, the purpose
of this article is to lay the groundwork for classical gradient boosting, intuitively and comprehensively.
Motivation
We’ll start with a simple example. We want to predict a person’s age based on whether they play video
games, enjoy gardening, and their preference on wearing hats. Our objective is to minimize squared
error. We have these nine training samples to build our model.
We can do a quick and dirty inspection of the data to check these assumptions:
LikesGardening {13, 14, 15, 35} {25, 49, 68, 71, 73}
PlaysVideoGames {49, 71, 73} {13, 14, 15, 25, 35, 68}
LikesHats {14, 15, 49, 71} {13, 25, 35, 68, 73}
Now let’s model the data with a regression tree. To start, we’ll require that terminal nodes have at least
three samples. With this in mind, the regression tree will make its rst and last split on LikesGardening.
This is nice, but it’s missing valuable information from the feature LikesVideoGames. Let’s try letting
terminal nodes have 2 samples.
Here we pick up some information from PlaysVideoGames but we also pick up information from
LikesHats – a good indication that we’re over tting and our tree is splitting random noise.
Here in lies the drawback to using a single decision/regression tree – it fails to include predictive
power from multiple, overlapping regions of the feature space. Suppose we measure the training
errors from our rst tree.
1 13 19.25 -6.25
2 14 19.25 -5.25
3 15 19.25 -4.25
4 25 57.2 -32.2
5 35 19.25 15.75
6 49 57.2 -8.2
7 68 57.2 10.8
8 71 57.2 13.8
9 73 57.2 15.8
Now we can t a second regression tree to the residuals of the rst tree.
Notice that this tree does not include LikesHats even though our over tted regression tree above did.
The reason is because this regression tree is able to consider LikesHats and PlaysVideoGames with
respect to all the training samples, contrary to our over t regression tree which only considered each
feature inside a small region of the input space, thus allowing random noise to select LikesHats as a
splitting feature.
Now we can improve the predictions from our rst tree by adding the “error-correcting” predictions
from this tree.
1994 1765
It’s not hard to see how we can generalize this idea by inserting more models that correct the errors of
the previous model. Speci cally,
, for
At this point you might be wondering how to select the best value for the model’s hyper-parameter .
In other words, how many times should we iterate the residual-correction procedure until we decide
upon a nal model, ? This is best answered by testing di erent values of via cross-validation
(https://en.wikipedia.org/wiki/Cross-validation_(statistics)).
1. Depending on the size of the data this could be very computationally expensive. (Each considered
split would need to search for a median.)
2. It ruins our “plug-in” system. We’d only be able to plug in weak learns that support the objective
function(s) we want to use.
Instead we’re going to do something much niftier. Recall our example problem. To determine , we
start by choosing a minimizer for absolute error. This’ll be . Now we can measure the
residuals, .
1 13 35 -22
PersonID Age F0 Residual0
2 14 35 -21
3 15 35 -20
4 25 35 -10
5 35 35 0
6 49 35 14
7 68 35 33
8 71 35 36
9 73 35 38
Consider the rst and fourth training samples. They have residuals of -22 and -10 respectively. Now
suppose we’re able to make each prediction 1 unit closer to its target. Respective squared error
reductions would be 43 and 19, while respective absolute error reductions would be 1 and 1. So a
regression tree, which by default minimizes squared error, will focus heavily on reducing the residual of
the rst training sample. But if we want to minimize absolute error, moving each prediction one unit
closer to the target produces an equal reduction in the cost function. With this in mind, suppose that
instead of training on the residuals of , we instead train on the gradient of the loss function, with
respect to the prediction values produced by . Essentially, we’ll train on the cost reduction for each
sample if the predicted value were to become one unit closer to the observed value. In the case of
absolute error, will simply consider the sign of every residual (as apposed to squared error which
would consider the magnitude of every residual). After samples in are grouped into leaves, an average
gradient can be calculated and then scaled by some factor, , so that minimizes the loss function for the
samples in each leaf. (Note that in practice, a di erent factor is chosen for each leaf.)
Gradient Descent
Let’s formalize this idea using the concept of gradient descent
(https://en.wikipedia.org/wiki/Gradient_descent). Consider a di erentiable function we want to
minimize. For example,
The goal here is to nd the pair that minimizes . Notice, you can interpret this function as calculating
the squared error for two data points, 15 and 25 given two prediction values, and (but with a
multiplier to make the math work out nicely). Although we can minimize this function directly, gradient
descent will let us minimize more complicated loss functions that we can’t minimize directly.
Initialization Steps:
Number of iteration steps
Starting point
Step size
For iteration to :
1. Calculate the gradient of at the point
2. “Step” in the direction of greatest descent (the negative gradient) with step size . That is,
If you’re struggling with this part, just google gradient descent (https://www.google.com/webhp?
sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=gradient%20descent). It’s been explained many times in
many ways.
Take a second to stand in awe of what we just did. We modi ed our gradient boosting algorithm so that
it works with any di erentiable loss function. (This is the part that gets butchered by a lot of gradient
boosting explanations.) Let’s clean up the ideas above and reformulate our gradient boosting model
once again.
For m = 1 to M:
Compute pseudo residuals,
Fit base learner, to pseudo residuals
Compute step magnitude multiplier . (In the case of tree models, compute a di erent for every leaf.)
Update
In case you want to check your understanding so far, our current gradient boosting applied to our
sample problem for both squared error and absolute error objectives yields the following results.
Squared Error
Age F0 PseudoResidual0 h0 gamma0 F1 PseudoResidual1 h1 gamma1 F2
49 35 1 0.6 55 68 -1 0.3333 9 71
Age F0 PseudoResidual0 h0 gamma0 F1 PseudoResidual1 h1 gamma1 F2
71 35 1 0.6 55 68 1 0.3333 9 71
73 35 1 0.6 55 68 1 0.3333 9 71
What else can it do? Although I presented gradient boosting as a regression model, it’s also very
e ective as a classi cation and ranking model. As long as you have a di erentiable loss function for the
algorithm to minimize, you’re good to go. The logistic function
(https://en.wikipedia.org/wiki/Logistic_function) is typically used for binary classi cation and the softmax
function (https://en.wikipedia.org/wiki/Softmax_function) is often used for multi-class classi cation.
I leave you with a quote from my fellow Kaggler Mike Kim (https://www.kaggle.com/mikeskim).
Share this:
(http://blog.kaggle.com/2017/01/23/a-kaggle-master-explains-gradient-boosting/?share=twitter&nb=1)
(http://blog.kaggle.com/2017/01/23/a-kaggle-master-explains-gradient-boosting/?share=facebook&nb=1)
159
(http://blog.kaggle.com/2017/01/23/a-kaggle-master-explains-gradient-boosting/?share=google-plus-1&nb=1)
TUTORIAL (HTTP://BLOG.KAGGLE.COM/TAG/TUTORIAL/)
XGBOOST (HTTP://BLOG.KAGGLE.COM/TAG/XGBOOST/)
Name
There's just one minor nitpick in this part, where thing get a bit confusing - at least for me:
> "They have F_0 residuals of -22 and -10 respectively. ... Respective squared error
reductions would be 43 and 19,..."
If you define squared error without 1/2 in front of the sum, this is indeed true (i.e. if you
change prediction 35 (for age 13) to 34, you get (35-13)^2 - (34-13)^2 = 43), but that means
that the residual (y - F(x)) would have to be 2*(y - F(x)), if you want it to be the gradient of the
loss function... and that means that the aforementioned residual (-22) should be -44, right?
I'm just trying to visualize what this all means in terms of gradients in function space and all
that, and it's perfectly possible that I blew it...
△ ▽ • Reply • Share ›
"My personal take is that it causes sample-predictions to slowly converge toward observed
values. As this slow convergence occurs, samples that get closer to their target end up
being grouped together into larger and larger leaves (due to fixed tree size parameters),
resulting in a natural regularization effect."
I'm not following unto which values exactly they converge... what are the observed values?
Why does this grouping effect help regularization? Is it that samples that are intrinsically
better explained via some sort of feature, eventually end up in a leaf described by it? Or
something different?
△ ▽ • Reply • Share ›
Any chance you could (here or in another article) give us a sense of what the prediction step
for new examples looks like? Is it an average or weighted average of the stump models? Or
would we only use model 2? Something entirely different? thanks --Sean
△ ▽ • Reply • Share ›
In the section "leveraging gradient descent" where you present a summary table to check
understanding (for squared error), shouldn't the "PseudoResidual0" column contain
gradients of residuals rather than the residuals themselves? Right now this column contains
residuals only i.e. difference between age and F0. Or am I missing something?
△ ▽ • Reply • Share ›
It will be cool if we you can say what the logistic function is used for. Is it to convert the
prediction to a probability?
△ ▽ • Reply • Share ›
b k • a year ago
In Draft '3', I question in following sentence "They have F_0 residuals of -22 and -10
respectively. Now suppose we’re able to make each prediction 1 unit closer to its target.
Respective squared error reductions would be 43 and 19, while respective absolute error
reductions would be 1 and 1". Can anyone help me in understanding on values 43,19 are
arrived?
△ ▽ • Reply • Share ›
There is, however, one point that I did not get when you are giving the estimating process
for the squared error and the absolute error loss functions. For the squared error, h0 and h1
forecasts are given by the mean of the samples contained in each leaf node. However, I did
not get how you got the h0 and h1 forecasts for the absolute error function. Besides, the
way gamma0 and gamma1 are estimated for the absolute error loss function is still obscure
(though I understand from the line search article from wikipedia that we are looking for the
step that minimizes the loss function given the gradient in one point).
That would be great if you could explicit those last details so we could understand the
whole picture of your article ;-)
The way that h0 and h1 are computed in the absolute error loss function example is
again by the mean of the samples contained in each leaf node. The problem is that
the first decision tree has a drawing mistake. The left leaf (LikesGardening==F)
should contain the values {-1,-1,-1,-1} corresponding to PersonID {1,2,3,5}. The
second leaf (LikesGardening==T) should contain the values {-1,1,1,1,1}
corresponding to PersonID {4,6,7,8,9}. Now, buy computing the mean in leaf1: (-1-1-
1-1)/4 = -1 and for leaf2: (-1+1+1+1+1)/5=0 6 This values coincide with the values
(https://www.facebook.com/kaggle) (https://twitter.com/kaggle)