lgb.importance.R 2.06 KB
Newer Older
1
#' Compute feature importance in a model
2
#'
3
#' Creates a \code{data.table} of feature importances in a model.
4
#'
5
6
#' @param model object of class \code{lgb.Booster}.
#' @param percentage whether to show importance in relative percentage.
7
#'
8
#' @return
9
#'
10
11
12
13
14
15
16
#' For a tree model, a \code{data.table} with the following columns:
#' \itemize{
#'   \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.
#'   \item \code{Frequency} The number of times a feature splited in trees.
#' }
17
#'
18
#' @examples
19
20
#' library(lightgbm)
#' data(agaricus.train, package = "lightgbm")
21
22
23
#' train <- agaricus.train
#' dtrain <- lgb.Dataset(train$data, label = train$label)
#'
24
25
26
#' params <- list(
#'   objective = "binary"
#'   , learning_rate = 0.01
27
28
29
30
#'   , num_leaves = 63L
#'   , max_depth = -1L
#'   , min_data_in_leaf = 1L
#'   , min_sum_hessian_in_leaf = 1.0
31
#' )
32
#' model <- lgb.train(params, dtrain, 10L)
33
34
35
#'
#' tree_imp1 <- lgb.importance(model, percentage = TRUE)
#' tree_imp2 <- lgb.importance(model, percentage = FALSE)
36
#'
37
#' @importFrom data.table := setnames setorderv
38
39
#' @export
lgb.importance <- function(model, percentage = TRUE) {
40

41
  # Check if model is a lightgbm model
42
  if (!inherits(model, "lgb.Booster")) {
43
44
    stop("'model' has to be an object of class lgb.Booster")
  }
45

46
  # Setup importance
47
  tree_dt <- lgb.model.dt.tree(model)
48

49
  # Extract elements
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  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(
    tree_imp_dt
    , old = "split_feature"
    , new = "Feature"
  )

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

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

78
  # Return importance table
79
  return(tree_imp_dt)
80

81
}