lgb.Dataset.R 29.8 KB
Newer Older
James Lamb's avatar
James Lamb committed
1
2

#' @importFrom methods is
James Lamb's avatar
James Lamb committed
3
4
5
#' @importFrom R6 R6Class
Dataset <- R6::R6Class(

6
  classname = "lgb.Dataset",
7
  cloneable = FALSE,
Guolin Ke's avatar
Guolin Ke committed
8
  public = list(
James Lamb's avatar
James Lamb committed
9

10
    # Finalize will free up the handles
Guolin Ke's avatar
Guolin Ke committed
11
    finalize = function() {
James Lamb's avatar
James Lamb committed
12

13
      # Check the need for freeing handle
Guolin Ke's avatar
Guolin Ke committed
14
      if (!lgb.is.null.handle(private$handle)) {
James Lamb's avatar
James Lamb committed
15

16
        # Freeing up handle
Guolin Ke's avatar
Guolin Ke committed
17
18
        lgb.call("LGBM_DatasetFree_R", ret = NULL, private$handle)
        private$handle <- NULL
James Lamb's avatar
James Lamb committed
19

Guolin Ke's avatar
Guolin Ke committed
20
      }
James Lamb's avatar
James Lamb committed
21

Guolin Ke's avatar
Guolin Ke committed
22
    },
James Lamb's avatar
James Lamb committed
23

24
    # Initialize will create a starter dataset
Guolin Ke's avatar
Guolin Ke committed
25
    initialize = function(data,
26
27
28
                          params = list(),
                          reference = NULL,
                          colnames = NULL,
29
                          categorical_feature = NULL,
30
31
32
33
                          predictor = NULL,
                          free_raw_data = TRUE,
                          used_indices = NULL,
                          info = list(),
Guolin Ke's avatar
Guolin Ke committed
34
                          ...) {
James Lamb's avatar
James Lamb committed
35

36
      # Check for additional parameters
37
      additional_params <- list(...)
James Lamb's avatar
James Lamb committed
38

39
40
      # Create known attributes list
      INFO_KEYS <- c("label", "weight", "init_score", "group")
James Lamb's avatar
James Lamb committed
41

42
      # Check if attribute key is in the known attribute list
43
      for (key in names(additional_params)) {
James Lamb's avatar
James Lamb committed
44

45
        # Key existing
46
        if (key %in% INFO_KEYS) {
James Lamb's avatar
James Lamb committed
47

48
          # Store as info
49
          info[[key]] <- additional_params[[key]]
James Lamb's avatar
James Lamb committed
50

Guolin Ke's avatar
Guolin Ke committed
51
        } else {
James Lamb's avatar
James Lamb committed
52

53
          # Store as param
54
          params[[key]] <- additional_params[[key]]
James Lamb's avatar
James Lamb committed
55

Guolin Ke's avatar
Guolin Ke committed
56
        }
James Lamb's avatar
James Lamb committed
57

Guolin Ke's avatar
Guolin Ke committed
58
      }
James Lamb's avatar
James Lamb committed
59

60
      # Check for dataset reference
Guolin Ke's avatar
Guolin Ke committed
61
62
      if (!is.null(reference)) {
        if (!lgb.check.r6.class(reference, "lgb.Dataset")) {
63
          stop("lgb.Dataset: Can only use ", sQuote("lgb.Dataset"), " as reference")
Guolin Ke's avatar
Guolin Ke committed
64
65
        }
      }
James Lamb's avatar
James Lamb committed
66

67
      # Check for predictor reference
Guolin Ke's avatar
Guolin Ke committed
68
69
      if (!is.null(predictor)) {
        if (!lgb.check.r6.class(predictor, "lgb.Predictor")) {
70
          stop("lgb.Dataset: Only can use ", sQuote("lgb.Predictor"), " as predictor")
Guolin Ke's avatar
Guolin Ke committed
71
72
        }
      }
James Lamb's avatar
James Lamb committed
73

74
75
76
77
78
79
80
      # Check for matrix format
      if (is.matrix(data)) {
        # Check whether matrix is the correct type first ("double")
        if (storage.mode(data) != "double") {
          storage.mode(data) <- "double"
        }
      }
James Lamb's avatar
James Lamb committed
81

82
83
84
      # Setup private attributes
      private$raw_data <- data
      private$params <- params
Guolin Ke's avatar
Guolin Ke committed
85
      private$reference <- reference
86
      private$colnames <- colnames
87

88
      private$categorical_feature <- categorical_feature
89
90
91
92
      private$predictor <- predictor
      private$free_raw_data <- free_raw_data
      private$used_indices <- used_indices
      private$info <- info
James Lamb's avatar
James Lamb committed
93

Guolin Ke's avatar
Guolin Ke committed
94
    },
James Lamb's avatar
James Lamb committed
95

96
97
98
    create_valid = function(data,
                            info = list(),
                            ...) {
James Lamb's avatar
James Lamb committed
99

100
101
102
103
104
105
106
107
108
109
110
      # Create new dataset
      ret <- Dataset$new(data,
                         private$params,
                         self,
                         private$colnames,
                         private$categorical_feature,
                         private$predictor,
                         private$free_raw_data,
                         NULL,
                         info,
                         ...)
James Lamb's avatar
James Lamb committed
111

112
      # Return ret
113
      return(invisible(ret))
James Lamb's avatar
James Lamb committed
114

Guolin Ke's avatar
Guolin Ke committed
115
    },
James Lamb's avatar
James Lamb committed
116

117
    # Dataset constructor
Guolin Ke's avatar
Guolin Ke committed
118
    construct = function() {
James Lamb's avatar
James Lamb committed
119

120
      # Check for handle null
Guolin Ke's avatar
Guolin Ke committed
121
      if (!lgb.is.null.handle(private$handle)) {
122
        return(invisible(self))
Guolin Ke's avatar
Guolin Ke committed
123
      }
James Lamb's avatar
James Lamb committed
124

Guolin Ke's avatar
Guolin Ke committed
125
126
      # Get feature names
      cnames <- NULL
James Lamb's avatar
James Lamb committed
127
      if (is.matrix(private$raw_data) || methods::is(private$raw_data, "dgCMatrix")) {
Guolin Ke's avatar
Guolin Ke committed
128
129
        cnames <- colnames(private$raw_data)
      }
James Lamb's avatar
James Lamb committed
130

Guolin Ke's avatar
Guolin Ke committed
131
      # set feature names if not exist
132
      if (is.null(private$colnames) && !is.null(cnames)) {
Guolin Ke's avatar
Guolin Ke committed
133
134
        private$colnames <- as.character(cnames)
      }
James Lamb's avatar
James Lamb committed
135

136
137
      # Get categorical feature index
      if (!is.null(private$categorical_feature)) {
James Lamb's avatar
James Lamb committed
138

139
        # Check for character name
140
        if (is.character(private$categorical_feature)) {
James Lamb's avatar
James Lamb committed
141

142
            cate_indices <- as.list(match(private$categorical_feature, private$colnames) - 1)
James Lamb's avatar
James Lamb committed
143

144
            # Provided indices, but some indices are not existing?
145
146
147
            if (sum(is.na(cate_indices)) > 0) {
              stop("lgb.self.get.handle: supplied an unknown feature in categorical_feature: ", sQuote(private$categorical_feature[is.na(cate_indices)]))
            }
James Lamb's avatar
James Lamb committed
148

149
          } else {
James Lamb's avatar
James Lamb committed
150

151
            # Check if more categorical features were output over the feature space
152
153
154
            if (max(private$categorical_feature) > length(private$colnames)) {
              stop("lgb.self.get.handle: supplied a too large value in categorical_feature: ", max(private$categorical_feature), " but only ", length(private$colnames), " features")
            }
James Lamb's avatar
James Lamb committed
155

156
            # Store indices as [0, n-1] indexed instead of [1, n] indexed
157
            cate_indices <- as.list(private$categorical_feature - 1)
James Lamb's avatar
James Lamb committed
158

159
          }
James Lamb's avatar
James Lamb committed
160

161
        # Store indices for categorical features
162
        private$params$categorical_feature <- cate_indices
James Lamb's avatar
James Lamb committed
163

164
      }
James Lamb's avatar
James Lamb committed
165

Guolin Ke's avatar
Guolin Ke committed
166
167
      # Check has header or not
      has_header <- FALSE
168
169
      if (!is.null(private$params$has_header) || !is.null(private$params$header)) {
        if (tolower(as.character(private$params$has_header)) == "true" || tolower(as.character(private$params$header)) == "true") {
Guolin Ke's avatar
Guolin Ke committed
170
171
172
          has_header <- TRUE
        }
      }
James Lamb's avatar
James Lamb committed
173

Guolin Ke's avatar
Guolin Ke committed
174
175
      # Generate parameter str
      params_str <- lgb.params2str(private$params)
James Lamb's avatar
James Lamb committed
176

177
      # Get handle of reference dataset
Guolin Ke's avatar
Guolin Ke committed
178
179
180
181
      ref_handle <- NULL
      if (!is.null(private$reference)) {
        ref_handle <- private$reference$.__enclos_env__$private$get_handle()
      }
182
      handle <- NA_real_
James Lamb's avatar
James Lamb committed
183

184
      # Not subsetting
Guolin Ke's avatar
Guolin Ke committed
185
      if (is.null(private$used_indices)) {
James Lamb's avatar
James Lamb committed
186

187
        # Are we using a data file?
188
        if (is.character(private$raw_data)) {
James Lamb's avatar
James Lamb committed
189

190
191
192
193
194
          handle <- lgb.call("LGBM_DatasetCreateFromFile_R",
                             ret = handle,
                             lgb.c_str(private$raw_data),
                             params_str,
                             ref_handle)
James Lamb's avatar
James Lamb committed
195

Guolin Ke's avatar
Guolin Ke committed
196
        } else if (is.matrix(private$raw_data)) {
James Lamb's avatar
James Lamb committed
197

198
199
200
201
202
203
204
205
          # Are we using a matrix?
          handle <- lgb.call("LGBM_DatasetCreateFromMat_R",
                             ret = handle,
                             private$raw_data,
                             nrow(private$raw_data),
                             ncol(private$raw_data),
                             params_str,
                             ref_handle)
James Lamb's avatar
James Lamb committed
206
207

        } else if (methods::is(private$raw_data, "dgCMatrix")) {
208
209
210
          if (length(private$raw_data@p) > 2147483647) {
            stop("Cannot support large CSC matrix")
          }
211
212
213
214
215
216
217
218
219
220
221
          # Are we using a dgCMatrix (sparsed matrix column compressed)
          handle <- lgb.call("LGBM_DatasetCreateFromCSC_R",
                             ret = handle,
                             private$raw_data@p,
                             private$raw_data@i,
                             private$raw_data@x,
                             length(private$raw_data@p),
                             length(private$raw_data@x),
                             nrow(private$raw_data),
                             params_str,
                             ref_handle)
James Lamb's avatar
James Lamb committed
222

Guolin Ke's avatar
Guolin Ke committed
223
        } else {
James Lamb's avatar
James Lamb committed
224

225
226
          # Unknown data type
          stop("lgb.Dataset.construct: does not support constructing from ", sQuote(class(private$raw_data)))
James Lamb's avatar
James Lamb committed
227

Guolin Ke's avatar
Guolin Ke committed
228
        }
James Lamb's avatar
James Lamb committed
229

Guolin Ke's avatar
Guolin Ke committed
230
      } else {
James Lamb's avatar
James Lamb committed
231

232
        # Reference is empty
Guolin Ke's avatar
Guolin Ke committed
233
        if (is.null(private$reference)) {
234
          stop("lgb.Dataset.construct: reference cannot be NULL for constructing data subset")
Guolin Ke's avatar
Guolin Ke committed
235
        }
James Lamb's avatar
James Lamb committed
236

237
238
239
240
        # Construct subset
        handle <- lgb.call("LGBM_DatasetGetSubset_R",
                           ret = handle,
                           ref_handle,
241
                           c(private$used_indices), # Adding c() fixes issue in R v3.5
242
243
                           length(private$used_indices),
                           params_str)
James Lamb's avatar
James Lamb committed
244

Guolin Ke's avatar
Guolin Ke committed
245
      }
Guolin Ke's avatar
Guolin Ke committed
246
247
248
      if (lgb.is.null.handle(handle)) {
        stop("lgb.Dataset.construct: cannot create Dataset handle")
      }
249
      # Setup class and private type
Guolin Ke's avatar
Guolin Ke committed
250
251
      class(handle) <- "lgb.Dataset.handle"
      private$handle <- handle
James Lamb's avatar
James Lamb committed
252

253
254
255
256
      # Set feature names
      if (!is.null(private$colnames)) {
        self$set_colnames(private$colnames)
      }
257

258
259
      # Load init score if requested
      if (!is.null(private$predictor) && is.null(private$used_indices)) {
James Lamb's avatar
James Lamb committed
260

261
        # Setup initial scores
262
        init_score <- private$predictor$predict(private$raw_data, rawscore = TRUE, reshape = TRUE)
James Lamb's avatar
James Lamb committed
263

264
        # Not needed to transpose, for is col_marjor
Guolin Ke's avatar
Guolin Ke committed
265
266
        init_score <- as.vector(init_score)
        private$info$init_score <- init_score
James Lamb's avatar
James Lamb committed
267

268
      }
James Lamb's avatar
James Lamb committed
269

270
271
272
      # Should we free raw data?
      if (isTRUE(private$free_raw_data)) {
        private$raw_data <- NULL
Guolin Ke's avatar
Guolin Ke committed
273
      }
James Lamb's avatar
James Lamb committed
274

275
      # Get private information
Guolin Ke's avatar
Guolin Ke committed
276
      if (length(private$info) > 0) {
James Lamb's avatar
James Lamb committed
277

278
        # Set infos
279
        for (i in seq_along(private$info)) {
James Lamb's avatar
James Lamb committed
280

Guolin Ke's avatar
Guolin Ke committed
281
282
          p <- private$info[i]
          self$setinfo(names(p), p[[1]])
James Lamb's avatar
James Lamb committed
283

Guolin Ke's avatar
Guolin Ke committed
284
        }
James Lamb's avatar
James Lamb committed
285

Guolin Ke's avatar
Guolin Ke committed
286
      }
James Lamb's avatar
James Lamb committed
287

288
      # Get label information existence
Guolin Ke's avatar
Guolin Ke committed
289
290
291
      if (is.null(self$getinfo("label"))) {
        stop("lgb.Dataset.construct: label should be set")
      }
James Lamb's avatar
James Lamb committed
292

293
294
      # Return self
      return(invisible(self))
James Lamb's avatar
James Lamb committed
295

Guolin Ke's avatar
Guolin Ke committed
296
    },
James Lamb's avatar
James Lamb committed
297

298
    # Dimension function
Guolin Ke's avatar
Guolin Ke committed
299
    dim = function() {
James Lamb's avatar
James Lamb committed
300

301
      # Check for handle
Guolin Ke's avatar
Guolin Ke committed
302
      if (!lgb.is.null.handle(private$handle)) {
James Lamb's avatar
James Lamb committed
303

304
305
        num_row <- 0L
        num_col <- 0L
James Lamb's avatar
James Lamb committed
306

307
308
309
        # Get numeric data and numeric features
        c(lgb.call("LGBM_DatasetGetNumData_R", ret = num_row, private$handle),
          lgb.call("LGBM_DatasetGetNumFeature_R", ret = num_col, private$handle))
James Lamb's avatar
James Lamb committed
310
311
312

      } else if (is.matrix(private$raw_data) || methods::is(private$raw_data, "dgCMatrix")) {

313
        # Check if dgCMatrix (sparse matrix column compressed)
314
        dim(private$raw_data)
James Lamb's avatar
James Lamb committed
315

Guolin Ke's avatar
Guolin Ke committed
316
      } else {
James Lamb's avatar
James Lamb committed
317

318
319
        # Trying to work with unknown dimensions is not possible
        stop("dim: cannot get dimensions before dataset has been constructed, please call lgb.Dataset.construct explicitly")
James Lamb's avatar
James Lamb committed
320

Guolin Ke's avatar
Guolin Ke committed
321
      }
James Lamb's avatar
James Lamb committed
322

Guolin Ke's avatar
Guolin Ke committed
323
    },
James Lamb's avatar
James Lamb committed
324

325
    # Get column names
Guolin Ke's avatar
Guolin Ke committed
326
    get_colnames = function() {
James Lamb's avatar
James Lamb committed
327

328
      # Check for handle
Guolin Ke's avatar
Guolin Ke committed
329
      if (!lgb.is.null.handle(private$handle)) {
James Lamb's avatar
James Lamb committed
330

331
        # Get feature names and write them
332
333
334
        cnames <- lgb.call.return.str("LGBM_DatasetGetFeatureNames_R", private$handle)
        private$colnames <- as.character(base::strsplit(cnames, "\t")[[1]])
        private$colnames
James Lamb's avatar
James Lamb committed
335
336
337

      } else if (is.matrix(private$raw_data) || methods::is(private$raw_data, "dgCMatrix")) {

338
        # Check if dgCMatrix (sparse matrix column compressed)
339
        colnames(private$raw_data)
James Lamb's avatar
James Lamb committed
340

Guolin Ke's avatar
Guolin Ke committed
341
      } else {
James Lamb's avatar
James Lamb committed
342

343
344
        # Trying to work with unknown dimensions is not possible
        stop("dim: cannot get dimensions before dataset has been constructed, please call lgb.Dataset.construct explicitly")
James Lamb's avatar
James Lamb committed
345

Guolin Ke's avatar
Guolin Ke committed
346
      }
James Lamb's avatar
James Lamb committed
347

Guolin Ke's avatar
Guolin Ke committed
348
    },
James Lamb's avatar
James Lamb committed
349

350
    # Set column names
Guolin Ke's avatar
Guolin Ke committed
351
    set_colnames = function(colnames) {
James Lamb's avatar
James Lamb committed
352

353
354
      # Check column names non-existence
      if (is.null(colnames)) {
355
        return(invisible(self))
356
      }
James Lamb's avatar
James Lamb committed
357

358
      # Check empty column names
Guolin Ke's avatar
Guolin Ke committed
359
      colnames <- as.character(colnames)
360
      if (length(colnames) == 0) {
361
        return(invisible(self))
362
      }
James Lamb's avatar
James Lamb committed
363

364
      # Write column names
Guolin Ke's avatar
Guolin Ke committed
365
366
      private$colnames <- colnames
      if (!lgb.is.null.handle(private$handle)) {
James Lamb's avatar
James Lamb committed
367

368
        # Merge names with tab separation
Guolin Ke's avatar
Guolin Ke committed
369
370
371
372
373
        merged_name <- paste0(as.list(private$colnames), collapse = "\t")
        lgb.call("LGBM_DatasetSetFeatureNames_R",
                 ret = NULL,
                 private$handle,
                 lgb.c_str(merged_name))
James Lamb's avatar
James Lamb committed
374

Guolin Ke's avatar
Guolin Ke committed
375
      }
James Lamb's avatar
James Lamb committed
376

377
      # Return self
378
      return(invisible(self))
James Lamb's avatar
James Lamb committed
379

Guolin Ke's avatar
Guolin Ke committed
380
    },
James Lamb's avatar
James Lamb committed
381

382
    # Get information
Guolin Ke's avatar
Guolin Ke committed
383
    getinfo = function(name) {
James Lamb's avatar
James Lamb committed
384

385
      # Create known attributes list
386
      INFONAMES <- c("label", "weight", "init_score", "group")
James Lamb's avatar
James Lamb committed
387

388
389
390
      # Check if attribute key is in the known attribute list
      if (!is.character(name) || length(name) != 1 || !name %in% INFONAMES) {
        stop("getinfo: name must one of the following: ", paste0(sQuote(INFONAMES), collapse = ", "))
Guolin Ke's avatar
Guolin Ke committed
391
      }
James Lamb's avatar
James Lamb committed
392

393
      # Check for info name and handle
394
395
396
397
      if (is.null(private$info[[name]])) {
        if (lgb.is.null.handle(private$handle)){
          stop("Cannot perform getinfo before construct Dataset.")
        }
398
        # Get field size of info
399
        info_len <- 0L
400
401
402
403
        info_len <- lgb.call("LGBM_DatasetGetFieldSize_R",
                             ret = info_len,
                             private$handle,
                             lgb.c_str(name))
James Lamb's avatar
James Lamb committed
404

405
        # Check if info is not empty
Guolin Ke's avatar
Guolin Ke committed
406
        if (info_len > 0) {
James Lamb's avatar
James Lamb committed
407

408
          # Get back fields
Guolin Ke's avatar
Guolin Ke committed
409
          ret <- NULL
410
411
412
413
414
          ret <- if (name == "group") {
            integer(info_len) # Integer
          } else {
            numeric(info_len) # Numeric
          }
James Lamb's avatar
James Lamb committed
415

416
417
418
419
          ret <- lgb.call("LGBM_DatasetGetField_R",
                          ret = ret,
                          private$handle,
                          lgb.c_str(name))
James Lamb's avatar
James Lamb committed
420

Guolin Ke's avatar
Guolin Ke committed
421
          private$info[[name]] <- ret
James Lamb's avatar
James Lamb committed
422

Guolin Ke's avatar
Guolin Ke committed
423
424
        }
      }
James Lamb's avatar
James Lamb committed
425

426
      private$info[[name]]
James Lamb's avatar
James Lamb committed
427

Guolin Ke's avatar
Guolin Ke committed
428
    },
James Lamb's avatar
James Lamb committed
429

430
    # Set information
Guolin Ke's avatar
Guolin Ke committed
431
    setinfo = function(name, info) {
James Lamb's avatar
James Lamb committed
432

433
      # Create known attributes list
434
      INFONAMES <- c("label", "weight", "init_score", "group")
James Lamb's avatar
James Lamb committed
435

436
437
438
439
      # Check if attribute key is in the known attribute list
      if (!is.character(name) || length(name) != 1 || !name %in% INFONAMES) {
        stop("setinfo: name must one of the following: ", paste0(sQuote(INFONAMES), collapse = ", "))
      }
James Lamb's avatar
James Lamb committed
440

441
442
443
444
445
446
      # Check for type of information
      info <- if (name == "group") {
        as.integer(info) # Integer
      } else {
        as.numeric(info) # Numeric
      }
James Lamb's avatar
James Lamb committed
447

448
      # Store information privately
Guolin Ke's avatar
Guolin Ke committed
449
      private$info[[name]] <- info
James Lamb's avatar
James Lamb committed
450

451
      if (!lgb.is.null.handle(private$handle) && !is.null(info)) {
James Lamb's avatar
James Lamb committed
452

Guolin Ke's avatar
Guolin Ke committed
453
        if (length(info) > 0) {
James Lamb's avatar
James Lamb committed
454

455
456
457
458
459
460
          lgb.call("LGBM_DatasetSetField_R",
                   ret = NULL,
                   private$handle,
                   lgb.c_str(name),
                   info,
                   length(info))
James Lamb's avatar
James Lamb committed
461

Guolin Ke's avatar
Guolin Ke committed
462
        }
James Lamb's avatar
James Lamb committed
463

Guolin Ke's avatar
Guolin Ke committed
464
      }
James Lamb's avatar
James Lamb committed
465

466
      # Return self
467
      return(invisible(self))
James Lamb's avatar
James Lamb committed
468

Guolin Ke's avatar
Guolin Ke committed
469
    },
James Lamb's avatar
James Lamb committed
470

471
    # Slice dataset
Guolin Ke's avatar
Guolin Ke committed
472
    slice = function(idxset, ...) {
James Lamb's avatar
James Lamb committed
473

474
475
476
477
478
479
480
481
482
483
484
      # Perform slicing
      Dataset$new(NULL,
                  private$params,
                  self,
                  private$colnames,
                  private$categorical_feature,
                  private$predictor,
                  private$free_raw_data,
                  idxset,
                  NULL,
                  ...)
James Lamb's avatar
James Lamb committed
485

Guolin Ke's avatar
Guolin Ke committed
486
    },
James Lamb's avatar
James Lamb committed
487

488
    # Update parameters
489
    update_params = function(params) {
James Lamb's avatar
James Lamb committed
490

491
      # Parameter updating
Guolin Ke's avatar
Guolin Ke committed
492
      private$params <- modifyList(private$params, params)
493
      return(invisible(self))
James Lamb's avatar
James Lamb committed
494

Guolin Ke's avatar
Guolin Ke committed
495
    },
James Lamb's avatar
James Lamb committed
496

497
    # Set categorical feature parameter
498
    set_categorical_feature = function(categorical_feature) {
James Lamb's avatar
James Lamb committed
499

500
501
      # Check for identical input
      if (identical(private$categorical_feature, categorical_feature)) {
502
        return(invisible(self))
503
      }
James Lamb's avatar
James Lamb committed
504

505
      # Check for empty data
506
      if (is.null(private$raw_data)) {
507
508
        stop("set_categorical_feature: cannot set categorical feature after freeing raw data,
          please set ", sQuote("free_raw_data = FALSE"), " when you construct lgb.Dataset")
509
      }
James Lamb's avatar
James Lamb committed
510

511
      # Overwrite categorical features
512
      private$categorical_feature <- categorical_feature
James Lamb's avatar
James Lamb committed
513

514
      # Finalize and return self
515
      self$finalize()
516
      return(invisible(self))
James Lamb's avatar
James Lamb committed
517

518
    },
James Lamb's avatar
James Lamb committed
519

520
    # Set reference
Guolin Ke's avatar
Guolin Ke committed
521
    set_reference = function(reference) {
James Lamb's avatar
James Lamb committed
522

523
      # Set known references
524
      self$set_categorical_feature(reference$.__enclos_env__$private$categorical_feature)
Guolin Ke's avatar
Guolin Ke committed
525
526
      self$set_colnames(reference$get_colnames())
      private$set_predictor(reference$.__enclos_env__$private$predictor)
James Lamb's avatar
James Lamb committed
527

528
529
      # Check for identical references
      if (identical(private$reference, reference)) {
530
        return(invisible(self))
531
      }
James Lamb's avatar
James Lamb committed
532

533
      # Check for empty data
Guolin Ke's avatar
Guolin Ke committed
534
      if (is.null(private$raw_data)) {
James Lamb's avatar
James Lamb committed
535

536
537
        stop("set_reference: cannot set reference after freeing raw data,
          please set ", sQuote("free_raw_data = FALSE"), " when you construct lgb.Dataset")
James Lamb's avatar
James Lamb committed
538

Guolin Ke's avatar
Guolin Ke committed
539
      }
James Lamb's avatar
James Lamb committed
540

541
      # Check for non-existing reference
Guolin Ke's avatar
Guolin Ke committed
542
      if (!is.null(reference)) {
James Lamb's avatar
James Lamb committed
543

544
        # Reference is unknown
Guolin Ke's avatar
Guolin Ke committed
545
        if (!lgb.check.r6.class(reference, "lgb.Dataset")) {
546
          stop("set_reference: Can only use lgb.Dataset as a reference")
Guolin Ke's avatar
Guolin Ke committed
547
        }
James Lamb's avatar
James Lamb committed
548

Guolin Ke's avatar
Guolin Ke committed
549
      }
James Lamb's avatar
James Lamb committed
550

551
      # Store reference
Guolin Ke's avatar
Guolin Ke committed
552
      private$reference <- reference
James Lamb's avatar
James Lamb committed
553

554
      # Finalize and return self
Guolin Ke's avatar
Guolin Ke committed
555
      self$finalize()
556
      return(invisible(self))
James Lamb's avatar
James Lamb committed
557

Guolin Ke's avatar
Guolin Ke committed
558
    },
James Lamb's avatar
James Lamb committed
559

560
    # Save binary model
Guolin Ke's avatar
Guolin Ke committed
561
    save_binary = function(fname) {
James Lamb's avatar
James Lamb committed
562

563
      # Store binary data
Guolin Ke's avatar
Guolin Ke committed
564
565
566
567
568
      self$construct()
      lgb.call("LGBM_DatasetSaveBinary_R",
               ret = NULL,
               private$handle,
               lgb.c_str(fname))
569
      return(invisible(self))
Guolin Ke's avatar
Guolin Ke committed
570
    }
James Lamb's avatar
James Lamb committed
571

Guolin Ke's avatar
Guolin Ke committed
572
573
  ),
  private = list(
574
575
576
577
578
    handle = NULL,
    raw_data = NULL,
    params = list(),
    reference = NULL,
    colnames = NULL,
579
    categorical_feature = NULL,
580
581
582
583
    predictor = NULL,
    free_raw_data = TRUE,
    used_indices = NULL,
    info = NULL,
James Lamb's avatar
James Lamb committed
584

585
586
    # Get handle
    get_handle = function() {
James Lamb's avatar
James Lamb committed
587

588
589
590
591
      # Get handle and construct if needed
      if (lgb.is.null.handle(private$handle)) {
        self$construct()
      }
592
      private$handle
James Lamb's avatar
James Lamb committed
593

Guolin Ke's avatar
Guolin Ke committed
594
    },
James Lamb's avatar
James Lamb committed
595

596
    # Set predictor
Guolin Ke's avatar
Guolin Ke committed
597
    set_predictor = function(predictor) {
James Lamb's avatar
James Lamb committed
598

599
600
      # Return self is identical predictor
      if (identical(private$predictor, predictor)) {
601
        return(invisible(self))
602
      }
James Lamb's avatar
James Lamb committed
603

604
      # Check for empty data
Guolin Ke's avatar
Guolin Ke committed
605
      if (is.null(private$raw_data)) {
606
607
        stop("set_predictor: cannot set predictor after free raw data,
          please set ", sQuote("free_raw_data = FALSE"), " when you construct lgb.Dataset")
Guolin Ke's avatar
Guolin Ke committed
608
      }
James Lamb's avatar
James Lamb committed
609

610
      # Check for empty predictor
Guolin Ke's avatar
Guolin Ke committed
611
      if (!is.null(predictor)) {
James Lamb's avatar
James Lamb committed
612

613
        # Predictor is unknown
Guolin Ke's avatar
Guolin Ke committed
614
        if (!lgb.check.r6.class(predictor, "lgb.Predictor")) {
615
          stop("set_predictor: Can only use lgb.Predictor as predictor")
Guolin Ke's avatar
Guolin Ke committed
616
        }
James Lamb's avatar
James Lamb committed
617

Guolin Ke's avatar
Guolin Ke committed
618
      }
James Lamb's avatar
James Lamb committed
619

620
      # Store predictor
Guolin Ke's avatar
Guolin Ke committed
621
      private$predictor <- predictor
James Lamb's avatar
James Lamb committed
622

623
      # Finalize and return self
Guolin Ke's avatar
Guolin Ke committed
624
      self$finalize()
625
      return(invisible(self))
James Lamb's avatar
James Lamb committed
626

Guolin Ke's avatar
Guolin Ke committed
627
    }
James Lamb's avatar
James Lamb committed
628

Guolin Ke's avatar
Guolin Ke committed
629
630
631
  )
)

wxchan's avatar
wxchan committed
632
#' Construct lgb.Dataset object
Guolin Ke's avatar
Guolin Ke committed
633
#'
wxchan's avatar
wxchan committed
634
#' Construct lgb.Dataset object from dense matrix, sparse matrix
Guolin Ke's avatar
Guolin Ke committed
635
636
637
638
639
640
#' or local file (that was created previously by saving an \code{lgb.Dataset}).
#'
#' @param data a \code{matrix} object, a \code{dgCMatrix} object or a character representing a filename
#' @param params a list of parameters
#' @param reference reference dataset
#' @param colnames names of columns
641
#' @param categorical_feature categorical features
Guolin Ke's avatar
Guolin Ke committed
642
643
644
#' @param free_raw_data TRUE for need to free raw data after construct
#' @param info a list of information of the lgb.Dataset object
#' @param ... other information to pass to \code{info} or parameters pass to \code{params}
James Lamb's avatar
James Lamb committed
645
#'
Guolin Ke's avatar
Guolin Ke committed
646
#' @return constructed dataset
James Lamb's avatar
James Lamb committed
647
#'
Guolin Ke's avatar
Guolin Ke committed
648
#' @examples
649
#' \dontrun{
650
651
652
653
654
655
656
#' library(lightgbm)
#' data(agaricus.train, package = "lightgbm")
#' train <- agaricus.train
#' dtrain <- lgb.Dataset(train$data, label = train$label)
#' lgb.Dataset.save(dtrain, "lgb.Dataset.data")
#' dtrain <- lgb.Dataset("lgb.Dataset.data")
#' lgb.Dataset.construct(dtrain)
657
#' }
James Lamb's avatar
James Lamb committed
658
#'
Guolin Ke's avatar
Guolin Ke committed
659
660
#' @export
lgb.Dataset <- function(data,
661
662
663
                        params = list(),
                        reference = NULL,
                        colnames = NULL,
664
                        categorical_feature = NULL,
665
666
                        free_raw_data = TRUE,
                        info = list(),
Guolin Ke's avatar
Guolin Ke committed
667
                        ...) {
James Lamb's avatar
James Lamb committed
668

669
  # Create new dataset
670
  invisible(Dataset$new(data,
671
672
673
674
675
676
677
678
              params,
              reference,
              colnames,
              categorical_feature,
              NULL,
              free_raw_data,
              NULL,
              info,
679
              ...))
James Lamb's avatar
James Lamb committed
680

Guolin Ke's avatar
Guolin Ke committed
681
682
}

wxchan's avatar
wxchan committed
683
#' Construct validation data
James Lamb's avatar
James Lamb committed
684
#'
wxchan's avatar
wxchan committed
685
#' Construct validation data according to training data
James Lamb's avatar
James Lamb committed
686
#'
Guolin Ke's avatar
Guolin Ke committed
687
688
689
690
#' @param dataset \code{lgb.Dataset} object, training data
#' @param data a \code{matrix} object, a \code{dgCMatrix} object or a character representing a filename
#' @param info a list of information of the lgb.Dataset object
#' @param ... other information to pass to \code{info}.
James Lamb's avatar
James Lamb committed
691
#'
Guolin Ke's avatar
Guolin Ke committed
692
#' @return constructed dataset
James Lamb's avatar
James Lamb committed
693
#'
Guolin Ke's avatar
Guolin Ke committed
694
#' @examples
695
#' \dontrun{
696
697
698
699
700
701
702
#' 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)
703
#' }
James Lamb's avatar
James Lamb committed
704
#'
Guolin Ke's avatar
Guolin Ke committed
705
#' @export
706
lgb.Dataset.create.valid <- function(dataset, data, info = list(), ...) {
James Lamb's avatar
James Lamb committed
707

708
  # Check if dataset is not a dataset
709
710
  if (!lgb.is.Dataset(dataset)) {
    stop("lgb.Dataset.create.valid: input data should be an lgb.Dataset object")
Guolin Ke's avatar
Guolin Ke committed
711
  }
James Lamb's avatar
James Lamb committed
712

713
  # Create validation dataset
714
  invisible(dataset$create_valid(data, info, ...))
James Lamb's avatar
James Lamb committed
715

716
}
Guolin Ke's avatar
Guolin Ke committed
717

718
#' Construct Dataset explicitly
James Lamb's avatar
James Lamb committed
719
#'
Guolin Ke's avatar
Guolin Ke committed
720
#' @param dataset Object of class \code{lgb.Dataset}
James Lamb's avatar
James Lamb committed
721
#'
Guolin Ke's avatar
Guolin Ke committed
722
#' @examples
723
#' \dontrun{
724
725
726
727
728
#' library(lightgbm)
#' data(agaricus.train, package = "lightgbm")
#' train <- agaricus.train
#' dtrain <- lgb.Dataset(train$data, label = train$label)
#' lgb.Dataset.construct(dtrain)
729
#' }
James Lamb's avatar
James Lamb committed
730
#'
Guolin Ke's avatar
Guolin Ke committed
731
732
#' @export
lgb.Dataset.construct <- function(dataset) {
James Lamb's avatar
James Lamb committed
733

734
  # Check if dataset is not a dataset
735
736
  if (!lgb.is.Dataset(dataset)) {
    stop("lgb.Dataset.construct: input data should be an lgb.Dataset object")
Guolin Ke's avatar
Guolin Ke committed
737
  }
James Lamb's avatar
James Lamb committed
738

739
  # Construct the dataset
740
  invisible(dataset$construct())
James Lamb's avatar
James Lamb committed
741

Guolin Ke's avatar
Guolin Ke committed
742
743
}

744
#' Dimensions of an lgb.Dataset
James Lamb's avatar
James Lamb committed
745
#'
Guolin Ke's avatar
Guolin Ke committed
746
747
748
#' Returns a vector of numbers of rows and of columns in an \code{lgb.Dataset}.
#' @param x Object of class \code{lgb.Dataset}
#' @param ... other parameters
James Lamb's avatar
James Lamb committed
749
#'
Guolin Ke's avatar
Guolin Ke committed
750
#' @return a vector of numbers of rows and of columns
James Lamb's avatar
James Lamb committed
751
#'
Guolin Ke's avatar
Guolin Ke committed
752
753
754
#' @details
#' Note: since \code{nrow} and \code{ncol} internally use \code{dim}, they can also
#' be directly used with an \code{lgb.Dataset} object.
James Lamb's avatar
James Lamb committed
755
#'
Guolin Ke's avatar
Guolin Ke committed
756
#' @examples
757
758
759
760
761
#' \dontrun{
#' library(lightgbm)
#' data(agaricus.train, package = "lightgbm")
#' train <- agaricus.train
#' dtrain <- lgb.Dataset(train$data, label = train$label)
James Lamb's avatar
James Lamb committed
762
#'
763
764
765
#' stopifnot(nrow(dtrain) == nrow(train$data))
#' stopifnot(ncol(dtrain) == ncol(train$data))
#' stopifnot(all(dim(dtrain) == dim(train$data)))
766
#' }
James Lamb's avatar
James Lamb committed
767
#'
Guolin Ke's avatar
Guolin Ke committed
768
769
770
#' @rdname dim
#' @export
dim.lgb.Dataset <- function(x, ...) {
James Lamb's avatar
James Lamb committed
771

772
  # Check if dataset is not a dataset
773
774
  if (!lgb.is.Dataset(x)) {
    stop("dim.lgb.Dataset: input data should be an lgb.Dataset object")
Guolin Ke's avatar
Guolin Ke committed
775
  }
James Lamb's avatar
James Lamb committed
776

777
  # Return dimensions
778
  x$dim()
James Lamb's avatar
James Lamb committed
779

Guolin Ke's avatar
Guolin Ke committed
780
781
782
783
784
}

#' Handling of column names of \code{lgb.Dataset}
#'
#' Only column names are supported for \code{lgb.Dataset}, thus setting of
785
#' row names would have no effect and returned row names would be NULL.
Guolin Ke's avatar
Guolin Ke committed
786
787
788
789
790
791
792
793
794
795
#'
#' @param x object of class \code{lgb.Dataset}
#' @param value a list of two elements: the first one is ignored
#'        and the second one is column names
#'
#' @details
#' Generic \code{dimnames} methods are used by \code{colnames}.
#' Since row names are irrelevant, it is recommended to use \code{colnames} directly.
#'
#' @examples
796
797
798
799
800
801
802
803
804
805
#' \dontrun{
#' library(lightgbm)
#' data(agaricus.train, package = "lightgbm")
#' train <- agaricus.train
#' dtrain <- lgb.Dataset(train$data, label = train$label)
#' lgb.Dataset.construct(dtrain)
#' dimnames(dtrain)
#' colnames(dtrain)
#' colnames(dtrain) <- make.names(1:ncol(train$data))
#' print(dtrain, verbose = TRUE)
806
#' }
James Lamb's avatar
James Lamb committed
807
#'
Guolin Ke's avatar
Guolin Ke committed
808
809
810
#' @rdname dimnames.lgb.Dataset
#' @export
dimnames.lgb.Dataset <- function(x) {
James Lamb's avatar
James Lamb committed
811

812
  # Check if dataset is not a dataset
813
814
  if (!lgb.is.Dataset(x)) {
    stop("dimnames.lgb.Dataset: input data should be an lgb.Dataset object")
Guolin Ke's avatar
Guolin Ke committed
815
  }
James Lamb's avatar
James Lamb committed
816

817
  # Return dimension names
818
  list(NULL, x$get_colnames())
James Lamb's avatar
James Lamb committed
819

Guolin Ke's avatar
Guolin Ke committed
820
821
822
823
824
}

#' @rdname dimnames.lgb.Dataset
#' @export
`dimnames<-.lgb.Dataset` <- function(x, value) {
James Lamb's avatar
James Lamb committed
825

826
827
  # Check if invalid element list
  if (!is.list(value) || length(value) != 2L) {
828
    stop("invalid ", sQuote("value"), " given: must be a list of two elements")
829
  }
James Lamb's avatar
James Lamb committed
830

831
832
833
834
  # Check for unknown row names
  if (!is.null(value[[1L]])) {
    stop("lgb.Dataset does not have rownames")
  }
James Lamb's avatar
James Lamb committed
835

836
  # Check for second value missing
Guolin Ke's avatar
Guolin Ke committed
837
  if (is.null(value[[2]])) {
James Lamb's avatar
James Lamb committed
838

839
    # No column names
Guolin Ke's avatar
Guolin Ke committed
840
841
    x$set_colnames(NULL)
    return(x)
James Lamb's avatar
James Lamb committed
842

843
  }
James Lamb's avatar
James Lamb committed
844

845
846
847
  # Check for unmatching column size
  if (ncol(x) != length(value[[2]])) {
    stop("can't assign ", sQuote(length(value[[2]])), " colnames to an lgb.Dataset with ", sQuote(ncol(x)), " columns")
Guolin Ke's avatar
Guolin Ke committed
848
  }
James Lamb's avatar
James Lamb committed
849

850
  # Set column names properly, and return
Guolin Ke's avatar
Guolin Ke committed
851
  x$set_colnames(value[[2]])
852
  x
James Lamb's avatar
James Lamb committed
853

Guolin Ke's avatar
Guolin Ke committed
854
855
}

856
#' Slice a dataset
James Lamb's avatar
James Lamb committed
857
#'
858
#' Get a new \code{lgb.Dataset} containing the specified rows of
James Lamb's avatar
James Lamb committed
859
860
#' original lgb.Dataset object
#' 
Guolin Ke's avatar
Guolin Ke committed
861
862
863
864
#' @param dataset Object of class "lgb.Dataset"
#' @param idxset a integer vector of indices of rows needed
#' @param ... other parameters (currently not used)
#' @return constructed sub dataset
James Lamb's avatar
James Lamb committed
865
#'
Guolin Ke's avatar
Guolin Ke committed
866
#' @examples
867
#' \dontrun{
868
869
870
871
#' library(lightgbm)
#' data(agaricus.train, package = "lightgbm")
#' train <- agaricus.train
#' dtrain <- lgb.Dataset(train$data, label = train$label)
James Lamb's avatar
James Lamb committed
872
#'
873
874
#' dsub <- lightgbm::slice(dtrain, 1:42)
#' labels <- lightgbm::getinfo(dsub, "label")
875
#' }
James Lamb's avatar
James Lamb committed
876
#'
Guolin Ke's avatar
Guolin Ke committed
877
#' @export
878
879
880
slice <- function(dataset, ...) {
  UseMethod("slice")
}
Guolin Ke's avatar
Guolin Ke committed
881
882
883
884

#' @rdname slice
#' @export
slice.lgb.Dataset <- function(dataset, idxset, ...) {
James Lamb's avatar
James Lamb committed
885

886
  # Check if dataset is not a dataset
887
888
  if (!lgb.is.Dataset(dataset)) {
    stop("slice.lgb.Dataset: input dataset should be an lgb.Dataset object")
Guolin Ke's avatar
Guolin Ke committed
889
  }
James Lamb's avatar
James Lamb committed
890

891
  # Return sliced set
892
  invisible(dataset$slice(idxset, ...))
James Lamb's avatar
James Lamb committed
893

Guolin Ke's avatar
Guolin Ke committed
894
895
896
}

#' Get information of an lgb.Dataset object
James Lamb's avatar
James Lamb committed
897
#'
Guolin Ke's avatar
Guolin Ke committed
898
899
900
901
#' @param dataset Object of class \code{lgb.Dataset}
#' @param name the name of the information field to get (see details)
#' @param ... other parameters
#' @return info data
James Lamb's avatar
James Lamb committed
902
#'
Guolin Ke's avatar
Guolin Ke committed
903
904
#' @details
#' The \code{name} field can be one of the following:
James Lamb's avatar
James Lamb committed
905
#'
Guolin Ke's avatar
Guolin Ke committed
906
907
908
909
910
911
#' \itemize{
#'     \item \code{label}: label lightgbm learn from ;
#'     \item \code{weight}: to do a weight rescale ;
#'     \item \code{group}: group size
#'     \item \code{init_score}: initial score is the base prediction lightgbm will boost from ;
#' }
James Lamb's avatar
James Lamb committed
912
#'
Guolin Ke's avatar
Guolin Ke committed
913
#' @examples
914
#' \dontrun{
915
916
917
918
919
#' library(lightgbm)
#' data(agaricus.train, package = "lightgbm")
#' train <- agaricus.train
#' dtrain <- lgb.Dataset(train$data, label = train$label)
#' lgb.Dataset.construct(dtrain)
James Lamb's avatar
James Lamb committed
920
#'
921
922
#' labels <- lightgbm::getinfo(dtrain, "label")
#' lightgbm::setinfo(dtrain, "label", 1 - labels)
James Lamb's avatar
James Lamb committed
923
#'
924
925
#' labels2 <- lightgbm::getinfo(dtrain, "label")
#' stopifnot(all(labels2 == 1 - labels))
926
#' }
James Lamb's avatar
James Lamb committed
927
#'
Guolin Ke's avatar
Guolin Ke committed
928
#' @export
929
930
931
getinfo <- function(dataset, ...) {
  UseMethod("getinfo")
}
Guolin Ke's avatar
Guolin Ke committed
932
933
934
935

#' @rdname getinfo
#' @export
getinfo.lgb.Dataset <- function(dataset, name, ...) {
James Lamb's avatar
James Lamb committed
936

937
  # Check if dataset is not a dataset
938
939
  if (!lgb.is.Dataset(dataset)) {
    stop("getinfo.lgb.Dataset: input dataset should be an lgb.Dataset object")
Guolin Ke's avatar
Guolin Ke committed
940
  }
James Lamb's avatar
James Lamb committed
941

942
  # Return information
943
  dataset$getinfo(name)
James Lamb's avatar
James Lamb committed
944

Guolin Ke's avatar
Guolin Ke committed
945
946
947
}

#' Set information of an lgb.Dataset object
James Lamb's avatar
James Lamb committed
948
#'
Guolin Ke's avatar
Guolin Ke committed
949
950
951
952
953
#' @param dataset Object of class "lgb.Dataset"
#' @param name the name of the field to get
#' @param info the specific field of information to set
#' @param ... other parameters
#' @return passed object
James Lamb's avatar
James Lamb committed
954
#'
Guolin Ke's avatar
Guolin Ke committed
955
956
#' @details
#' The \code{name} field can be one of the following:
James Lamb's avatar
James Lamb committed
957
#'
Guolin Ke's avatar
Guolin Ke committed
958
959
960
961
962
963
#' \itemize{
#'     \item \code{label}: label lightgbm learn from ;
#'     \item \code{weight}: to do a weight rescale ;
#'     \item \code{init_score}: initial score is the base prediction lightgbm will boost from ;
#'     \item \code{group}.
#' }
James Lamb's avatar
James Lamb committed
964
#'
Guolin Ke's avatar
Guolin Ke committed
965
#' @examples
966
#' \dontrun{
967
968
969
970
971
#' library(lightgbm)
#' data(agaricus.train, package = "lightgbm")
#' train <- agaricus.train
#' dtrain <- lgb.Dataset(train$data, label = train$label)
#' lgb.Dataset.construct(dtrain)
James Lamb's avatar
James Lamb committed
972
#'
973
974
#' labels <- lightgbm::getinfo(dtrain, "label")
#' lightgbm::setinfo(dtrain, "label", 1 - labels)
James Lamb's avatar
James Lamb committed
975
#'
976
977
#' labels2 <- lightgbm::getinfo(dtrain, "label")
#' stopifnot(all.equal(labels2, 1 - labels))
978
#' }
James Lamb's avatar
James Lamb committed
979
#'
Guolin Ke's avatar
Guolin Ke committed
980
#' @export
981
982
983
setinfo <- function(dataset, ...) {
  UseMethod("setinfo")
}
Guolin Ke's avatar
Guolin Ke committed
984
985
986
987

#' @rdname setinfo
#' @export
setinfo.lgb.Dataset <- function(dataset, name, info, ...) {
James Lamb's avatar
James Lamb committed
988

989
  # Check if dataset is not a dataset
990
991
  if (!lgb.is.Dataset(dataset)) {
    stop("setinfo.lgb.Dataset: input dataset should be an lgb.Dataset object")
Guolin Ke's avatar
Guolin Ke committed
992
  }
James Lamb's avatar
James Lamb committed
993

994
  # Set information
995
  invisible(dataset$setinfo(name, info))
Guolin Ke's avatar
Guolin Ke committed
996
997
}

998
#' Set categorical feature of \code{lgb.Dataset}
James Lamb's avatar
James Lamb committed
999
#'
1000
1001
#' @param dataset object of class \code{lgb.Dataset}
#' @param categorical_feature categorical features
James Lamb's avatar
James Lamb committed
1002
#'
1003
#' @return passed dataset
James Lamb's avatar
James Lamb committed
1004
#'
1005
1006
#' @examples
#' \dontrun{
1007
1008
1009
1010
1011
1012
1013
#' library(lightgbm)
#' data(agaricus.train, package = "lightgbm")
#' train <- agaricus.train
#' dtrain <- lgb.Dataset(train$data, label = train$label)
#' lgb.Dataset.save(dtrain, "lgb.Dataset.data")
#' dtrain <- lgb.Dataset("lgb.Dataset.data")
#' lgb.Dataset.set.categorical(dtrain, 1:2)
1014
#' }
James Lamb's avatar
James Lamb committed
1015
#'
1016
1017
1018
#' @rdname lgb.Dataset.set.categorical
#' @export
lgb.Dataset.set.categorical <- function(dataset, categorical_feature) {
James Lamb's avatar
James Lamb committed
1019

1020
  # Check if dataset is not a dataset
1021
1022
1023
  if (!lgb.is.Dataset(dataset)) {
    stop("lgb.Dataset.set.categorical: input dataset should be an lgb.Dataset object")
  }
James Lamb's avatar
James Lamb committed
1024

1025
  # Set categoricals
1026
  invisible(dataset$set_categorical_feature(categorical_feature))
James Lamb's avatar
James Lamb committed
1027

1028
1029
}

1030
#' Set reference of \code{lgb.Dataset}
James Lamb's avatar
James Lamb committed
1031
#'
1032
#' If you want to use validation data, you should set reference to training data
James Lamb's avatar
James Lamb committed
1033
#'
Guolin Ke's avatar
Guolin Ke committed
1034
1035
#' @param dataset object of class \code{lgb.Dataset}
#' @param reference object of class \code{lgb.Dataset}
James Lamb's avatar
James Lamb committed
1036
#'
Guolin Ke's avatar
Guolin Ke committed
1037
#' @return passed dataset
James Lamb's avatar
James Lamb committed
1038
#'
Guolin Ke's avatar
Guolin Ke committed
1039
#' @examples
1040
#' \dontrun{
1041
1042
1043
1044
1045
1046
1047
1048
#' 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(test$data, test = train$label)
#' lgb.Dataset.set.reference(dtest, dtrain)
1049
#' }
James Lamb's avatar
James Lamb committed
1050
#'
Guolin Ke's avatar
Guolin Ke committed
1051
1052
1053
#' @rdname lgb.Dataset.set.reference
#' @export
lgb.Dataset.set.reference <- function(dataset, reference) {
James Lamb's avatar
James Lamb committed
1054

1055
  # Check if dataset is not a dataset
1056
1057
  if (!lgb.is.Dataset(dataset)) {
    stop("lgb.Dataset.set.reference: input dataset should be an lgb.Dataset object")
Guolin Ke's avatar
Guolin Ke committed
1058
  }
James Lamb's avatar
James Lamb committed
1059

1060
  # Set reference
1061
  invisible(dataset$set_reference(reference))
Guolin Ke's avatar
Guolin Ke committed
1062
1063
}

1064
#' Save \code{lgb.Dataset} to a binary file
James Lamb's avatar
James Lamb committed
1065
#'
Guolin Ke's avatar
Guolin Ke committed
1066
1067
#' @param dataset object of class \code{lgb.Dataset}
#' @param fname object filename of output file
James Lamb's avatar
James Lamb committed
1068
#'
Guolin Ke's avatar
Guolin Ke committed
1069
#' @return passed dataset
James Lamb's avatar
James Lamb committed
1070
#'
Guolin Ke's avatar
Guolin Ke committed
1071
#' @examples
James Lamb's avatar
James Lamb committed
1072
#'
1073
#' \dontrun{
1074
1075
1076
1077
1078
#' library(lightgbm)
#' data(agaricus.train, package = "lightgbm")
#' train <- agaricus.train
#' dtrain <- lgb.Dataset(train$data, label = train$label)
#' lgb.Dataset.save(dtrain, "data.bin")
1079
#' }
James Lamb's avatar
James Lamb committed
1080
#'
Guolin Ke's avatar
Guolin Ke committed
1081
1082
1083
#' @rdname lgb.Dataset.save
#' @export
lgb.Dataset.save <- function(dataset, fname) {
James Lamb's avatar
James Lamb committed
1084

1085
  # Check if dataset is not a dataset
1086
1087
  if (!lgb.is.Dataset(dataset)) {
    stop("lgb.Dataset.set: input dataset should be an lgb.Dataset object")
Guolin Ke's avatar
Guolin Ke committed
1088
  }
James Lamb's avatar
James Lamb committed
1089

1090
  # File-type is not matching
1091
1092
  if (!is.character(fname)) {
    stop("lgb.Dataset.set: fname should be a character or a file connection")
Guolin Ke's avatar
Guolin Ke committed
1093
  }
James Lamb's avatar
James Lamb committed
1094

1095
  # Store binary
1096
  invisible(dataset$save_binary(fname))
Guolin Ke's avatar
Guolin Ke committed
1097
}