lgb.convert.R 2.92 KB
Newer Older
1
#' @name lgb.convert
2
3
#' @title Data preparator for LightGBM datasets (integer)
#' @description Attempts to prepare a clean dataset to prepare to put in a \code{lgb.Dataset}.
4
5
#'              Factors and characters are converted to integer.
#'              Please use \code{\link{lgb.convert_with_rules}} if you want to apply this transformation to
6
#'              other datasets. This is useful if you have a specific need for integer dataset instead
7
8
9
#'              of numeric dataset.
#'
#'              NOTE: In previous releases of LightGBM, this function was called \code{lgb.prepare}.
10
#' @param data A data.frame or data.table to prepare.
11
12
#' @return The cleaned dataset. It must be converted to a matrix format (\code{as.matrix})
#'         for input in \code{lgb.Dataset}.
13
#'
14
#' @examples
15
#' \dontrun{
16
#' data(iris)
17
#'
18
#' str(iris)
19
#'
20
#' # Convert all factors/chars to integer
21
#' str(lgb.convert(data = iris))
22
#'
23
24
25
#' # When lightgbm package is installed, and you do not want to load it
#' # You can still use the function!
#' lgb.unloader()
26
#' str(lgb.convert(data = iris))
27
28
29
30
31
32
#' # 'data.frame':	150 obs. of  5 variables:
#' # $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#' # $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#' # $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#' # $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#' # $ Species     : int  1 1 1 1 1 1 1 1 1 1 ...
33
#' }
34
#' @export
35
lgb.convert <- function(data) {
36

37
38
  # data.table not behaving like data.frame
  if (inherits(data, "data.table")) {
39

40
    # Get data classes
41
    list_classes <- vapply(data, class, character(1L))
42

43
    # Convert characters to integer
44
    is_char <- which(list_classes == "character")
45
    if (length(is_char) > 0L) {
46
47
      data[, (is_char) := lapply(.SD, function(x) {as.integer(as.factor(x))}), .SDcols = is_char]
    }
48

49
    # Convert factors to integer
50
    is_fact <- c(which(list_classes == "factor"), is_char)
51
    if (length(is_fact) > 0L) {
52
53
      data[, (is_fact) := lapply(.SD, function(x) {as.integer(x)}), .SDcols = is_fact]
    }
54

55
  } else {
56

57
58
    # Default routine (data.frame)
    if (inherits(data, "data.frame")) {
59

60
      # Get data classes
61
      list_classes <- vapply(data, class, character(1L))
62

63
64
      # Convert characters to factors to numeric (integer is more efficient actually)
      is_char <- which(list_classes == "character")
65
      if (length(is_char) > 0L) {
66
67
        data[is_char] <- lapply(data[is_char], function(x) {as.integer(as.factor(x))})
      }
68

69
70
      # Convert factors to numeric (integer is more efficient actually)
      is_fact <- which(list_classes == "factor")
71
      if (length(is_fact) > 0L) {
72
73
        data[is_fact] <- lapply(data[is_fact], function(x) {as.integer(x)})
      }
74

75
    } else {
76

77
      stop(
78
        "lgb.convert: you provided "
79
80
81
        , paste(class(data), collapse = " & ")
        , " but data should have class data.frame or data.table"
      )
82

83
    }
84

85
  }
86

87
  return(data)
88

89
}