Unverified Commit 5442aa97 authored by James Lamb's avatar James Lamb Committed by GitHub
Browse files

[R-package] fixed examples in lgb.plot.importance (#1635)

parent 59702674
#' Plot feature importance as a bar graph #' Plot feature importance as a bar graph
#' #'
#' Plot previously calculated feature importance: Gain, Cover and Frequency, as a bar graph. #' Plot previously calculated feature importance: Gain, Cover and Frequency, as a bar graph.
#' #'
#' @param tree_imp a \code{data.table} returned by \code{\link{lgb.importance}}. #' @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 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 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 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}. #' @param cex (base R barplot) passed as \code{cex.names} parameter to \code{barplot}.
#' #'
#' @details #' @details
#' The graph represents each feature as a horizontal bar of length proportional to the defined importance of a feature. #' 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. #' Features are shown ranked in a decreasing importance order.
#' #'
#' @return #' @return
#' The \code{lgb.plot.importance} function creates a \code{barplot} #' 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. #' and silently returns a processed data.table with \code{top_n} features sorted by defined importance.
#' #'
#' @examples #' @examples
# data(agaricus.train, package = "lightgbm") #' data(agaricus.train, package = "lightgbm")
# train <- agaricus.train #' train <- agaricus.train
# dtrain <- lgb.Dataset(train$data, label = train$label) #' dtrain <- lgb.Dataset(train$data, label = train$label)
# #'
# params <- list( #' params <- list(
# objective = "binary" #' objective = "binary"
# , learning_rate = 0.01 #' , learning_rate = 0.01
# , num_leaves = 63 #' , num_leaves = 63
# , max_depth = -1 #' , max_depth = -1
# , min_data_in_leaf = 1 #' , min_data_in_leaf = 1
# , min_sum_hessian_in_leaf = 1 #' , min_sum_hessian_in_leaf = 1
# ) #' )
# #'
# model <- lgb.train(params, dtrain, 20) #' model <- lgb.train(params, dtrain, 20)
# #'
# tree_imp <- lgb.importance(model, percentage = TRUE) #' tree_imp <- lgb.importance(model, percentage = TRUE)
# lgb.plot.importance(tree_imp, top_n = 10, measure = "Gain") #' lgb.plot.importance(tree_imp, top_n = 10, measure = "Gain")
#' @importFrom graphics barplot par #' @importFrom graphics barplot par
#' @export #' @export
lgb.plot.importance <- function(tree_imp, lgb.plot.importance <- function(tree_imp,
...@@ -41,28 +41,28 @@ lgb.plot.importance <- function(tree_imp, ...@@ -41,28 +41,28 @@ lgb.plot.importance <- function(tree_imp,
measure = "Gain", measure = "Gain",
left_margin = 10, left_margin = 10,
cex = NULL) { cex = NULL) {
# Check for measurement (column names) correctness # Check for measurement (column names) correctness
measure <- match.arg(measure, choices = c("Gain", "Cover", "Frequency"), several.ok = FALSE) measure <- match.arg(measure, choices = c("Gain", "Cover", "Frequency"), several.ok = FALSE)
# Get top N importance (defaults to 10) # Get top N importance (defaults to 10)
top_n <- min(top_n, nrow(tree_imp)) top_n <- min(top_n, nrow(tree_imp))
# Parse importance # Parse importance
tree_imp <- tree_imp[order(abs(get(measure)), decreasing = TRUE),][seq_len(top_n),] tree_imp <- tree_imp[order(abs(get(measure)), decreasing = TRUE),][seq_len(top_n),]
# Attempt to setup a correct cex # Attempt to setup a correct cex
if (is.null(cex)) { if (is.null(cex)) {
cex <- 2.5 / log2(1 + top_n) cex <- 2.5 / log2(1 + top_n)
} }
# Refresh plot # Refresh plot
op <- graphics::par(no.readonly = TRUE) op <- graphics::par(no.readonly = TRUE)
on.exit(graphics::par(op)) on.exit(graphics::par(op))
# Do some magic plotting # Do some magic plotting
graphics::par(mar = op$mar %>% magrittr::inset(., 2, left_margin)) graphics::par(mar = op$mar %>% magrittr::inset(., 2, left_margin))
# Do plot # Do plot
tree_imp[.N:1, tree_imp[.N:1,
graphics::barplot( graphics::barplot(
...@@ -75,8 +75,8 @@ lgb.plot.importance <- function(tree_imp, ...@@ -75,8 +75,8 @@ lgb.plot.importance <- function(tree_imp,
cex.names = cex, cex.names = cex,
las = 1 las = 1
)] )]
# Return invisibly # Return invisibly
invisible(tree_imp) invisible(tree_imp)
} }
...@@ -29,3 +29,22 @@ Plot previously calculated feature importance: Gain, Cover and Frequency, as a b ...@@ -29,3 +29,22 @@ Plot previously calculated feature importance: Gain, Cover and Frequency, as a b
The graph represents each feature as a horizontal bar of length proportional to the defined importance of a feature. 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. Features are shown ranked in a decreasing importance order.
} }
\examples{
data(agaricus.train, package = "lightgbm")
train <- agaricus.train
dtrain <- lgb.Dataset(train$data, label = train$label)
params <- list(
objective = "binary"
, learning_rate = 0.01
, num_leaves = 63
, max_depth = -1
, min_data_in_leaf = 1
, min_sum_hessian_in_leaf = 1
)
model <- lgb.train(params, dtrain, 20)
tree_imp <- lgb.importance(model, percentage = TRUE)
lgb.plot.importance(tree_imp, top_n = 10, measure = "Gain")
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment