You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
author: Tianqi Chen, Tong He, Michaël Benesty, David Cortes
10
10
vignette: >
11
11
%\VignetteIndexEntry{XGBoost presentation}
12
12
%\VignetteEngine{knitr::rmarkdown}
@@ -25,50 +25,34 @@ The purpose of this Vignette is to show you how to use **XGBoost** to build a mo
25
25
26
26
It is an efficient and scalable implementation of gradient boosting framework by @friedman2000additive and @friedman2001greedy. Two solvers are included:
27
27
28
+
-*tree learning* algorithm (in different varieties).
28
29
-*linear* model ;
29
-
-*tree learning* algorithm.
30
30
31
-
It supports various objective functions, including *regression*, *classification* and *ranking*. The package is made to be extendible, so that users are also allowed to define their own objective functions easily.
31
+
It supports various objective functions, including *regression*, *classification*(binary and multi-class) and *ranking*. The package is made to be extensible, so that users are also allowed to define their own objective functions easily.
32
32
33
33
It has been [used](https://github.com/dmlc/xgboost) to win several [Kaggle](http://www.kaggle.com) competitions.
34
34
35
35
It has several features:
36
36
37
-
* Speed: it can automatically do parallel computation on *Windows* and *Linux*, with *OpenMP*. It is generally over 10 times faster than the classical `gbm`.
37
+
* Speed: it can automatically do parallel computations with *OpenMP*. It is generally over 10 times faster than the classical `gbm`.
38
38
* Input Type: it takes several types of input data:
39
39
**Dense* Matrix: *R*'s *dense* matrix, i.e. `matrix` ;
40
40
**Sparse* Matrix: *R*'s *sparse* matrix, i.e. `Matrix::dgCMatrix` ;
41
41
* Data File: local data files ;
42
-
*`xgb.DMatrix`: its own class (recommended).
43
-
* Sparsity: it accepts *sparse* input for both *tree booster* and *linear booster*, and is optimized for *sparse* input ;
42
+
* Data frames (class `data.frame` and sub-classes from it such as `data.table`), taking
43
+
both numeric and categorical (factor) features.
44
+
*`xgb.DMatrix`: its own class (recommended, also supporting numeric and categorical features).
44
45
* Customization: it supports customized objective functions and evaluation functions.
45
46
46
47
## Installation
47
48
48
-
49
-
### GitHub version
50
-
51
-
52
-
For weekly updated version (highly recommended), install from *GitHub*:
install.packages("xgboost", repos = "http://dmlc.ml/drat/", type = "source")
58
-
```
59
-
60
-
> *Windows* user will need to install [Rtools](https://cran.r-project.org/bin/windows/Rtools/) first.
61
-
62
-
### CRAN version
63
-
64
-
65
-
The version 0.4-2 is on CRAN, and you can install it by:
49
+
Package can be easily installed from CRAN:
66
50
67
51
```{r, eval=FALSE}
68
52
install.packages("xgboost")
69
53
```
70
54
71
-
Formerly available versions can be obtained from the CRAN [archive](https://cran.r-project.org/src/contrib/Archive/xgboost/)
55
+
For the development version, see the [GitHub page](https://github.com/dmlc/xgboost) and the [installation docs](https://xgboost.readthedocs.io/en/stable/install.html) for further instructions.
72
56
73
57
## Learning
74
58
@@ -124,7 +108,7 @@ dim(train$data)
124
108
dim(test$data)
125
109
```
126
110
127
-
This dataset is very small to not make the **R** package too heavy, however **XGBoost** is built to manage huge dataset very efficiently.
111
+
This dataset is very small to not make the **R** package too heavy, however **XGBoost** is built to manage huge datasets very efficiently.
128
112
129
113
As seen below, the `data` are stored in a `dgCMatrix` which is a *sparse* matrix and `label` vector is a `numeric` vector (`{0,1}`):
130
114
@@ -144,7 +128,7 @@ We are using the `train` data. As explained above, both `data` and `label` are s
144
128
145
129
In a *sparse* matrix, cells containing `0` are not stored in memory. Therefore, in a dataset mainly made of `0`, memory size is reduced. It is very usual to have such dataset.
146
130
147
-
We will train decision tree model using the following parameters:
131
+
We will train a decision tree model using the following parameters:
148
132
149
133
*`objective = "binary:logistic"`: we will train a binary classification model (note that this is set automatically when `y` is a `factor`) ;
150
134
*`max_depth = 2`: the trees won't be deep, because our case is very simple ;
@@ -156,12 +140,36 @@ bstSparse <- xgboost(
156
140
x = train$data
157
141
, y = factor(train$label, levels = c(0, 1))
158
142
, objective = "binary:logistic"
159
-
, params = list(max_depth = 2, eta = 1)
143
+
, max_depth = 2
144
+
, eta = 1
160
145
, nrounds = 2
161
146
, nthread = 2
162
147
)
163
148
```
164
149
150
+
Note that, while the R function `xgboost()` follows typical R idioms for statistical modeling packages
151
+
such as an x/y division and having those as first arguments, it also offers a more flexible `xgb.train`
152
+
interface which is more consistent across different language bindings (e.g. arguments are the same as
153
+
in the Python XGBoost library) and which exposes some additional functionalities. The `xgb.train`
154
+
interface uses XGBoost's own DMatrix class to pass data to it, and accepts the model parameters instead
155
+
as a named list:
156
+
157
+
```{r}
158
+
bstTrInterface <- xgb.train(
159
+
data = xgb.DMatrix(train$data, label = train$label, nthread = 1)
160
+
, params = list(
161
+
objective = "binary:logistic"
162
+
, max_depth = 2
163
+
, eta = 1
164
+
)
165
+
, nthread = 2
166
+
, nrounds = 2
167
+
)
168
+
```
169
+
170
+
For the rest of this tutorial, we'll nevertheless be using the `xgboost()` interface which will be
171
+
more familiar to users of packages such as GLMNET or Ranger.
172
+
165
173
> More complex the relationship between your features and your `label` is, more passes you need.
166
174
167
175
#### Parameter variations
@@ -174,78 +182,51 @@ Alternatively, you can put your dataset in a *dense* matrix, i.e. a basic **R**
174
182
bstDense <- xgboost(
175
183
x = as.matrix(train$data),
176
184
y = factor(train$label, levels = c(0, 1)),
177
-
params = list(max_depth = 2, eta = 1),
178
-
nrounds = 2,
179
-
nthread = 2
185
+
max_depth = 2,
186
+
eta = 1,
187
+
nthread = 2,
188
+
nrounds = 2
180
189
)
181
190
```
182
191
183
-
##### xgb.DMatrix
192
+
##### Data frame
184
193
185
-
**XGBoost** offers a way to group them in a `xgb.DMatrix`. You can even add other meta data in it. It will be useful for the most advanced features we will discover later.
194
+
As another alternative, XGBoost will also accept `data.frame` objects, from which it can
**XGBoost** has several features to help you to view how the learning progress internally. The purpose is to help you to set the best parameters, which is the key of your model quality.
209
+
##### Verbosity levels
204
210
205
-
One of the simplest way to see the training progress is to set the `verbose` option (see below for more advanced techniques).
211
+
**XGBoost** has several features to help you to view how the learning progresses internally. The purpose is to help you
212
+
set the best parameters, which is the key of your model quality. Note that when using the `xgb.train` interface,
213
+
one can also use a separate evaluation dataset (e.g. a different subset of the data than the training dataset) on
214
+
which to monitor metrics of interest, and it also offers an `xgb.cv` function which automatically splits the data
215
+
to create evaluation subsets for you.
206
216
207
-
```{r trainingVerbose0, message=T, warning=F}
208
-
# verbose = 0, no message
209
-
bst <- xgb.train(
210
-
data = dtrain
211
-
, params = list(
212
-
max_depth = 2
213
-
, eta = 1
214
-
, nthread = 2
215
-
, objective = "binary:logistic"
216
-
)
217
-
, nrounds = 2
218
-
, verbose = 0
219
-
)
220
-
```
217
+
One of the simplest way to see the training progress is to set the `verbosity` option:
221
218
222
219
```{r trainingVerbose1, message=T, warning=F}
223
-
# verbose = 1, print evaluation metric
224
-
bst <- xgb.train(
225
-
data = dtrain
226
-
, params = list(
227
-
max_depth = 2
228
-
, eta = 1
229
-
, nthread = 2
230
-
, objective = "binary:logistic"
231
-
)
232
-
, nrounds = 2
233
-
, verbose = 1
234
-
)
235
-
```
236
-
237
-
```{r trainingVerbose2, message=T, warning=F}
238
-
# verbose = 2, also print information about tree
239
-
bst <- xgb.train(
240
-
data = dtrain
241
-
, params = list(
242
-
max_depth = 2
243
-
, eta = 1
244
-
, nthread = 2
245
-
, objective = "binary:logistic"
246
-
)
247
-
, nrounds = 2
248
-
, verbose = 2
220
+
# verbosity = 1, print evaluation metric
221
+
bst <- xgboost(
222
+
x = train$data,
223
+
y = factor(train$label, levels = c(0, 1)),
224
+
max_depth = 2,
225
+
eta = 1,
226
+
nthread = 2,
227
+
objective = "binary:logistic",
228
+
nrounds = 5,
229
+
verbosity = 1
249
230
)
250
231
```
251
232
@@ -267,56 +248,53 @@ print(length(pred))
267
248
print(head(pred))
268
249
```
269
250
270
-
These numbers doesn't look like *binary classification*`{0,1}`. We need to perform a simple transformation before being able to use these results.
271
-
272
-
## Transform the regression in a binary classification
273
-
274
-
275
-
The only thing that **XGBoost** does is a *regression*. **XGBoost** is using `label` vector to build its *regression* model.
276
-
277
-
How can we use a *regression* model to perform a binary classification?
278
-
279
-
If we think about the meaning of a regression applied to our data, the numbers we get are probabilities that a datum will be classified as `1`. Therefore, we will set the rule that if this probability for a specific datum is `> 0.5` then the observation is classified as `1` (or `0` otherwise).
251
+
These numbers reflect the predicted probabilities of belonging to the class '1' in the 'y' data. Tautologically,
252
+
the probability of belonging to the class '0' is then $P(y=0) = 1 - P(y=1)$. This implies: if the number is greater
253
+
than 0.5, then according to the model it is more likely than an observation will be of class '1', whereas if the
254
+
number if lower than 0.5, it is more likely that the observation will be of class '0':
280
255
281
256
```{r predictingTest, message=F, warning=F}
282
257
prediction <- as.numeric(pred > 0.5)
283
258
print(head(prediction))
284
259
```
285
260
261
+
Note that one can also control the prediction type directly to obtain classes instead of probabilities.
262
+
286
263
## Measuring model performance
287
264
288
265
289
-
To measure the model performance, we will compute a simple metric, the *average error*.
266
+
To measure the model performance, we will compute a simple metric, the *accuracy rate*.
> Note that the algorithm has not seen the `test` data during the model construction.
297
274
298
275
Steps explanation:
299
276
300
277
1.`as.numeric(pred > 0.5)` applies our rule that when the probability (<=> regression <=> prediction) is `> 0.5` the observation is classified as `1` and `0` otherwise ;
301
-
2.`probabilityVectorPreviouslyComputed != test$label`computes the vector of error between true data and computed probabilities ;
302
-
3.`mean(vectorOfErrors)` computes the *average error* itself.
278
+
2.`probabilityVectorPreviouslyComputed == test$label`whether the predicted class matches with the real data ;
279
+
3.`mean(vectorOfMatches)` computes the *accuracy rate* itself.
303
280
304
-
The most important thing to remember is that **to do a classification, you just do a regression to the**`label`**and then apply a threshold**.
281
+
The most important thing to remember is that **to obtain the predicted class of an observation, a threshold needs to be applied on the predicted probabilities**.
305
282
306
283
*Multiclass* classification works in a similar way.
307
284
308
-
This metric is **`r round(err, 2)`** and is pretty low: our yummy mushroom model works well!
285
+
This metric is **`r round(acc, 2)`** and is pretty high: our yummy mushroom model works well!
309
286
310
287
## Advanced features
311
288
312
289
313
290
Most of the features below have been implemented to help you to improve your model by offering a better understanding of its content.
314
291
315
292
316
-
### Dataset preparation
293
+
### Dataset preparation for xgb.train
317
294
318
295
319
-
For the following advanced features, we need to put data in `xgb.DMatrix` as explained above.
296
+
For the following advanced features, we'll be using the `xgb.train()` interface instead of the `xbgoost()`
297
+
interface, so we need to put data in an `xgb.DMatrix` as explained earlier:
@@ -332,7 +310,7 @@ One of the special feature of `xgb.train` is the capacity to follow the progress
332
310
333
311
One way to measure progress in learning of a model is to provide to **XGBoost** a second dataset already classified. Therefore it can learn on the first dataset and test its model on the second one. Some metrics are measured after each round during the learning.
334
312
335
-
> in some way it is similar to what we have done above with the average error. The main difference is that below it was after building the model, and now it is during the construction that we measure errors.
313
+
> in some way it is similar to what we have done above with the prediction accuracy. The main difference is that below it was after building the model, and now it is during the construction that we measure quality of predictions.
336
314
337
315
For the purpose of this example, we use the `evals` parameter. It is a list of `xgb.DMatrix` objects, each of them tagged with a name.
338
316
@@ -352,7 +330,9 @@ bst <- xgb.train(
352
330
)
353
331
```
354
332
355
-
**XGBoost** has computed at each round the same average error metric than seen above (we set `nrounds` to 2, that is why we have two lines). Obviously, the `train-error` number is related to the training dataset (the one the algorithm learns from) and the `test-error` number to the test dataset.
333
+
**XGBoost** has computed at each round the same (negative of) average log-loss (logarithm of the Bernoulli likelihood)
334
+
that it uses as optimization objective to minimize in both of the datasets. Obviously, the `train_logloss` number is
335
+
related to the training dataset (the one the algorithm learns from) and the `test_logloss` number to the test dataset.
356
336
357
337
Both training and test error related metrics are very similar, and in some way, it makes sense: what we have learned from the training dataset matches the observations from the test dataset.
You can dump the tree you learned using `xgb.dump` into a text file.
439
+
XGBoost can output the trees it fitted in a standard tabular format:
460
440
461
-
```{r dump, message=T, warning=F}
462
-
xgb.dump(bst, with_stats = TRUE)
441
+
```{r}
442
+
xgb.model.dt.tree(bst)
463
443
```
464
444
465
445
You can plot the trees from your model using ```xgb.plot.tree``
466
446
467
-
```
447
+
```{r}
468
448
xgb.plot.tree(model = bst)
469
449
```
470
450
@@ -475,7 +455,9 @@ xgb.plot.tree(model = bst)
475
455
476
456
Maybe your dataset is big, and it takes time to train a model on it? May be you are not a big fan of losing time in redoing the same task again and again? In these very rare cases, you will want to save your model and load it when required.
477
457
478
-
Hopefully for you, **XGBoost** implements such functions.
458
+
XGBoost models can be saved through R functions such as `save` and `saveRDS`, but in addition, it also offers
459
+
its own serialization format, which might have better compatibility guarantees across versions of XGBoost and
460
+
which can also be loaded into other language bindings:
479
461
480
462
```{r saveModel, message=F, warning=F}
481
463
# save model to binary local file
@@ -507,7 +489,7 @@ file.remove(fname)
507
489
508
490
> result is `0`? We are good!
509
491
510
-
In some very specific cases, you will want to save the model as a *R*binary vector. See below how to do it.
492
+
In some very specific cases, you will want to save the model as a *R*raw vector. See below how to do it.
0 commit comments