lgb.plot.importance.R 3.1 KB
Newer Older
1
2
3
#' @name lgb.plot.importance
#' @title Plot feature importance as a bar graph
#' @description Plot previously calculated feature importance: Gain, Cover and Frequency, as a bar graph.
4
5
6
7
#' @param tree_imp a \code{data.table} returned by \code{\link{lgb.importance}}.
#' @param top_n maximal number of top features to include into the plot.
#' @param measure the name of importance measure to plot, can be "Gain", "Cover" or "Frequency".
#' @param left_margin (base R barplot) allows to adjust the left margin size to fit feature names.
8
9
10
#' @param cex (base R barplot) passed as \code{cex.names} parameter to \code{\link[graphics]{barplot}}.
#'            Set a number smaller than 1.0 to make the bar labels smaller than R's default and values
#'            greater than 1.0 to make them larger.
11
#'
12
13
14
#' @details
#' The graph represents each feature as a horizontal bar of length proportional to the defined importance of a feature.
#' Features are shown ranked in a decreasing importance order.
15
#'
16
17
18
#' @return
#' The \code{lgb.plot.importance} function creates a \code{barplot}
#' and silently returns a processed data.table with \code{top_n} features sorted by defined importance.
19
#'
20
#' @examples
21
#' \donttest{
22
23
#' \dontshow{setLGBMthreads(2L)}
#' \dontshow{data.table::setDTthreads(1L)}
24
25
26
27
28
29
#' data(agaricus.train, package = "lightgbm")
#' train <- agaricus.train
#' dtrain <- lgb.Dataset(train$data, label = train$label)
#'
#' params <- list(
#'     objective = "binary"
30
#'     , learning_rate = 0.1
31
32
#'     , min_data_in_leaf = 1L
#'     , min_sum_hessian_in_leaf = 1.0
33
#'     , num_threads = 2L
34
35
#' )
#'
36
37
38
39
40
#' model <- lgb.train(
#'     params = params
#'     , data = dtrain
#'     , nrounds = 5L
#' )
41
42
#'
#' tree_imp <- lgb.importance(model, percentage = TRUE)
43
#' lgb.plot.importance(tree_imp, top_n = 5L, measure = "Gain")
44
#' }
James Lamb's avatar
James Lamb committed
45
#' @importFrom graphics barplot par
46
#' @export
47
lgb.plot.importance <- function(tree_imp,
48
                                top_n = 10L,
49
                                measure = "Gain",
50
51
52
                                left_margin = 10L,
                                cex = NULL
                                ) {
53

54
  # Check for measurement (column names) correctness
55
56
57
58
59
  measure <- match.arg(
    measure
    , choices = c("Gain", "Cover", "Frequency")
    , several.ok = FALSE
  )
60

61
  # Get top N importance (defaults to 10)
62
  top_n <- min(top_n, nrow(tree_imp))
63

64
  # Parse importance
65
  tree_imp <- tree_imp[order(abs(get(measure)), decreasing = TRUE), ][seq_len(top_n), ]
66

67
  # Attempt to setup a correct cex
68
  if (is.null(cex)) {
69
    cex <- 2.5 / log2(1.0 + top_n)
70
  }
71

72
  # Refresh plot
James Lamb's avatar
James Lamb committed
73
74
  op <- graphics::par(no.readonly = TRUE)
  on.exit(graphics::par(op))
75

76
77
  graphics::par(
    mar = c(
78
      op$mar[1L]
79
      , left_margin
80
81
      , op$mar[3L]
      , op$mar[4L]
82
83
    )
  )
84

85
  tree_imp[rev(seq_len(.N)),
James Lamb's avatar
James Lamb committed
86
           graphics::barplot(
87
88
89
90
91
92
93
               height = get(measure)
               , names.arg = Feature
               , horiz = TRUE
               , border = NA
               , main = "Feature Importance"
               , xlab = measure
               , cex.names = cex
94
               , las = 1L
James Lamb's avatar
James Lamb committed
95
           )]
96

97
  return(invisible(tree_imp))
98

99
}