Debian Codegraph @

man/xgb.cb.gblinear.history.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/callbacks.R
3 \name{xgb.cb.gblinear.history}
4 \alias{xgb.cb.gblinear.history}
5 \title{Callback for collecting coefficients history of a gblinear booster}
6 \usage{
7 xgb.cb.gblinear.history(sparse = FALSE)
8 }
9 \arguments{
10 \item{sparse}{When set to \code{FALSE}/\code{TRUE}, a dense/sparse matrix is used to store the result.
11 Sparse format is useful when one expects only a subset of coefficients to be non-zero,
12 when using the "thrifty" feature selector with fairly small number of top features
13 selected per iteration.}
14 }
15 \value{
16 An \code{xgb.Callback} object, which can be passed to \code{\link[=xgb.train]{xgb.train()}} or \code{\link[=xgb.cv]{xgb.cv()}}.
17 }
18 \description{
19 Callback for collecting coefficients history of a gblinear booster
20 }
21 \details{
22 To keep things fast and simple, gblinear booster does not internally store the history of linear
23 model coefficients at each boosting iteration. This callback provides a workaround for storing
24 the coefficients' path, by extracting them after each training iteration.
25
26 This callback will construct a matrix where rows are boosting iterations and columns are
27 feature coefficients (same order as when calling \link{coef.xgb.Booster}, with the intercept
28 corresponding to the first column).
29
30 When there is more than one coefficient per feature (e.g. multi-class classification),
31 the result will be reshaped into a vector where coefficients are arranged first by features and
32 then by class (e.g. first 1 through N coefficients will be for the first class, then
33 coefficients N+1 through 2N for the second class, and so on).
34
35 If the result has only one coefficient per feature in the data, then the resulting matrix
36 will have column names matching with the feature names, otherwise (when there's more than
37 one coefficient per feature) the names will be composed as 'column name' + ':' + 'class index'
38 (so e.g. column 'c1' for class '0' will be named 'c1:0').
39
40 With \code{\link[=xgb.train]{xgb.train()}}, the output is either a dense or a sparse matrix.
41 With with \code{\link[=xgb.cv]{xgb.cv()}}, it is a list (one element per each fold) of such matrices.
42
43 Function \link{xgb.gblinear.history} provides an easy way to retrieve the
44 outputs from this callback.
45 }
46 \examples{
47 #### Binary classification:
48
49 ## Keep the number of threads to 1 for examples
50 nthread <- 1
51 data.table::setDTthreads(nthread)
52
53 # In the iris dataset, it is hard to linearly separate Versicolor class from the rest
54 # without considering the 2nd order interactions:
55 x <- model.matrix(Species ~ .^2, iris)[, -1]
56 colnames(x)
57 dtrain <- xgb.DMatrix(
58 scale(x),
59 label = 1 * (iris$Species == "versicolor"),
60 nthread = nthread
61 )
62 param <- xgb.params(
63 booster = "gblinear",
64 objective = "reg:logistic",
65 eval_metric = "auc",
66 reg_lambda = 0.0003,
67 reg_alpha = 0.0003,
68 nthread = nthread
69 )
70
71 # For 'shotgun', which is a default linear updater, using high learning_rate values may result in
72 # unstable behaviour in some datasets. With this simple dataset, however, the high learning
73 # rate does not break the convergence, but allows us to illustrate the typical pattern of
74 # "stochastic explosion" behaviour of this lock-free algorithm at early boosting iterations.
75 bst <- xgb.train(
76 c(param, list(learning_rate = 1.)),
77 dtrain,
78 evals = list(tr = dtrain),
79 nrounds = 200,
80 callbacks = list(xgb.cb.gblinear.history())
81 )
82
83 # Extract the coefficients' path and plot them vs boosting iteration number:
84 coef_path <- xgb.gblinear.history(bst)
85 matplot(coef_path, type = "l")
86
87 # With the deterministic coordinate descent updater, it is safer to use higher learning rates.
88 # Will try the classical componentwise boosting which selects a single best feature per round:
89 bst <- xgb.train(
90 c(
91 param,
92 xgb.params(
93 learning_rate = 0.8,
94 updater = "coord_descent",
95 feature_selector = "thrifty",
96 top_k = 1
97 )
98 ),
99 dtrain,
100 evals = list(tr = dtrain),
101 nrounds = 200,
102 callbacks = list(xgb.cb.gblinear.history())
103 )
104 matplot(xgb.gblinear.history(bst), type = "l")
105 # Componentwise boosting is known to have similar effect to Lasso regularization.
106 # Try experimenting with various values of top_k, learning_rate, nrounds,
107 # as well as different feature_selectors.
108
109 # For xgb.cv:
110 bst <- xgb.cv(
111 c(
112 param,
113 xgb.params(
114 learning_rate = 0.8,
115 updater = "coord_descent",
116 feature_selector = "thrifty",
117 top_k = 1
118 )
119 ),
120 dtrain,
121 nfold = 5,
122 nrounds = 100,
123 callbacks = list(xgb.cb.gblinear.history())
124 )
125 # coefficients in the CV fold #3
126 matplot(xgb.gblinear.history(bst)[[3]], type = "l")
127
128
129 #### Multiclass classification:
130 dtrain <- xgb.DMatrix(scale(x), label = as.numeric(iris$Species) - 1, nthread = nthread)
131
132 param <- xgb.params(
133 booster = "gblinear",
134 objective = "multi:softprob",
135 num_class = 3,
136 reg_lambda = 0.0003,
137 reg_alpha = 0.0003,
138 nthread = nthread
139 )
140
141 # For the default linear updater 'shotgun' it sometimes is helpful
142 # to use smaller learning_rate to reduce instability
143 bst <- xgb.train(
144 c(param, list(learning_rate = 0.5)),
145 dtrain,
146 evals = list(tr = dtrain),
147 nrounds = 50,
148 callbacks = list(xgb.cb.gblinear.history())
149 )
150
151 # Will plot the coefficient paths separately for each class:
152 matplot(xgb.gblinear.history(bst, class_index = 0), type = "l")
153 matplot(xgb.gblinear.history(bst, class_index = 1), type = "l")
154 matplot(xgb.gblinear.history(bst, class_index = 2), type = "l")
155
156 # CV:
157 bst <- xgb.cv(
158 c(param, list(learning_rate = 0.5)),
159 dtrain,
160 nfold = 5,
161 nrounds = 70,
162 callbacks = list(xgb.cb.gblinear.history(FALSE))
163 )
164 # 1st fold of 1st class
165 matplot(xgb.gblinear.history(bst, class_index = 0)[[1]], type = "l")
166
167 }
168 \seealso{
169 \link{xgb.gblinear.history}, \link{coef.xgb.Booster}.
170 }