lgb.Booster.R 27.7 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
Booster <- R6Class(
2
  classname = "lgb.Booster",
3
  cloneable = FALSE,
Guolin Ke's avatar
Guolin Ke committed
4
  public = list(
5
6
    
    best_iter = -1,
Laurae's avatar
Laurae committed
7
    best_score = -1,
Guolin Ke's avatar
Guolin Ke committed
8
    record_evals = list(),
9
10
11
12
13
    
    # Finalize will free up the handles
    finalize = function() {
      
      # Check the need for freeing handle
14
      if (!lgb.is.null.handle(private$handle)) {
15
16
        
        # Freeing up handle
17
        lgb.call("LGBM_BoosterFree_R", ret = NULL, private$handle)
Guolin Ke's avatar
Guolin Ke committed
18
        private$handle <- NULL
19
        
Guolin Ke's avatar
Guolin Ke committed
20
      }
21
      
22
    },
23
24
25
    
    # Initialize will create a starter booster
    initialize = function(params = list(),
Guolin Ke's avatar
Guolin Ke committed
26
27
                          train_set = NULL,
                          modelfile = NULL,
28
                          model_str = NULL,
Guolin Ke's avatar
Guolin Ke committed
29
                          ...) {
30
31
32
      
      # Create parameters and handle
      params <- append(params, list(...))
Guolin Ke's avatar
Guolin Ke committed
33
      params_str <- lgb.params2str(params)
Guolin Ke's avatar
Guolin Ke committed
34
      handle <- 0.0
35
36
37
      
      # Attempts to create a handle for the dataset
      try({
38
        
39
40
        # Check if training dataset is not null
        if (!is.null(train_set)) {
41
          
42
43
44
45
          # Check if training dataset is lgb.Dataset or not
          if (!lgb.check.r6.class(train_set, "lgb.Dataset")) {
            stop("lgb.Booster: Can only use lgb.Dataset as training data")
          }
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
          # Store booster handle
          handle <- lgb.call("LGBM_BoosterCreate_R", ret = handle, train_set$.__enclos_env__$private$get_handle(), params_str)
          
          # Create private booster information
          private$train_set <- train_set
          private$num_dataset <- 1
          private$init_predictor <- train_set$.__enclos_env__$private$predictor
          
          # Check if predictor is existing
          if (!is.null(private$init_predictor)) {
            
            # Merge booster
            lgb.call("LGBM_BoosterMerge_R",
                     ret = NULL,
                     handle,
                     private$init_predictor$.__enclos_env__$private$handle)
            
          }
          
          # Check current iteration
          private$is_predicted_cur_iter <- c(private$is_predicted_cur_iter, FALSE)
          
        } else if (!is.null(modelfile)) {
          
          # Do we have a model file as character?
          if (!is.character(modelfile)) {
            stop("lgb.Booster: Can only use a string as model file path")
          }
          
          # Create booster from model
          handle <- lgb.call("LGBM_BoosterCreateFromModelfile_R",
                             ret = handle,
                             lgb.c_str(modelfile))
          
        } else if (!is.null(model_str)) {
          
          # Do we have a model_str as character?
84
85
86
87
88
89
90
91
          if (!is.character(model_str)) {
            stop("lgb.Booster: Can only use a string as model_str")
          }
          
          # Create booster from model
          handle <- lgb.call("LGBM_BoosterLoadModelFromString_R",
                             ret = handle,
                             lgb.c_str(model_str))
92
93
94
95
96
97
98
          
        } else {
          
          # Booster non existent
          stop("lgb.Booster: Need at least either training dataset, model file, or model_str to create booster instance")
          
        }
99
        
100
101
102
      })
      
      # Check whether the handle was created properly if it was not stopped earlier by a stop call
Guolin Ke's avatar
Guolin Ke committed
103
      if (lgb.is.null.handle(handle)) {
104
        
Guolin Ke's avatar
Guolin Ke committed
105
        stop("lgb.Booster: cannot create Booster handle")
106
        
Guolin Ke's avatar
Guolin Ke committed
107
      } else {
108
        
Guolin Ke's avatar
Guolin Ke committed
109
110
111
112
113
114
115
        # Create class
        class(handle) <- "lgb.Booster.handle"
        private$handle <- handle
        private$num_class <- 1L
        private$num_class <- lgb.call("LGBM_BoosterGetNumClasses_R",
                                      ret = private$num_class,
                                      private$handle)
116
        
Guolin Ke's avatar
Guolin Ke committed
117
      }
118
      
Guolin Ke's avatar
Guolin Ke committed
119
    },
120
121
    
    # Set training data name
Guolin Ke's avatar
Guolin Ke committed
122
    set_train_data_name = function(name) {
123
124
      
      # Set name
Guolin Ke's avatar
Guolin Ke committed
125
      private$name_train_set <- name
126
      return(invisible(self))
127
      
Guolin Ke's avatar
Guolin Ke committed
128
    },
129
130
    
    # Add validation data
Guolin Ke's avatar
Guolin Ke committed
131
    add_valid = function(data, name) {
132
133
      
      # Check if data is lgb.Dataset
Guolin Ke's avatar
Guolin Ke committed
134
      if (!lgb.check.r6.class(data, "lgb.Dataset")) {
135
        stop("lgb.Booster.add_valid: Can only use lgb.Dataset as validation data")
Guolin Ke's avatar
Guolin Ke committed
136
      }
137
138
      
      # Check if predictors are identical
Guolin Ke's avatar
Guolin Ke committed
139
      if (!identical(data$.__enclos_env__$private$predictor, private$init_predictor)) {
140
        stop("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
141
      }
142
143
      
      # Check if names are character
144
145
      if (!is.character(name)) {
        stop("lgb.Booster.add_valid: Can only use characters as data name")
Guolin Ke's avatar
Guolin Ke committed
146
      }
147
148
149
150
151
152
153
154
155
156
157
158
159
160
      
      # Add validation data to booster
      lgb.call("LGBM_BoosterAddValidData_R",
               ret = NULL,
               private$handle,
               data$.__enclos_env__$private$get_handle())
      
      # Store private information
      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
      private$is_predicted_cur_iter <- c(private$is_predicted_cur_iter, FALSE)
      
      # Return self
161
      return(invisible(self))
162
      
Guolin Ke's avatar
Guolin Ke committed
163
    },
164
165
    
    # Reset parameters of booster
Guolin Ke's avatar
Guolin Ke committed
166
    reset_parameter = function(params, ...) {
167
168
169
      
      # Append parameters
      params <- append(params, list(...))
170
      params_str <- lgb.params2str(params)
171
172
173
174
175
176
177
178
      
      # Reset parameters
      lgb.call("LGBM_BoosterResetParameter_R",
               ret = NULL,
               private$handle,
               params_str)
      
      # Return self
179
      return(invisible(self))
180
      
Guolin Ke's avatar
Guolin Ke committed
181
    },
182
183
    
    # Perform boosting update iteration
Guolin Ke's avatar
Guolin Ke committed
184
    update = function(train_set = NULL, fobj = NULL) {
185
186
      
      # Check if training set is not null
Guolin Ke's avatar
Guolin Ke committed
187
      if (!is.null(train_set)) {
188
189
        
        # Check if training set is lgb.Dataset
Guolin Ke's avatar
Guolin Ke committed
190
191
192
        if (!lgb.check.r6.class(train_set, "lgb.Dataset")) {
          stop("lgb.Booster.update: Only can use lgb.Dataset as training data")
        }
193
194
        
        # Check if predictors are identical
Guolin Ke's avatar
Guolin Ke committed
195
        if (!identical(train_set$predictor, private$init_predictor)) {
196
          stop("lgb.Booster.update: Change train_set failed, you should use the same predictor for these data")
Guolin Ke's avatar
Guolin Ke committed
197
        }
198
199
200
201
202
203
204
205
        
        # Reset training data on booster
        lgb.call("LGBM_BoosterResetTrainingData_R",
                 ret = NULL,
                 private$handle,
                 train_set$.__enclos_env__$private$get_handle())
        
        # Store private train set
Guolin Ke's avatar
Guolin Ke committed
206
        private$train_set = train_set
207
        
Guolin Ke's avatar
Guolin Ke committed
208
      }
209
210
      
      # Check if objective is empty
Guolin Ke's avatar
Guolin Ke committed
211
      if (is.null(fobj)) {
212
213
214
        if (private$set_objective_to_none) {
          stop("lgb.Booster.update: cannot update due to null objective function")
        }
215
        # Boost iteration from known objective
216
        ret <- lgb.call("LGBM_BoosterUpdateOneIter_R", ret = NULL, private$handle)
217
        
Guolin Ke's avatar
Guolin Ke committed
218
      } else {
219
220
221
222
223
        
        # Check if objective is function
        if (!is.function(fobj)) {
          stop("lgb.Booster.update: fobj should be a function")
        }
224
        if (!private$set_objective_to_none) {
225
          self$reset_parameter(params = list(objective = "none"))
226
227
          private$set_objective_to_none = TRUE
        }
228
        # Perform objective calculation
Guolin Ke's avatar
Guolin Ke committed
229
        gpair <- fobj(private$inner_predict(1), private$train_set)
230
231
        
        # Check for gradient and hessian as list
232
        if(is.null(gpair$grad) || is.null(gpair$hess)){
233
234
235
          stop("lgb.Booster.update: custom objective should 
            return a list with attributes (hess, grad)")
        }
236
237
238
239
240
241
242
243
244
        
        # Return custom boosting gradient/hessian
        ret <- lgb.call("LGBM_BoosterUpdateOneIterCustom_R",
                        ret = NULL,
                        private$handle,
                        gpair$grad,
                        gpair$hess,
                        length(gpair$grad))
        
Guolin Ke's avatar
Guolin Ke committed
245
      }
246
247
      
      # Loop through each iteration
248
      for (i in seq_along(private$is_predicted_cur_iter)) {
Guolin Ke's avatar
Guolin Ke committed
249
250
        private$is_predicted_cur_iter[[i]] <- FALSE
      }
251
252
253
      
      return(ret)
      
Guolin Ke's avatar
Guolin Ke committed
254
    },
255
256
    
    # Return one iteration behind
Guolin Ke's avatar
Guolin Ke committed
257
    rollback_one_iter = function() {
258
259
260
261
262
263
264
      
      # Return one iteration behind
      lgb.call("LGBM_BoosterRollbackOneIter_R",
               ret = NULL,
               private$handle)
      
      # Loop through each iteration
265
      for (i in seq_along(private$is_predicted_cur_iter)) {
Guolin Ke's avatar
Guolin Ke committed
266
267
        private$is_predicted_cur_iter[[i]] <- FALSE
      }
268
269
      
      # Return self
270
      return(invisible(self))
271
      
Guolin Ke's avatar
Guolin Ke committed
272
    },
273
274
    
    # Get current iteration
Guolin Ke's avatar
Guolin Ke committed
275
    current_iter = function() {
276
      
277
      cur_iter <- 0L
278
279
280
281
      lgb.call("LGBM_BoosterGetCurrentIteration_R",
               ret = cur_iter,
               private$handle)
      
Guolin Ke's avatar
Guolin Ke committed
282
    },
283
284
    
    # Evaluate data on metrics
Guolin Ke's avatar
Guolin Ke committed
285
    eval = function(data, name, feval = NULL) {
286
287
      
      # Check if dataset is lgb.Dataset
Guolin Ke's avatar
Guolin Ke committed
288
      if (!lgb.check.r6.class(data, "lgb.Dataset")) {
289
        stop("lgb.Booster.eval: Can only use lgb.Dataset to eval")
Guolin Ke's avatar
Guolin Ke committed
290
      }
291
292
      
      # Check for identical data
Guolin Ke's avatar
Guolin Ke committed
293
      data_idx <- 0
294
295
296
297
298
      if (identical(data, private$train_set)) {
        data_idx <- 1
      } else {
        
        # Check for validation data
299
        if (length(private$valid_sets) > 0) {
300
301
          
          # Loop through each validation set
302
          for (i in seq_along(private$valid_sets)) {
303
304
            
            # Check for identical validation data with training data
Guolin Ke's avatar
Guolin Ke committed
305
            if (identical(data, private$valid_sets[[i]])) {
306
307
              
              # Found identical data, skip
Guolin Ke's avatar
Guolin Ke committed
308
309
              data_idx <- i + 1
              break
310
              
Guolin Ke's avatar
Guolin Ke committed
311
            }
312
            
Guolin Ke's avatar
Guolin Ke committed
313
          }
314
          
Guolin Ke's avatar
Guolin Ke committed
315
        }
316
        
Guolin Ke's avatar
Guolin Ke committed
317
      }
318
319
      
      # Check if evaluation was not done
Guolin Ke's avatar
Guolin Ke committed
320
      if (data_idx == 0) {
321
322
        
        # Add validation data by name
Guolin Ke's avatar
Guolin Ke committed
323
324
        self$add_valid(data, name)
        data_idx <- private$num_dataset
325
        
Guolin Ke's avatar
Guolin Ke committed
326
      }
327
328
      
      # Evaluate data
329
      private$inner_eval(name, data_idx, feval)
330
      
Guolin Ke's avatar
Guolin Ke committed
331
    },
332
333
    
    # Evaluation training data
Guolin Ke's avatar
Guolin Ke committed
334
    eval_train = function(feval = NULL) {
335
      private$inner_eval(private$name_train_set, 1, feval)
Guolin Ke's avatar
Guolin Ke committed
336
    },
337
338
    
    # Evaluation validation data
Guolin Ke's avatar
Guolin Ke committed
339
    eval_valid = function(feval = NULL) {
340
341
      
      # Create ret list
Guolin Ke's avatar
Guolin Ke committed
342
      ret = list()
343
344
345
346
347
348
349
      
      # Check if validation is empty
      if (length(private$valid_sets) <= 0) {
        return(ret)
      }
      
      # Loop through each validation set
350
351
      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
352
      }
353
354
355
356
      
      # Return ret
      return(ret)
      
Guolin Ke's avatar
Guolin Ke committed
357
    },
358
359
    
    # Save model
Guolin Ke's avatar
Guolin Ke committed
360
    save_model = function(filename, num_iteration = NULL) {
361
362
363
364
365
366
367
368
369
370
371
372
373
374
      
      # Check if number of iteration is non existent
      if (is.null(num_iteration)) {
        num_iteration <- self$best_iter
      }
      
      # Save booster model
      lgb.call("LGBM_BoosterSaveModel_R",
               ret = NULL,
               private$handle,
               as.integer(num_iteration),
               lgb.c_str(filename))
      
      # Return self
375
      return(invisible(self))
Guolin Ke's avatar
Guolin Ke committed
376
    },
377
    
378
379
380
381
382
383
384
385
386
387
    # Save model to string
    save_model_to_string = function(num_iteration = NULL) {
      
      # Check if number of iteration is non existent
      if (is.null(num_iteration)) {
        num_iteration <- self$best_iter
      }
      
      # Return model string
      return(lgb.call.return.str("LGBM_BoosterSaveModelToString_R",
388
389
                                 private$handle,
                                 as.integer(num_iteration)))
390
391
392
      
    },
    
393
    # Dump model in memory
Guolin Ke's avatar
Guolin Ke committed
394
    dump_model = function(num_iteration = NULL) {
395
396
397
398
399
400
401
402
403
404
405
      
      # Check if number of iteration is non existent
      if (is.null(num_iteration)) {
        num_iteration <- self$best_iter
      }
      
      # Return dumped model
      lgb.call.return.str("LGBM_BoosterDumpModel_R",
                          private$handle,
                          as.integer(num_iteration))
      
Guolin Ke's avatar
Guolin Ke committed
406
    },
407
408
    
    # Predict on new data
Guolin Ke's avatar
Guolin Ke committed
409
    predict = function(data,
410
411
412
                       num_iteration = NULL,
                       rawscore = FALSE,
                       predleaf = FALSE,
413
                       predcontrib = FALSE,
414
                       header = FALSE,
415
                       reshape = FALSE, ...) {
416
417
418
419
420
421
422
      
      # Check if number of iteration is  non existent
      if (is.null(num_iteration)) {
        num_iteration <- self$best_iter
      }
      
      # Predict on new data
423
      predictor <- Predictor$new(private$handle, ...)
424
      predictor$predict(data, num_iteration, rawscore, predleaf, predcontrib, header, reshape)
425
426
427
428
429
430
      
    },
    
    # Transform into predictor
    to_predictor = function() {
      Predictor$new(private$handle)
Guolin Ke's avatar
Guolin Ke committed
431
    },
432
433
    
    # Used for save
434
    raw = NA,
435
436
    
    # Save model to temporary file for in-memory saving
437
    save = function() {
438
439
      
      # Overwrite model in object
440
      self$raw <- self$save_model_to_string(NULL)
441
      
442
    }
443
    
Guolin Ke's avatar
Guolin Ke committed
444
445
  ),
  private = list(
446
447
448
449
450
451
452
453
454
455
456
    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
457
    higher_better_inner_eval = NULL,
458
    set_objective_to_none = FALSE,
459
460
461
462
    # Predict data
    inner_predict = function(idx) {
      
      # Store data name
Guolin Ke's avatar
Guolin Ke committed
463
      data_name <- private$name_train_set
464
465
466
467
468
469
470
      
      # Check for id bigger than 1
      if (idx > 1) {
        data_name <- private$name_valid_sets[[idx - 1]]
      }
      
      # Check for unknown dataset (over the maximum provided range)
Guolin Ke's avatar
Guolin Ke committed
471
472
473
      if (idx > private$num_dataset) {
        stop("data_idx should not be greater than num_dataset")
      }
474
475
      
      # Check for prediction buffer
Guolin Ke's avatar
Guolin Ke committed
476
      if (is.null(private$predict_buffer[[data_name]])) {
477
478
        
        # Store predictions
479
        npred <- 0L
480
        npred <- lgb.call("LGBM_BoosterGetNumPredict_R",
481
482
483
484
485
                          ret = npred,
                          private$handle,
                          as.integer(idx - 1))
        private$predict_buffer[[data_name]] <- numeric(npred)
        
Guolin Ke's avatar
Guolin Ke committed
486
      }
487
488
      
      # Check if current iteration was already predicted
Guolin Ke's avatar
Guolin Ke committed
489
      if (!private$is_predicted_cur_iter[[idx]]) {
490
491
492
493
494
495
        
        # Use buffer
        private$predict_buffer[[data_name]] <- lgb.call("LGBM_BoosterGetPredict_R",
                                                        ret = private$predict_buffer[[data_name]],
                                                        private$handle,
                                                        as.integer(idx - 1))
Guolin Ke's avatar
Guolin Ke committed
496
497
        private$is_predicted_cur_iter[[idx]] <- TRUE
      }
498
499
500
      
      # Return prediction buffer
      return(private$predict_buffer[[data_name]])
Guolin Ke's avatar
Guolin Ke committed
501
    },
502
503
    
    # Get evaluation information
Guolin Ke's avatar
Guolin Ke committed
504
    get_eval_info = function() {
505
506
      
      # Check for evaluation names emptiness
Guolin Ke's avatar
Guolin Ke committed
507
      if (is.null(private$eval_names)) {
508
509
510
511
512
513
        
        # Get evaluation names
        names <- lgb.call.return.str("LGBM_BoosterGetEvalNames_R",
                                     private$handle)
        
        # Check names' length
514
        if (nchar(names) > 0) {
515
516
          
          # Parse and store privately names
Guolin Ke's avatar
Guolin Ke committed
517
518
          names <- strsplit(names, "\t")[[1]]
          private$eval_names <- names
519
          private$higher_better_inner_eval <- grepl("^ndcg|^auc$", names)
520
          
Guolin Ke's avatar
Guolin Ke committed
521
        }
522
        
Guolin Ke's avatar
Guolin Ke committed
523
      }
524
525
526
527
      
      # Return evaluation names
      return(private$eval_names)
      
Guolin Ke's avatar
Guolin Ke committed
528
    },
529
530
    
    # Perform inner evaluation
Guolin Ke's avatar
Guolin Ke committed
531
    inner_eval = function(data_name, data_idx, feval = NULL) {
532
533
      
      # Check for unknown dataset (over the maximum provided range)
Guolin Ke's avatar
Guolin Ke committed
534
535
536
      if (data_idx > private$num_dataset) {
        stop("data_idx should not be greater than num_dataset")
      }
537
538
      
      # Get evaluation information
Guolin Ke's avatar
Guolin Ke committed
539
      private$get_eval_info()
540
541
      
      # Prepare return
Guolin Ke's avatar
Guolin Ke committed
542
      ret <- list()
543
544
      
      # Check evaluation names existence
Guolin Ke's avatar
Guolin Ke committed
545
      if (length(private$eval_names) > 0) {
546
547
548
549
550
551
552
553
554
        
        # Create evaluation values
        tmp_vals <- numeric(length(private$eval_names))
        tmp_vals <- lgb.call("LGBM_BoosterGetEval_R",
                             ret = tmp_vals,
                             private$handle,
                             as.integer(data_idx - 1))
        
        # Loop through all evaluation names
555
        for (i in seq_along(private$eval_names)) {
556
557
558
559
560
561
          
          # Store evaluation and append to return
          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
562
          res$higher_better <- private$higher_better_inner_eval[i]
563
564
          ret <- append(ret, list(res))
          
Guolin Ke's avatar
Guolin Ke committed
565
        }
566
        
Guolin Ke's avatar
Guolin Ke committed
567
      }
568
569
      
      # Check if there are evaluation metrics
Guolin Ke's avatar
Guolin Ke committed
570
      if (!is.null(feval)) {
571
572
        
        # Check if evaluation metric is a function
573
        if (!is.function(feval)) {
Guolin Ke's avatar
Guolin Ke committed
574
575
          stop("lgb.Booster.eval: feval should be a function")
        }
576
577
        
        # Prepare data
Guolin Ke's avatar
Guolin Ke committed
578
        data <- private$train_set
579
580
581
582
583
584
585
        
        # Check if data to assess is existing differently
        if (data_idx > 1) {
          data <- private$valid_sets[[data_idx - 1]]
        }
        
        # Perform function evaluation
586
        res <- feval(private$inner_predict(data_idx), data)
587
588
        
        # Check for name correctness
589
        if(is.null(res$name) || is.null(res$value) ||  is.null(res$higher_better)) {
590
591
592
          stop("lgb.Booster.eval: custom eval function should return a 
            list with attribute (name, value, higher_better)");
        }
593
594
        
        # Append names and evaluation
Guolin Ke's avatar
Guolin Ke committed
595
        res$data_name <- data_name
596
        ret <- append(ret, list(res))
Guolin Ke's avatar
Guolin Ke committed
597
      }
598
599
600
601
      
      # Return ret
      return(ret)
      
Guolin Ke's avatar
Guolin Ke committed
602
    }
603
    
Guolin Ke's avatar
Guolin Ke committed
604
605
606
607
608
  )
)


#' Predict method for LightGBM model
609
#'
Guolin Ke's avatar
Guolin Ke committed
610
#' Predicted values based on class \code{lgb.Booster}
611
#'
Guolin Ke's avatar
Guolin Ke committed
612
613
614
#' @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
615
616
#' @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
617
#'        logistic regression would result in predictions for log-odds instead of probabilities.
618
#' @param predleaf whether predict leaf index instead.
619
#' @param predcontrib return per-feature contributions for each record.
Guolin Ke's avatar
Guolin Ke committed
620
#' @param header only used for prediction for text file. True if text file has header
621
622
#' @param reshape whether to reshape the vector of predictions to a matrix form when there are several
#'        prediction outputs per case.
James Lamb's avatar
James Lamb committed
623
624
#' @param ... Additional named arguments passed to the \code{predict()} method of
#'            the \code{lgb.Booster} object passed to \code{object}.
625
#' @return
Guolin Ke's avatar
Guolin Ke committed
626
#' For regression or binary classification, it returns a vector of length \code{nrows(data)}.
627
628
#' 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
629
#' the \code{reshape} value.
630
631
#'
#' When \code{predleaf = TRUE}, the output is a matrix object with the
Guolin Ke's avatar
Guolin Ke committed
632
#' number of columns corresponding to the number of trees.
633
#' 
Guolin Ke's avatar
Guolin Ke committed
634
#' @examples
635
#' \dontrun{
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
#' 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)
653
#' }
654
#' 
Guolin Ke's avatar
Guolin Ke committed
655
656
#' @rdname predict.lgb.Booster
#' @export
657
predict.lgb.Booster <- function(object, data,
Guolin Ke's avatar
Guolin Ke committed
658
                        num_iteration = NULL,
659
660
                        rawscore = FALSE,
                        predleaf = FALSE,
661
                        predcontrib = FALSE,
662
                        header = FALSE,
663
                        reshape = FALSE, ...) {
664
665
  
  # Check booster existence
666
667
  if (!lgb.is.Booster(object)) {
    stop("predict.lgb.Booster: object should be an ", sQuote("lgb.Booster"))
Guolin Ke's avatar
Guolin Ke committed
668
  }
669
670
671
672
673
674
  
  # Return booster predictions
  object$predict(data,
                 num_iteration,
                 rawscore,
                 predleaf,
675
                 predcontrib,
676
                 header,
677
                 reshape, ...)
Guolin Ke's avatar
Guolin Ke committed
678
679
680
}

#' Load LightGBM model
681
#'
682
683
684
#' Load LightGBM model from saved model file or string
#' Load LightGBM takes in either a file path or model string
#' If both are provided, Load will default to loading from file
685
#'
Guolin Ke's avatar
Guolin Ke committed
686
#' @param filename path of model file
687
#' @param model_str a str containing the model
688
#'
689
#' @return lgb.Booster
690
#' 
Guolin Ke's avatar
Guolin Ke committed
691
#' @examples
692
#' \dontrun{
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
#' 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")
710
711
712
#' load_booster <- lgb.load(filename = "model.txt")
#' model_string <- model$save_model_to_string(NULL) # saves best iteration
#' load_booster_from_str <- lgb.load(model_str = model_string)
713
#' }
714
#' 
715
#' @rdname lgb.load
Guolin Ke's avatar
Guolin Ke committed
716
#' @export
717
lgb.load <- function(filename = NULL, model_str = NULL){
718
  
719
720
721
722
723
724
  if (is.null(filename) && is.null(model_str)) {
    stop("lgb.load: either filename or model_str must be given")
  }
  
  # Load from filename
  if (!is.null(filename) && !is.character(filename)) {
725
726
727
728
    stop("lgb.load: filename should be character")
  }
  
  # Return new booster
729
  if (!is.null(filename) && !file.exists(filename)) stop("lgb.load: file does not exist for supplied filename")
730
  if (!is.null(filename)) return(invisible(Booster$new(modelfile = filename)))
731
732
733
734
735
736
  
  # Load from model_str
  if (!is.null(model_str) && !is.character(model_str)) {
    stop("lgb.load: model_str should be character")
  }    
  # Return new booster
737
  if (!is.null(model_str)) return(invisible(Booster$new(model_str = model_str)))
738
  
Guolin Ke's avatar
Guolin Ke committed
739
740
741
}

#' Save LightGBM model
742
#'
Guolin Ke's avatar
Guolin Ke committed
743
#' Save LightGBM model
744
#'
Guolin Ke's avatar
Guolin Ke committed
745
746
747
#' @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
748
#'
749
#' @return lgb.Booster
750
#' 
Guolin Ke's avatar
Guolin Ke committed
751
#' @examples
752
#' \dontrun{
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
#' 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")
770
#' }
771
#' 
772
#' @rdname lgb.save
Guolin Ke's avatar
Guolin Ke committed
773
#' @export
774
lgb.save <- function(booster, filename, num_iteration = NULL){
775
776
777
778
779
780
781
782
783
784
785
786
  
  # Check if booster is booster
  if (!lgb.is.Booster(booster)) {
    stop("lgb.save: booster should be an ", sQuote("lgb.Booster"))
  }
  
  # Check if file name is character
  if (!is.character(filename)) {
    stop("lgb.save: filename should be a character")
  }
  
  # Store booster
787
  invisible(booster$save_model(filename, num_iteration))
788
  
Guolin Ke's avatar
Guolin Ke committed
789
790
791
}

#' Dump LightGBM model to json
792
#'
Guolin Ke's avatar
Guolin Ke committed
793
#' Dump LightGBM model to json
794
#'
Guolin Ke's avatar
Guolin Ke committed
795
796
#' @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
797
#'
Guolin Ke's avatar
Guolin Ke committed
798
#' @return json format of model
799
#' 
Guolin Ke's avatar
Guolin Ke committed
800
#' @examples
801
#' \dontrun{
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
#' 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)
819
#' }
820
#' 
821
#' @rdname lgb.dump
Guolin Ke's avatar
Guolin Ke committed
822
#' @export
823
lgb.dump <- function(booster, num_iteration = NULL){
824
825
826
827
828
829
830
  
  # Check if booster is booster
  if (!lgb.is.Booster(booster)) {
    stop("lgb.save: booster should be an ", sQuote("lgb.Booster"))
  }
  
  # Return booster at requested iteration
Guolin Ke's avatar
Guolin Ke committed
831
  booster$dump_model(num_iteration)
832
  
Guolin Ke's avatar
Guolin Ke committed
833
834
835
}

#' Get record evaluation result from booster
836
#'
Guolin Ke's avatar
Guolin Ke committed
837
838
839
840
841
842
#' 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
843
#' 
Guolin Ke's avatar
Guolin Ke committed
844
#' @return vector of evaluation result
845
#' 
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
#' @examples
#' \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.get.eval.result(model, "test", "l2")
#' }
#' 
Guolin Ke's avatar
Guolin Ke committed
867
868
#' @rdname lgb.get.eval.result
#' @export
869
lgb.get.eval.result <- function(booster, data_name, eval_name, iters = NULL, is_err = FALSE) {
870
871
  
  # Check if booster is booster
872
873
  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
874
  }
875
876
  
  # Check if data and evaluation name are characters or not
877
878
  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
879
  }
880
881
  
  # Check if recorded evaluation is existing
882
  if (is.null(booster$record_evals[[data_name]])) {
Guolin Ke's avatar
Guolin Ke committed
883
884
    stop("lgb.get.eval.result: wrong data name")
  }
885
886
  
  # Check if evaluation result is existing
887
  if (is.null(booster$record_evals[[data_name]][[eval_name]])) {
Guolin Ke's avatar
Guolin Ke committed
888
889
    stop("lgb.get.eval.result: wrong eval name")
  }
890
891
  
  # Create result
Guolin Ke's avatar
Guolin Ke committed
892
  result <- booster$record_evals[[data_name]][[eval_name]]$eval
893
894
  
  # Check if error is requested
895
  if (is_err) {
Guolin Ke's avatar
Guolin Ke committed
896
897
    result <- booster$record_evals[[data_name]][[eval_name]]$eval_err
  }
898
899
  
  # Check if iteration is non existant
900
  if (is.null(iters)) {
Guolin Ke's avatar
Guolin Ke committed
901
902
    return(as.numeric(result))
  }
903
904
  
  # Parse iteration and booster delta
Guolin Ke's avatar
Guolin Ke committed
905
906
907
  iters <- as.integer(iters)
  delta <- booster$record_evals$start_iter - 1
  iters <- iters - delta
908
909
  
  # Return requested result
910
  as.numeric(result[iters])
Guolin Ke's avatar
Guolin Ke committed
911
}