"src/git@developer.sourcefind.cn:tianlh/lightgbm-dcu.git" did not exist on "04d37f9b38cb851c5201081c2b8289eb0ecbca02"
Commit 8a4c5494 authored by James Lamb's avatar James Lamb Committed by Nikita Titov
Browse files

[R-package] added tests on lgb.plot.importance and clarified documentation (#2692)

parent f2afb2cd
...@@ -5,7 +5,9 @@ ...@@ -5,7 +5,9 @@
#' @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{\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.
#' #'
#' @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.
...@@ -29,7 +31,7 @@ ...@@ -29,7 +31,7 @@
#' , min_sum_hessian_in_leaf = 1.0 #' , min_sum_hessian_in_leaf = 1.0
#' ) #' )
#' #'
#' model <- lgb.train(params, dtrain, 10) #' model <- lgb.train(params, dtrain, 10L)
#' #'
#' tree_imp <- lgb.importance(model, percentage = TRUE) #' tree_imp <- lgb.importance(model, percentage = TRUE)
#' lgb.plot.importance(tree_imp, top_n = 10L, measure = "Gain") #' lgb.plot.importance(tree_imp, top_n = 10L, measure = "Gain")
......
...@@ -21,7 +21,9 @@ lgb.plot.importance( ...@@ -21,7 +21,9 @@ lgb.plot.importance(
\item{left_margin}{(base R barplot) allows to adjust the left margin size to fit feature names.} \item{left_margin}{(base R barplot) allows to adjust the left margin size to fit feature names.}
\item{cex}{(base R barplot) passed as \code{cex.names} parameter to \code{barplot}.} \item{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.}
} }
\value{ \value{
The \code{lgb.plot.importance} function creates a \code{barplot} The \code{lgb.plot.importance} function creates a \code{barplot}
...@@ -48,7 +50,7 @@ params <- list( ...@@ -48,7 +50,7 @@ params <- list(
, min_sum_hessian_in_leaf = 1.0 , min_sum_hessian_in_leaf = 1.0
) )
model <- lgb.train(params, dtrain, 10) model <- lgb.train(params, dtrain, 10L)
tree_imp <- lgb.importance(model, percentage = TRUE) tree_imp <- lgb.importance(model, percentage = TRUE)
lgb.plot.importance(tree_imp, top_n = 10L, measure = "Gain") lgb.plot.importance(tree_imp, top_n = 10L, measure = "Gain")
......
context("lgb.plot.importance()")
test_that("lgb.plot.importance() should run without error for well-formed inputs", {
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 = 63L
, max_depth = -1L
, min_data_in_leaf = 1L
, min_sum_hessian_in_leaf = 1.0
)
model <- lgb.train(params, dtrain, 10L)
tree_imp <- lgb.importance(model, percentage = TRUE)
# Check that there are no plots present before plotting
expect_null(dev.list())
resDT <- lgb.plot.importance(
tree_imp = tree_imp
, top_n = 10L
, measure = "Gain"
, cex = 0.75
)
# Check that lgb.plot.importance() returns the data.table of the plotted data
expect_true(data.table::is.data.table(resDT))
expect_named(resDT, c("Feature", "Gain", "Cover", "Frequency"))
# Check that a plot was produced
expect_false(is.null(dev.list()))
# remove all plots
dev.off()
expect_null(dev.list())
})
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