lgb.convert.R 2.94 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
15
#' @examples
#' data(iris)
16
#'
17
#' str(iris)
18
#'
19
#' # Convert all factors/chars to integer
20
#' str(lgb.convert(data = iris))
21
#'
22
#' \dontrun{
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(lightgbm::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
#'
35
#' @export
36
lgb.convert <- function(data) {
37

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

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

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

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

56
  } else {
57

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

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

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

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

76
    } else {
77

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

84
    }
85

86
  }
87

88
  return(data)
89

90
}