lgb.importance.R 2.25 KB
Newer Older
1
2
3
#' @name lgb.importance
#' @title Compute feature importance in a model
#' @description Creates a \code{data.table} of feature importances in a model.
4
5
#' @param model object of class \code{lgb.Booster}.
#' @param percentage whether to show importance in relative percentage.
6
#'
7
#' @return For a tree model, a \code{data.table} with the following columns:
8
#' \itemize{
9
10
11
#'   \item{\code{Feature}: Feature names in the model.}
#'   \item{\code{Gain}: The total gain of this feature's splits.}
#'   \item{\code{Cover}: The number of observation related to this feature.}
12
#'   \item{\code{Frequency}: The number of times a feature split in trees.}
13
#' }
14
#'
15
#' @examples
16
#' \donttest{
17
18
#' \dontshow{setLGBMthreads(2L)}
#' \dontshow{data.table::setDTthreads(1L)}
19
#' data(agaricus.train, package = "lightgbm")
20
21
22
#' train <- agaricus.train
#' dtrain <- lgb.Dataset(train$data, label = train$label)
#'
23
24
#' params <- list(
#'   objective = "binary"
25
#'   , learning_rate = 0.1
26
27
28
#'   , max_depth = -1L
#'   , min_data_in_leaf = 1L
#'   , min_sum_hessian_in_leaf = 1.0
29
#'   , num_threads = 2L
30
#' )
31
32
33
34
35
#' model <- lgb.train(
#'     params = params
#'     , data = dtrain
#'     , nrounds = 5L
#' )
36
37
38
#'
#' tree_imp1 <- lgb.importance(model, percentage = TRUE)
#' tree_imp2 <- lgb.importance(model, percentage = FALSE)
39
#' }
40
#' @importFrom data.table := setnames setorderv
41
42
#' @export
lgb.importance <- function(model, percentage = TRUE) {
43

44
  if (!.is_Booster(x = model)) {
45
46
    stop("'model' has to be an object of class lgb.Booster")
  }
47

48
  # Setup importance
49
  tree_dt <- lgb.model.dt.tree(model = model)
50

51
  # Extract elements
52
53
54
55
56
57
58
  tree_imp_dt <- tree_dt[
    !is.na(split_index)
    , .(Gain = sum(split_gain), Cover = sum(internal_count), Frequency = .N)
    , by = "split_feature"
  ]

  data.table::setnames(
59
    x = tree_imp_dt
60
61
62
63
64
65
66
    , old = "split_feature"
    , new = "Feature"
  )

  # Sort features by Gain
  data.table::setorderv(
    x = tree_imp_dt
67
68
    , cols = "Gain"
    , order = -1L
69
  )
70

71
  # Check if relative values are requested
72
  if (percentage) {
73
74
75
76
77
    tree_imp_dt[, `:=`(
      Gain = Gain / sum(Gain)
      , Cover = Cover / sum(Cover)
      , Frequency = Frequency / sum(Frequency)
    )]
78
  }
79

80
81
  # adding an empty [] to ensure the table is printed the first time print.data.table() is called
  return(tree_imp_dt[])
82

83
}