lgb.Booster.R 17.1 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
Booster <- R6Class(
  "lgb.Booster",
3
  cloneable = FALSE,
Guolin Ke's avatar
Guolin Ke committed
4
  public = list(
5
    best_iter    = -1,
Guolin Ke's avatar
Guolin Ke committed
6
    record_evals = list(),
7
8
9
10
    finalize     = function() {
      if (!lgb.is.null.handle(private$handle)) {
        cat("freeing booster handle\n")
        lgb.call("LGBM_BoosterFree_R", ret = NULL, private$handle)
Guolin Ke's avatar
Guolin Ke committed
11
12
        private$handle <- NULL
      }
13
14
    },
    initialize = function(params    = list(),
Guolin Ke's avatar
Guolin Ke committed
15
16
17
                          train_set = NULL,
                          modelfile = NULL,
                          ...) {
18
      params     <- append(params, list(...))
Guolin Ke's avatar
Guolin Ke committed
19
      params_str <- lgb.params2str(params)
20
      handle     <- lgb.new.handle()
Guolin Ke's avatar
Guolin Ke committed
21
22
      if (!is.null(train_set)) {
        if (!lgb.check.r6.class(train_set, "lgb.Dataset")) {
23
          stop("lgb.Booster: Can only use lgb.Dataset as training data")
Guolin Ke's avatar
Guolin Ke committed
24
        }
25

Guolin Ke's avatar
Guolin Ke committed
26
        handle <-
27
28
29
30
          lgb.call("LGBM_BoosterCreate_R", ret = handle, train_set$.__enclos_env__$private$get_handle(), params_str)

        private$train_set      <- train_set
        private$num_dataset    <- 1
Guolin Ke's avatar
Guolin Ke committed
31
32
        private$init_predictor <- train_set$.__enclos_env__$private$predictor
        if (!is.null(private$init_predictor)) {
33
          lgb.call("LGBM_BoosterMerge_R", ret = NULL,
Guolin Ke's avatar
Guolin Ke committed
34
35
36
                handle,
                private$init_predictor$.__enclos_env__$private$handle)
        }
37
        private$is_predicted_cur_iter <- c(private$is_predicted_cur_iter, FALSE)
Guolin Ke's avatar
Guolin Ke committed
38
39
      } else if (!is.null(modelfile)) {
        if (!is.character(modelfile)) {
40
          stop("lgb.Booster: Can only use a string as model file path")
Guolin Ke's avatar
Guolin Ke committed
41
42
43
        }
        handle <-
          lgb.call("LGBM_BoosterCreateFromModelfile_R",
44
            ret = handle,
Guolin Ke's avatar
Guolin Ke committed
45
46
47
            lgb.c_str(modelfile))
      } else {
        stop(
48
          "lgb.Booster: Need at least either training dataset or model file to create booster instance"
Guolin Ke's avatar
Guolin Ke committed
49
50
        )
      }
51
52
      class(handle)     <- "lgb.Booster.handle"
      private$handle    <- handle
Guolin Ke's avatar
Guolin Ke committed
53
54
      private$num_class <- as.integer(1)
      private$num_class <-
55
        lgb.call("LGBM_BoosterGetNumClasses_R", ret = private$num_class, private$handle)
Guolin Ke's avatar
Guolin Ke committed
56
57
58
    },
    set_train_data_name = function(name) {
      private$name_train_set <- name
59
      self
Guolin Ke's avatar
Guolin Ke committed
60
61
62
    },
    add_valid = function(data, name) {
      if (!lgb.check.r6.class(data, "lgb.Dataset")) {
63
        stop("lgb.Booster.add_valid: Can only use lgb.Dataset as validation data")
Guolin Ke's avatar
Guolin Ke committed
64
65
66
      }
      if (!identical(data$.__enclos_env__$private$predictor, private$init_predictor)) {
        stop(
67
          "lgb.Booster.add_valid: Failed to add validation data; you should use the same predictor for these data"
Guolin Ke's avatar
Guolin Ke committed
68
69
        )
      }
70
71
      if (!is.character(name)) {
        stop("lgb.Booster.add_valid: Can only use characters as data name")
Guolin Ke's avatar
Guolin Ke committed
72
      }
73
74
75
76
      lgb.call("LGBM_BoosterAddValidData_R", ret = NULL, private$handle, data$.__enclos_env__$private$get_handle())
      private$valid_sets            <- c(private$valid_sets, data)
      private$name_valid_sets       <- c(private$name_valid_sets, name)
      private$num_dataset           <- private$num_dataset + 1
Guolin Ke's avatar
Guolin Ke committed
77
78
      private$is_predicted_cur_iter <-
        c(private$is_predicted_cur_iter, FALSE)
79
80

      self
Guolin Ke's avatar
Guolin Ke committed
81
82
    },
    reset_parameter = function(params, ...) {
83
      params     <- append(params, list(...))
Guolin Ke's avatar
Guolin Ke committed
84
      params_str <- algb.params2str(params)
85
      lgb.call("LGBM_BoosterResetParameter_R", ret = NULL,
Guolin Ke's avatar
Guolin Ke committed
86
87
            private$handle,
            params_str)
88
      self
Guolin Ke's avatar
Guolin Ke committed
89
90
91
92
93
94
95
96
    },
    update = function(train_set = NULL, fobj = NULL) {
      if (!is.null(train_set)) {
        if (!lgb.check.r6.class(train_set, "lgb.Dataset")) {
          stop("lgb.Booster.update: Only can use lgb.Dataset as training data")
        }
        if (!identical(train_set$predictor, private$init_predictor)) {
          stop(
97
            "lgb.Booster.update: Change train_set failed, you should use the same predictor for these data"
Guolin Ke's avatar
Guolin Ke committed
98
99
          )
        }
100
        lgb.call("LGBM_BoosterResetTrainingData_R", ret = NULL,
Guolin Ke's avatar
Guolin Ke committed
101
102
103
104
105
              private$handle,
              train_set$.__enclos_env__$private$get_handle())
        private$train_set = train_set
      }
      if (is.null(fobj)) {
106
        ret <- lgb.call("LGBM_BoosterUpdateOneIter_R", ret = NULL, private$handle)
Guolin Ke's avatar
Guolin Ke committed
107
      } else {
108
        if (!is.function(fobj)) { stop("lgb.Booster.update: fobj should be a function") }
Guolin Ke's avatar
Guolin Ke committed
109
        gpair <- fobj(private$inner_predict(1), private$train_set)
110
111
        ret   <- lgb.call(
            "LGBM_BoosterUpdateOneIterCustom_R", ret = NULL,
Guolin Ke's avatar
Guolin Ke committed
112
113
114
115
116
117
            private$handle,
            gpair$grad,
            gpair$hess,
            length(gpair$grad)
          )
      }
118
      for (i in seq_along(private$is_predicted_cur_iter)) {
Guolin Ke's avatar
Guolin Ke committed
119
120
        private$is_predicted_cur_iter[[i]] <- FALSE
      }
121
      ret
Guolin Ke's avatar
Guolin Ke committed
122
123
    },
    rollback_one_iter = function() {
124
125
      lgb.call("LGBM_BoosterRollbackOneIter_R", ret = NULL, private$handle)
      for (i in seq_along(private$is_predicted_cur_iter)) {
Guolin Ke's avatar
Guolin Ke committed
126
127
        private$is_predicted_cur_iter[[i]] <- FALSE
      }
128
      self
Guolin Ke's avatar
Guolin Ke committed
129
130
131
    },
    current_iter = function() {
      cur_iter <- as.integer(0)
132
      lgb.call("LGBM_BoosterGetCurrentIteration_R", ret = cur_iter, private$handle)
Guolin Ke's avatar
Guolin Ke committed
133
134
135
    },
    eval = function(data, name, feval = NULL) {
      if (!lgb.check.r6.class(data, "lgb.Dataset")) {
136
        stop("lgb.Booster.eval: Can only use lgb.Dataset to eval")
Guolin Ke's avatar
Guolin Ke committed
137
138
      }
      data_idx <- 0
139
140
141
      if (identical(data, private$train_set)) { data_idx <- 1 } else {
        if (length(private$valid_sets) > 0) {
          for (i in seq_along(private$valid_sets)) {
Guolin Ke's avatar
Guolin Ke committed
142
143
144
145
146
147
148
149
150
151
152
            if (identical(data, private$valid_sets[[i]])) {
              data_idx <- i + 1
              break
            }
          }
        }
      }
      if (data_idx == 0) {
        self$add_valid(data, name)
        data_idx <- private$num_dataset
      }
153
      private$inner_eval(name, data_idx, feval)
Guolin Ke's avatar
Guolin Ke committed
154
155
    },
    eval_train = function(feval = NULL) {
156
      private$inner_eval(private$name_train_set, 1, feval)
Guolin Ke's avatar
Guolin Ke committed
157
158
159
    },
    eval_valid = function(feval = NULL) {
      ret = list()
160
161
162
      if (length(private$valid_sets) <= 0) { return(ret) }
      for (i in seq_along(private$valid_sets)) {
        ret <- append(ret, private$inner_eval(private$name_valid_sets[[i]], i + 1, feval))
Guolin Ke's avatar
Guolin Ke committed
163
      }
164
      ret
Guolin Ke's avatar
Guolin Ke committed
165
166
    },
    save_model = function(filename, num_iteration = NULL) {
167
      if (is.null(num_iteration)) { num_iteration <- self$best_iter }
Guolin Ke's avatar
Guolin Ke committed
168
169
170
171
172
173
174
      lgb.call(
        "LGBM_BoosterSaveModel_R",
        ret = NULL,
        private$handle,
        as.integer(num_iteration),
        lgb.c_str(filename)
      )
175
      self
Guolin Ke's avatar
Guolin Ke committed
176
177
    },
    dump_model = function(num_iteration = NULL) {
178
179
180
181
182
      if (is.null(num_iteration)) { num_iteration <- self$best_iter }
      lgb.call.return.str(
        "LGBM_BoosterDumpModel_R",
        private$handle,
        as.integer(num_iteration)
Guolin Ke's avatar
Guolin Ke committed
183
184
185
      )
    },
    predict = function(data,
186
187
188
189
190
191
                      num_iteration = NULL,
                      rawscore      = FALSE,
                      predleaf      = FALSE,
                      header        = FALSE,
                      reshape       = FALSE) {
      if (is.null(num_iteration)) { num_iteration <- self$best_iter }
Guolin Ke's avatar
Guolin Ke committed
192
      predictor <- Predictor$new(private$handle)
193
      predictor$predict(data, num_iteration, rawscore, predleaf, header, reshape)
Guolin Ke's avatar
Guolin Ke committed
194
    },
195
    to_predictor = function() { Predictor$new(private$handle) }
Guolin Ke's avatar
Guolin Ke committed
196
197
  ),
  private = list(
198
199
200
201
202
203
204
205
206
207
208
    handle                   = NULL,
    train_set                = NULL,
    name_train_set           = "training",
    valid_sets               = list(),
    name_valid_sets          = list(),
    predict_buffer           = list(),
    is_predicted_cur_iter    = list(),
    num_class                = 1,
    num_dataset              = 0,
    init_predictor           = NULL,
    eval_names               = NULL,
Guolin Ke's avatar
Guolin Ke committed
209
    higher_better_inner_eval = NULL,
210
    inner_predict            = function(idx) {
Guolin Ke's avatar
Guolin Ke committed
211
      data_name <- private$name_train_set
212
      if (idx > 1) { data_name <- private$name_valid_sets[[idx - 1]] }
Guolin Ke's avatar
Guolin Ke committed
213
214
215
216
217
      if (idx > private$num_dataset) {
        stop("data_idx should not be greater than num_dataset")
      }
      if (is.null(private$predict_buffer[[data_name]])) {
        npred <- as.integer(0)
218
        npred <- lgb.call("LGBM_BoosterGetNumPredict_R",
Guolin Ke's avatar
Guolin Ke committed
219
220
221
222
223
224
                ret = npred,
                private$handle,
                as.integer(idx - 1))
        private$predict_buffer[[data_name]] <- rep(0.0, npred)
      }
      if (!private$is_predicted_cur_iter[[idx]]) {
225
        private$predict_buffer[[data_name]] <- lgb.call(
Guolin Ke's avatar
Guolin Ke committed
226
            "LGBM_BoosterGetPredict_R",
227
            ret = private$predict_buffer[[data_name]],
Guolin Ke's avatar
Guolin Ke committed
228
229
230
231
232
            private$handle,
            as.integer(idx - 1)
          )
        private$is_predicted_cur_iter[[idx]] <- TRUE
      }
233
      private$predict_buffer[[data_name]]
Guolin Ke's avatar
Guolin Ke committed
234
235
236
    },
    get_eval_info = function() {
      if (is.null(private$eval_names)) {
237
238
        names <- lgb.call.return.str("LGBM_BoosterGetEvalNames_R", private$handle)
        if (nchar(names) > 0) {
Guolin Ke's avatar
Guolin Ke committed
239
240
          names <- strsplit(names, "\t")[[1]]
          private$eval_names <- names
241
242
          private$higher_better_inner_eval <- rep(FALSE, length(names))
          for (i in seq_along(names)) {
243
244
            if (names[i]) == "auc" |
                grepl("^ndcg", names[i])) {
Guolin Ke's avatar
Guolin Ke committed
245
246
247
248
249
              private$higher_better_inner_eval[i] <- TRUE
            }
          }
        }
      }
250
      private$eval_names
Guolin Ke's avatar
Guolin Ke committed
251
252
253
254
255
256
257
258
259
    },
    inner_eval = function(data_name, data_idx, feval = NULL) {
      if (data_idx > private$num_dataset) {
        stop("data_idx should not be greater than num_dataset")
      }
      private$get_eval_info()
      ret <- list()
      if (length(private$eval_names) > 0) {
        tmp_vals <- rep(0.0, length(private$eval_names))
260
        tmp_vals <- lgb.call("LGBM_BoosterGetEval_R", ret = tmp_vals,
Guolin Ke's avatar
Guolin Ke committed
261
262
                private$handle,
                as.integer(data_idx - 1))
263
264
265
266
267
        for (i in seq_along(private$eval_names)) {
          res               <- list()
          res$data_name     <- data_name
          res$name          <- private$eval_names[i]
          res$value         <- tmp_vals[i]
Guolin Ke's avatar
Guolin Ke committed
268
          res$higher_better <- private$higher_better_inner_eval[i]
269
          ret               <- append(ret, list(res))
Guolin Ke's avatar
Guolin Ke committed
270
271
272
        }
      }
      if (!is.null(feval)) {
273
        if (!is.function(feval)) {
Guolin Ke's avatar
Guolin Ke committed
274
275
276
          stop("lgb.Booster.eval: feval should be a function")
        }
        data <- private$train_set
277
278
        if (data_idx > 1) { data <- private$valid_sets[[data_idx - 1]] }
        res           <- feval(private$inner_predict(data_idx), data)
Guolin Ke's avatar
Guolin Ke committed
279
        res$data_name <- data_name
280
        ret           <- append(ret, list(res))
Guolin Ke's avatar
Guolin Ke committed
281
      }
282
      ret
Guolin Ke's avatar
Guolin Ke committed
283
284
285
286
287
288
    }
  )
)


#' Predict method for LightGBM model
289
#'
Guolin Ke's avatar
Guolin Ke committed
290
#' Predicted values based on class \code{lgb.Booster}
291
#'
Guolin Ke's avatar
Guolin Ke committed
292
293
294
#' @param object Object of class \code{lgb.Booster}
#' @param data a \code{matrix} object, a \code{dgCMatrix} object or a character representing a filename
#' @param num_iteration number of iteration want to predict with, NULL or <= 0 means use best iteration
295
296
#' @param rawscore whether the prediction should be returned in the for of original untransformed
#'        sum of predictions from boosting iterations' results. E.g., setting \code{rawscore=TRUE} for
Guolin Ke's avatar
Guolin Ke committed
297
#'        logistic regression would result in predictions for log-odds instead of probabilities.
298
#' @param predleaf whether predict leaf index instead.
Guolin Ke's avatar
Guolin Ke committed
299
#' @param header only used for prediction for text file. True if text file has header
300
301
#' @param reshape whether to reshape the vector of predictions to a matrix form when there are several
#'        prediction outputs per case.
Guolin Ke's avatar
Guolin Ke committed
302

303
#' @return
Guolin Ke's avatar
Guolin Ke committed
304
#' For regression or binary classification, it returns a vector of length \code{nrows(data)}.
305
306
#' For multiclass classification, either a \code{num_class * nrows(data)} vector or
#' a \code{(nrows(data), num_class)} dimension matrix is returned, depending on
Guolin Ke's avatar
Guolin Ke committed
307
#' the \code{reshape} value.
308
309
#'
#' When \code{predleaf = TRUE}, the output is a matrix object with the
Guolin Ke's avatar
Guolin Ke committed
310
311
#' number of columns corresponding to the number of trees.
#' @examples
312
313
314
315
316
317
318
319
320
321
322
323
324
#' \dontrun{
#'   library(lightgbm)
#'   data(agaricus.train, package='lightgbm')
#'   train <- agaricus.train
#'   dtrain <- lgb.Dataset(train$data, label=train$label)
#'   data(agaricus.test, package='lightgbm')
#'   test <- agaricus.test
#'   dtest <- lgb.Dataset.create.valid(dtrain, test$data, label=test$label)
#'   params <- list(objective="regression", metric="l2")
#'   valids <- list(test=dtest)
#'   model <- lgb.train(params, dtrain, 100, valids, min_data=1, learning_rate=1, early_stopping_rounds=10)
#'   preds <- predict(model, test$data)
#' }
Guolin Ke's avatar
Guolin Ke committed
325
326
#' @rdname predict.lgb.Booster
#' @export
327
predict.lgb.Booster <- function(object, data,
Guolin Ke's avatar
Guolin Ke committed
328
                        num_iteration = NULL,
329
330
331
332
333
334
                        rawscore      = FALSE,
                        predleaf      = FALSE,
                        header        = FALSE,
                        reshape       = FALSE) {
  if (!lgb.is.Booster(object)) {
    stop("predict.lgb.Booster: object should be an ", sQuote("lgb.Booster"))
Guolin Ke's avatar
Guolin Ke committed
335
336
337
338
339
  }
  object$predict(data, num_iteration, rawscore, predleaf, header, reshape)
}

#' Load LightGBM model
340
#'
Guolin Ke's avatar
Guolin Ke committed
341
#' Load LightGBM model from saved model file
342
#'
Guolin Ke's avatar
Guolin Ke committed
343
#' @param filename path of model file
344
#'
Guolin Ke's avatar
Guolin Ke committed
345
346
#' @return booster
#' @examples
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#' \dontrun{
#'   library(lightgbm)
#'   data(agaricus.train, package='lightgbm')
#'   train <- agaricus.train
#'   dtrain <- lgb.Dataset(train$data, label=train$label)
#'   data(agaricus.test, package='lightgbm')
#'   test <- agaricus.test
#'   dtest <- lgb.Dataset.create.valid(dtrain, test$data, label=test$label)
#'   params <- list(objective="regression", metric="l2")
#'   valids <- list(test=dtest)
#'   model <- lgb.train(params, dtrain, 100, valids, min_data=1, learning_rate=1, early_stopping_rounds=10)
#'   lgb.save(model, "model.txt")
#'   load_booster <- lgb.load("model.txt")
#' }
#' @rdname lgb.load
Guolin Ke's avatar
Guolin Ke committed
362
363
#' @export
lgb.load <- function(filename){
364
365
  if (!is.character(filename)) { stop("lgb.load: filename should be character") }
  Booster$new(modelfile = filename)
Guolin Ke's avatar
Guolin Ke committed
366
367
368
}

#' Save LightGBM model
369
#'
Guolin Ke's avatar
Guolin Ke committed
370
#' Save LightGBM model
371
#'
Guolin Ke's avatar
Guolin Ke committed
372
373
374
#' @param booster Object of class \code{lgb.Booster}
#' @param filename saved filename
#' @param num_iteration number of iteration want to predict with, NULL or <= 0 means use best iteration
375
#'
Guolin Ke's avatar
Guolin Ke committed
376
377
#' @return booster
#' @examples
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#' \dontrun{
#'   library(lightgbm)
#'   data(agaricus.train, package='lightgbm')
#'   train <- agaricus.train
#'   dtrain <- lgb.Dataset(train$data, label=train$label)
#'   data(agaricus.test, package='lightgbm')
#'   test <- agaricus.test
#'   dtest <- lgb.Dataset.create.valid(dtrain, test$data, label=test$label)
#'   params <- list(objective="regression", metric="l2")
#'   valids <- list(test=dtest)
#'   model <- lgb.train(params, dtrain, 100, valids, min_data=1, learning_rate=1, early_stopping_rounds=10)
#'   lgb.save(model, "model.txt")
#' }
#' @rdname lgb.save
Guolin Ke's avatar
Guolin Ke committed
392
#' @export
393
394
395
lgb.save <- function(booster, filename, num_iteration = NULL){
  if (!lgb.is.Booster(booster)) { stop("lgb.save: booster should be an ", sQuote("lgb.Booster")) }
  if (!is.character(filename)) { stop("lgb.save: filename should be a character") }
Guolin Ke's avatar
Guolin Ke committed
396
397
398
399
  booster$save_model(filename, num_iteration)
}

#' Dump LightGBM model to json
400
#'
Guolin Ke's avatar
Guolin Ke committed
401
#' Dump LightGBM model to json
402
#'
Guolin Ke's avatar
Guolin Ke committed
403
404
#' @param booster Object of class \code{lgb.Booster}
#' @param num_iteration number of iteration want to predict with, NULL or <= 0 means use best iteration
405
#'
Guolin Ke's avatar
Guolin Ke committed
406
407
#' @return json format of model
#' @examples
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#' \dontrun{
#'   library(lightgbm)
#'   data(agaricus.train, package='lightgbm')
#'   train <- agaricus.train
#'   dtrain <- lgb.Dataset(train$data, label=train$label)
#'   data(agaricus.test, package='lightgbm')
#'   test <- agaricus.test
#'   dtest <- lgb.Dataset.create.valid(dtrain, test$data, label=test$label)
#'   params <- list(objective="regression", metric="l2")
#'   valids <- list(test=dtest)
#'   model <- lgb.train(params, dtrain, 100, valids, min_data=1, learning_rate=1, early_stopping_rounds=10)
#'   json_model <- lgb.dump(model)
#' }
#' @rdname lgb.dump
Guolin Ke's avatar
Guolin Ke committed
422
#' @export
423
424
lgb.dump <- function(booster, num_iteration = NULL){
  if (!lgb.is.Booster(booster)) { stop("lgb.save: booster should be an ", sQuote("lgb.Booster")) }
Guolin Ke's avatar
Guolin Ke committed
425
426
427
428
  booster$dump_model(num_iteration)
}

#' Get record evaluation result from booster
429
#'
Guolin Ke's avatar
Guolin Ke committed
430
431
432
433
434
435
436
437
438
#' Get record evaluation result from booster
#' @param booster Object of class \code{lgb.Booster}
#' @param data_name name of dataset
#' @param eval_name name of evaluation
#' @param iters iterations, NULL will return all
#' @param is_err TRUE will return evaluation error instead
#' @return vector of evaluation result
#' @rdname lgb.get.eval.result
#' @export
439
440
441
lgb.get.eval.result <- function(booster, data_name, eval_name, iters = NULL, is_err = FALSE) {
  if (!lgb.is.Booster(booster)) {
    stop("lgb.get.eval.result: Can only use ", sQuote("lgb.Booster"), " to get eval result")
Guolin Ke's avatar
Guolin Ke committed
442
  }
443
444
  if (!is.character(data_name) || !is.character(eval_name)) {
    stop("lgb.get.eval.result: data_name and eval_name should be characters")
Guolin Ke's avatar
Guolin Ke committed
445
  }
446
  if (is.null(booster$record_evals[[data_name]])) {
Guolin Ke's avatar
Guolin Ke committed
447
448
    stop("lgb.get.eval.result: wrong data name")
  }
449
  if (is.null(booster$record_evals[[data_name]][[eval_name]])) {
Guolin Ke's avatar
Guolin Ke committed
450
451
452
    stop("lgb.get.eval.result: wrong eval name")
  }
  result <- booster$record_evals[[data_name]][[eval_name]]$eval
453
  if (is_err) {
Guolin Ke's avatar
Guolin Ke committed
454
455
    result <- booster$record_evals[[data_name]][[eval_name]]$eval_err
  }
456
  if (is.null(iters)) {
Guolin Ke's avatar
Guolin Ke committed
457
458
459
460
461
    return(as.numeric(result))
  }
  iters <- as.integer(iters)
  delta <- booster$record_evals$start_iter - 1
  iters <- iters - delta
462
  as.numeric(result[iters])
Guolin Ke's avatar
Guolin Ke committed
463
}