lgb.plot.importance.R 2.78 KB
Newer Older
1
#' Plot feature importance as a bar graph
2
#'
3
#' Plot previously calculated feature importance: Gain, Cover and Frequency, as a bar graph.
4
#'
5
6
7
8
9
#' @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.
#' @param cex (base R barplot) passed as \code{cex.names} parameter to \code{barplot}.
10
#'
11
12
13
#' @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.
14
#'
15
16
17
#' @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.
18
#'
19
#' @examples
20
21
22
23
24
25
26
#' data(agaricus.train, package = "lightgbm")
#' train <- agaricus.train
#' dtrain <- lgb.Dataset(train$data, label = train$label)
#'
#' 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
#' )
#'
33
#' model <- lgb.train(params, dtrain, 10)
34
35
#'
#' tree_imp <- lgb.importance(model, percentage = TRUE)
36
#' lgb.plot.importance(tree_imp, top_n = 10L, measure = "Gain")
James Lamb's avatar
James Lamb committed
37
#' @importFrom graphics barplot par
38
#' @export
39
lgb.plot.importance <- function(tree_imp,
40
                                top_n = 10L,
41
                                measure = "Gain",
42
43
44
                                left_margin = 10L,
                                cex = NULL
                                ) {
45

46
  # Check for measurement (column names) correctness
47
48
49
50
51
  measure <- match.arg(
    measure
    , choices = c("Gain", "Cover", "Frequency")
    , several.ok = FALSE
  )
52

53
  # Get top N importance (defaults to 10)
54
  top_n <- min(top_n, nrow(tree_imp))
55

56
  # Parse importance
57
  tree_imp <- tree_imp[order(abs(get(measure)), decreasing = TRUE), ][seq_len(top_n), ]
58

59
  # Attempt to setup a correct cex
60
  if (is.null(cex)) {
61
    cex <- 2.5 / log2(1.0 + top_n)
62
  }
63

64
  # Refresh plot
James Lamb's avatar
James Lamb committed
65
66
  op <- graphics::par(no.readonly = TRUE)
  on.exit(graphics::par(op))
67

68
69
  graphics::par(
    mar = c(
70
      op$mar[1L]
71
      , left_margin
72
73
      , op$mar[3L]
      , op$mar[4L]
74
75
    )
  )
76

77
  # Do plot
78
  tree_imp[.N:1L,
James Lamb's avatar
James Lamb committed
79
           graphics::barplot(
80
81
82
83
84
85
86
               height = get(measure)
               , names.arg = Feature
               , horiz = TRUE
               , border = NA
               , main = "Feature Importance"
               , xlab = measure
               , cex.names = cex
87
               , las = 1L
James Lamb's avatar
James Lamb committed
88
           )]
89

90
  # Return invisibly
91
  invisible(tree_imp)
92

93
}