tlverse / sl3
Showing 2 of 143 files from the diff.
Newly tracked file
R/Lrnr_lightgbm.R created.
Other files ignored by Codecov
man/Lrnr_glm.Rd has changed.
man/Lrnr_lstm.Rd has changed.
man/Stack.Rd has changed.
man/Lrnr_hts.Rd has changed.
NAMESPACE has changed.
man/Lrnr_gam.Rd has changed.
man/Lrnr_nnls.Rd has changed.
docs/sitemap.xml has changed.
docs/pkgdown.yml has changed.
man/Lrnr_solnp.Rd has changed.
man/Lrnr_tsDyn.Rd has changed.
man/Lrnr_svm.Rd has changed.
man/Lrnr_arima.Rd has changed.
man/Lrnr_gts.Rd has changed.
man/Lrnr_base.Rd has changed.
man/Lrnr_gbm.Rd has changed.
man/Lrnr_rpart.Rd has changed.
man/Lrnr_grf.Rd has changed.
man/Lrnr_nnet.Rd has changed.
man/Lrnr_caret.Rd has changed.
man/Lrnr_pca.Rd has changed.
man/Lrnr_earth.Rd has changed.
man/Lrnr_sl.Rd has changed.
man/Lrnr_optim.Rd has changed.
man/Lrnr_cv.Rd has changed.
man/Lrnr_bound.Rd has changed.
man/Lrnr_mean.Rd has changed.
DESCRIPTION has changed.
man/Pipeline.Rd has changed.

@@ -0,0 +1,155 @@
Loading
1 +
#' LightGBM: Light Gradient Boosting Machine
2 +
#'
3 +
#' This learner provides fitting procedures for \code{lightgbm} models, using
4 +
#' \pkg{lightgbm}, via \code{\link[lightgbm]{lgb.train}}. These gradient
5 +
#' boosted classification and regression tree models feature faster training
6 +
#' speed and higher efficiency, Lower memory usage, better accuracy, and
7 +
#' improved handling large-scale data. For details on the fitting procedure,
8 +
#' consult the documentation of the \pkg{lightgbm} package.
9 +
#'
10 +
#' @docType class
11 +
#'
12 +
#' @importFrom R6 R6Class
13 +
#' @importFrom stats predict
14 +
#'
15 +
#' @export
16 +
#'
17 +
#' @keywords data
18 +
#'
19 +
#' @return Learner object with methods for training and prediction. See
20 +
#'  \code{\link{Lrnr_base}} for documentation on learners.
21 +
#'
22 +
#' @format \code{\link{R6Class}} object.
23 +
#'
24 +
#' @family Learners
25 +
#'
26 +
#' @section Parameters:
27 +
#'   - \code{num_threads = 1L}: Number of threads for hyperthreading.
28 +
#'   - \code{...}: Other arguments passed to \code{\link[lightgbm]{lgb.train}}.
29 +
#'       See its documentation for further details.
30 +
#'
31 +
Lrnr_lightgbm <- R6Class(
32 +
  classname = "Lrnr_lightgbm", inherit = Lrnr_base,
33 +
  portable = TRUE, class = TRUE,
34 +
  public = list(
35 +
    initialize = function(num_threads = 1L, ...) {
36 +
      params <- args_to_list()
37 +
      super$initialize(params = params, ...)
38 +
    },
39 +
    importance = function(...) {
40 +
      self$assert_trained()
41 +
42 +
      # initiate argument list for lightgbm::lgb.importance
43 +
      args <- list(...)
44 +
      args$model <- self$fit_object
45 +
46 +
      # calculate importance metrics, already sorted by decreasing importance
47 +
      importance_result <- call_with_args(lightgbm::lgb.importance, args)
48 +
      rownames(importance_result) <- importance_result[["Feature"]]
49 +
      return(importance_result)
50 +
    }
51 +
  ),
52 +
  private = list(
53 +
    .properties = c(
54 +
      "continuous", "binomial", "categorical", "weights", "offset",
55 +
      "importance"
56 +
    ),
57 +
58 +
    .train = function(task) {
59 +
      args <- self$params
60 +
61 +
      verbose <- args$verbose
62 +
      if (is.null(verbose)) {
63 +
        verbose <- getOption("sl3.verbose")
64 +
      }
65 +
      args$verbose <- as.integer(verbose)
66 +
67 +
      # set up outcome
68 +
      outcome_type <- self$get_outcome_type(task)
69 +
      Y <- outcome_type$format(task$Y)
70 +
      if (outcome_type$type == "categorical") {
71 +
        # multiclass labels must start from zero
72 +
        Y <- as.numeric(Y) - 1L
73 +
      }
74 +
75 +
      # set up training data in custom format
76 +
      args$data <- try(lightgbm::lgb.Dataset(
77 +
        data = as.matrix(task$X),
78 +
        label = as.numeric(Y)
79 +
      ), silent = TRUE)
80 +
81 +
      # add observation-level weights if detected
82 +
      if (task$has_node("weights")) {
83 +
        try(lightgbm::setinfo(args$data, "weight", as.numeric(task$weights)),
84 +
            silent = TRUE
85 +
        )
86 +
      }
87 +
88 +
      # specify offset
89 +
      if (task$has_node("offset")) {
90 +
        # get outcome type and transform offset accordingly
91 +
        family <- outcome_type$glm_family(return_object = TRUE)
92 +
        link_fun <- args$family$linkfun
93 +
        offset <- as.numeric(task$offset_transformed(link_fun))
94 +
95 +
        # append offset to data
96 +
        try(lightgbm::setinfo(args$data, "init_score", offset), silent = TRUE)
97 +
      } else {
98 +
        link_fun <- NULL
99 +
      }
100 +
101 +
      # specify objective if it's NULL for fitting lightgbm
102 +
      if (is.null(args$objective)) {
103 +
        if (outcome_type$type == "continuous") {
104 +
          args$obj <- "regression"
105 +
        } else if (outcome_type$type == "binomial") {
106 +
          args$obj <- "binary"
107 +
          args$eval <- "binary_logloss"
108 +
        } else if (outcome_type$type == "quasibinomial") {
109 +
          args$obj <- "regression"
110 +
        } else if (outcome_type$type == "categorical") {
111 +
          args$obj <- "multiclass"
112 +
          args$eval <- "multi_error"
113 +
          args$num_class <- as.integer(length(outcome_type$levels))
114 +
        }
115 +
      }
116 +
117 +
      fit_object <- call_with_args(lightgbm::lgb.train, args, keep_all = TRUE)
118 +
      return(fit_object)
119 +
    },
120 +
121 +
    .predict = function(task = NULL) {
122 +
      fit_object <- private$.fit_object
123 +
124 +
      # set up test data for prediction (must be matrix or sparse matrix)
125 +
      Xmat <- as.matrix(task$X)
126 +
      if (is.integer(Xmat)) {
127 +
        Xmat[, 1] <- as.numeric(Xmat[, 1])
128 +
      }
129 +
130 +
      # order of columns has to be the same in xgboost training and test data
131 +
      train_name_ord <- match(names(private$.training_task$X), colnames(Xmat))
132 +
      Xmat_ord <- as.matrix(Xmat[, train_name_ord])
133 +
      if ((nrow(Xmat_ord) != nrow(Xmat)) & (ncol(Xmat_ord) == nrow(Xmat))) {
134 +
        Xmat_ord <- t(Xmat_ord)
135 +
      }
136 +
      stopifnot(nrow(Xmat_ord) == nrow(Xmat))
137 +
138 +
      # NOTE: cannot incorporate offset in prediction, at least for now
139 +
      # see https://github.com/microsoft/LightGBM/issues/1978
140 +
141 +
      # will generally return vector, needs to be put into data.table column
142 +
      predictions <- stats::predict(
143 +
        fit_object, Xmat_ord,
144 +
        reshape = TRUE
145 +
      )
146 +
147 +
      if (private$.training_outcome_type$type == "categorical") {
148 +
        # pack predictions in a single column
149 +
        predictions <- pack_predictions(predictions)
150 +
      }
151 +
      return(predictions)
152 +
    },
153 +
    .required_packages = c("lightgbm")
154 +
  )
155 +
)

@@ -113,8 +113,8 @@
Loading
113 113
          args$objective <- "reg:logistic"
114 114
        } else if (outcome_type$type == "categorical") {
115 115
          args$objective <- "multi:softprob"
116 -
          args$num_class <- length(outcome_type$levels)
117 116
          args$eval_metric <- "mlogloss"
117 +
          args$num_class <- as.integer(length(outcome_type$levels))
118 118
        }
119 119
      }
120 120
Files Coverage
R 75.72%
Project Totals (91 files) 75.72%
1
codecov:
2
  token: 0e172078-76a1-4b94-bd7f-2e10d0647b3d
3

4
comment: false
5

6
ignore:
7
  - "R/utils.R"
8

9
coverage:
10
  status:
11
    project:
12
      default:
13
        against: parent
14
        target: auto
15
        threshold: 1%
16
        branches:
17
          - master
18
        if_no_uploads: error
19
        if_not_found: success
20
        if_ci_failed: error
21
        only_pulls: false
22
        flags:
23
          - integration
24
        paths:
25
          - folder
26

27
    patch:
28
      default:
29
        against: parent
30
        target: 80%
31
        branches: null
32
        if_no_uploads: success
33
        if_not_found: success
34
        if_ci_failed: error
35
        only_pulls: false
36
        flags:
37
          - integration
38
        paths:
39
          - folder
40

41
    changes:
42
      default:
43
        against: parent
44
        branches: null
45
        if_no_uploads: error
46
        if_not_found: success
47
        if_ci_failed: error
48
        only_pulls: false
49
        flags:
50
          - integration
51
        paths:
52
          - folder
53

54
  flags:
55
    integration:
56
      branches:
57
        - master
58
      ignore:
59
        - app/ui
60

61
  ignore: # files and folders for processing
62
    - tests/*
63

64
  fixes:
65
    - "old_path::new_path"
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading