-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcatboost.R
402 lines (360 loc) · 11.5 KB
/
catboost.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#' Wrapper to add `catboost` engine to the parsnip `boost_tree` model
#' specification
#'
#' @return NULL
#' @export
add_boost_tree_catboost <- function() {
parsnip::set_model_engine("boost_tree", mode = "regression", eng = "catboost")
parsnip::set_model_engine("boost_tree", mode = "classification", eng = "catboost")
parsnip::set_dependency("boost_tree", eng = "catboost", pkg = "catboost")
parsnip::set_fit(
model = "boost_tree",
eng = "catboost",
mode = "regression",
value = list(
interface = "data.frame",
protect = c("x", "y"),
func = c(pkg = "treesnip", fun = "train_catboost"),
defaults = list()
)
)
parsnip::set_encoding(
model = "boost_tree",
mode = "regression",
eng = "catboost",
options = list(
predictor_indicators = "none",
compute_intercept = FALSE,
remove_intercept = FALSE
)
)
parsnip::set_encoding(
model = "boost_tree",
mode = "classification",
eng = "catboost",
options = list(
predictor_indicators = "none",
compute_intercept = FALSE,
remove_intercept = FALSE
)
)
parsnip::set_pred(
model = "boost_tree",
eng = "catboost",
mode = "regression",
type = "numeric",
value = list(
pre = NULL,
post = NULL,
func = c(fun = "predict"),
args = list(object = quote(object$fit), new_data = quote(new_data))
)
)
parsnip::set_pred(
model = "boost_tree",
eng = "catboost",
mode = "regression",
type = "raw",
value = list(
pre = NULL,
post = NULL,
func = c(fun = "predict"),
args = list(object = quote(object$fit), new_data = quote(new_data))
)
)
parsnip::set_fit(
model = "boost_tree",
eng = "catboost",
mode = "classification",
value = list(
interface = "data.frame",
protect = c("x", "y"),
func = c(pkg = "treesnip", fun = "train_catboost"),
defaults = list()
)
)
parsnip::set_pred(
model = "boost_tree",
eng = "catboost",
mode = "classification",
type = "class",
value = list(
pre = NULL,
post = function(x, object) {
if (is.vector(x)) {
x <- ifelse(x >= 0.5, object$lvl[2], object$lvl[1])
} else {
x <- object$lvl[apply(x, 1, which.max)]
}
x
},
func = c(pkg = NULL, fun = "predict"),
args = list(object = quote(object$fit), new_data = quote(new_data))
)
)
parsnip::set_pred(
model = "boost_tree",
eng = "catboost",
mode = "classification",
type = "prob",
value = list(
pre = NULL,
post = function(x, object) {
if (is.vector(x)) {
x <- tibble::tibble(v1 = 1 - x, v2 = x)
} else {
x <- tibble::as_tibble(x, .name_repair = make.names)
}
colnames(x) <- object$lvl
x
},
func = c(pkg = NULL, fun = "predict"),
args = list(object = quote(object$fit), new_data = quote(new_data))
)
)
parsnip::set_pred(
model = "boost_tree",
eng = "catboost",
mode = "classification",
type = "raw",
value = list(
pre = NULL,
post = NULL,
func = c(fun = "predict"),
args = list(object = quote(object$fit), new_data = quote(new_data))
)
)
# model args ----------------------------------------------------
parsnip::set_model_arg(
model = "boost_tree",
eng = "catboost",
parsnip = "tree_depth",
original = "depth",
func = list(pkg = "dials", fun = "tree_depth"),
has_submodel = FALSE
)
parsnip::set_model_arg(
model = "boost_tree",
eng = "catboost",
parsnip = "trees",
original = "iterations",
func = list(pkg = "dials", fun = "trees"),
has_submodel = TRUE
)
parsnip::set_model_arg(
model = "boost_tree",
eng = "catboost",
parsnip = "learn_rate",
original = "learning_rate",
func = list(pkg = "dials", fun = "learn_rate"),
has_submodel = FALSE
)
parsnip::set_model_arg(
model = "boost_tree",
eng = "catboost",
parsnip = "mtry",
original = "rsm",
func = list(pkg = "dials", fun = "mtry"),
has_submodel = FALSE
)
parsnip::set_model_arg(
model = "boost_tree",
eng = "catboost",
parsnip = "min_n",
original = "min_data_in_leaf",
func = list(pkg = "dials", fun = "min_n"),
has_submodel = FALSE
)
# parsnip::set_model_arg(
# model = "boost_tree",
# eng = "catboost",
# parsnip = "loss_reduction",
# original = "gamma", # There is no such parameter in catboost
# func = list(pkg = "dials", fun = "loss_reduction"),
# has_submodel = FALSE
# )
parsnip::set_model_arg(
model = "boost_tree",
eng = "catboost",
parsnip = "sample_size",
original = "subsample",
func = list(pkg = "dials", fun = "sample_size"),
has_submodel = FALSE
)
}
prepare_df_catboost <- function(x, y = NULL, categorical_cols= NULL) {
if(is.null(categorical_cols)){
# auto detect the categorical columns from data.frame
# Not strictly necessary but good form.
categorical_cols <- categorical_columns(x)
}
# catboost uses 0-indexed feature cols
if(!is.null(categorical_cols)){categorical_cols <- categorical_cols-1}
if (is.null(y))
return(x)
catboost::catboost.load_pool(
data = x,
label = y,
cat_features = categorical_cols
)
}
#' Boosted trees via catboost
#'
#' `catboost_train` is a wrapper for `catboost` tree-based models
#' where all of the model arguments are in the main function.
#'
#' @param x A data frame or matrix of predictors
#' @param y A vector (factor or numeric) or matrix (numeric) of outcome data.
#' @param depth An integer for the maximum depth of the tree.
#' @param iterations An integer for the number of boosting iterations.
#' @param learning_rate A numeric value between zero and one to control the learning rate.
#' @param rsm Subsampling proportion of columns.
#' @param min_data_in_leaf A numeric value for the minimum sum of instances needed
#' in a child to continue to split.
#' @param subsample Subsampling proportion of rows.
#' @param categorical_cols indices of categorical columns, when NULL (default) factor columns are automatically detected
#' @param ... Other options to pass to `catboost.train`.
#' @return A fitted `catboost.Model` object.
#' @keywords internal
#' @export
train_catboost <- function(x, y, depth = 6, iterations = 1000, learning_rate = NULL,
rsm = 1, min_data_in_leaf = 1, subsample = 1,
categorical_cols = NULL, ...) {
# learning rate --------------------
learning_rate <- max(learning_rate, 1e-6)
# rsm ------------------------------
if(!is.null(rsm)) {
rsm <- rsm/ncol(x)
}
if(rsm > 1) {
rsm <- 1
}
# subsample -----------------------
if (subsample > 1) {
subsample <- 1
}
# loss -------------------------
if (is.numeric(y)) {
loss_function <- "RMSE"
} else {
lvl <- levels(y)
y <- as.numeric(y) - 1
if (length(lvl) == 2) {
loss_function <- "Logloss"
} else {
loss_function <- "MultiClass"
}
}
arg_list <- list(
loss_function = loss_function,
iterations = iterations,
learning_rate = learning_rate,
depth = depth,
rsm = rsm,
min_data_in_leaf = min_data_in_leaf,
subsample = subsample
)
# train ------------------------
d <- prepare_df_catboost(x, y = y, categorical_cols = categorical_cols)
# override or add some other args
others <- list(...)
others <- others[!(names(others) %in% c("learn_pool", "test_pool", names(arg_list)))]
if(is.null(others$logging_level)) others$logging_level = "Silent"
if(is.null(others$bootstrap_type)) others$bootstrap_type = "Bernoulli" # subsample as is
if(is.null(others$sampling_frequency)) others$sampling_frequency = "PerTree" # subsample as is
# artificial alias for thread_count (for match xgboost and lightgbm)
if(is.null(others$thread_count) & is.null(others$nthread)) {
others$thread_count = 1L # parallelism should be explicitly specified by the user
} else {
others$thread_count = ifelse(!is.null(others$thread_count), others$thread_count, others$nthread)
others$nthread <- NULL
}
arg_list <- purrr::compact(c(arg_list, others))
main_args <- list(
learn_pool = quote(d),
params = arg_list
)
call <- parsnip:::make_call(fun = "catboost.train", ns = "catboost", main_args)
rlang::eval_tidy(call, env = rlang::current_env())
}
#' Model predictions across many sub-models
#'
#' For some models, predictions can be made on sub-models in the model object.
#'
#' @param object A model_fit object.
#' @param ... Optional arguments to pass to predict.model_fit(type = "raw") such as type.
#' @param new_data A rectangular data object, such as a data frame.
#' @param type A single character value or NULL. Possible values are "numeric", "class", "prob", "conf_int", "pred_int", "quantile", or "raw". When NULL, predict() will choose an appropriate value based on the model's mode.
#' @param trees An integer vector for the number of trees in the ensemble.
#' @param categorical_cols indices of categorical columns, when NULL (default) factor columns are automatically detected.
#'
#' @export
#' @importFrom purrr map_df
#' @importFrom parsnip multi_predict
multi_predict._catboost.Model <- function(object, new_data, type = NULL, trees = NULL, categorical_cols = NULL, ...) {
if (any(names(rlang::enquos(...)) == "newdata")) {
rlang::abort("Did you mean to use `new_data` instead of `newdata`?")
}
if (is.null(trees)) {
trees <- object$fit$tree_count
}
trees <- sort(trees)
if (is.null(type)) {
if (object$spec$mode == "classification")
type <- "Class"
else
type <- "RawFormulaVal"
} else {
type <- switch (
type,
"raw" = "RawFormulaVal",
"numeric" = "RawFormulaVal",
"class" = "Class",
"prob" = "Probability",
type
)
}
res <- map_df(trees, catboost_by_tree, object = object, new_data = new_data, type = type, categorical_cols = categorical_cols, ...)
res <- dplyr::arrange(res, .row, trees)
res <- split(res[, -1], res$.row)
names(res) <- NULL
tibble::tibble(.pred = res)
}
catboost_by_tree <- function(tree, object, new_data, type, categorical_cols = NULL,...) {
d <- prepare_df_catboost(new_data, categorical_cols = categorical_cols)
pred <- predict.catboost.Model(object$fit, d, ntree_end = tree, type = type, categorical_cols = categorical_cols, ...)
# switch based on prediction type
if (object$spec$mode == "regression") {
pred <- tibble::tibble(.pred = pred)
nms <- names(pred)
} else {
if (type == "Class") {
pred <- object$spec$method$pred$class$post(pred, object)
pred <- tibble::tibble(.pred_class = factor(pred, levels = object$lvl))
} else {
pred <- object$spec$method$pred$prob$post(pred, object)
pred <- tibble::as_tibble(pred)
names(pred) <- paste0(".pred_", names(pred))
}
nms <- names(pred)
}
pred[["trees"]] <- tree
pred[[".row"]] <- 1:nrow(new_data)
pred[, c(".row", "trees", nms)]
}
#' @export
predict.catboost.Model <- function(object, new_data, type = "RawFormulaVal", categorical_cols = NULL, ...) {
if (!inherits(new_data, "catboost.Pool")) {
d <- prepare_df_catboost(new_data, categorical_cols = categorical_cols)
new_data <- catboost::catboost.load_pool(d, cat_features = categorical_cols)
}
prediction_type <- switch (
type,
"raw" = "RawFormulaVal",
"numeric" = "RawFormulaVal",
"class" = "Class",
"prob" = "Probability",
type
)
catboost::catboost.predict(object, new_data, prediction_type = prediction_type, ...)
}