Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
tianlh
LightGBM-DCU
Commits
8a4c5494
Commit
8a4c5494
authored
Jan 24, 2020
by
James Lamb
Committed by
Nikita Titov
Jan 24, 2020
Browse files
[R-package] added tests on lgb.plot.importance and clarified documentation (#2692)
parent
f2afb2cd
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
4 deletions
+45
-4
R-package/R/lgb.plot.importance.R
R-package/R/lgb.plot.importance.R
+4
-2
R-package/man/lgb.plot.importance.Rd
R-package/man/lgb.plot.importance.Rd
+4
-2
R-package/tests/testthat/test_lgb.plot.importance.R
R-package/tests/testthat/test_lgb.plot.importance.R
+37
-0
No files found.
R-package/R/lgb.plot.importance.R
View file @
8a4c5494
...
@@ -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, 10
L
)
#'
#'
#' 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")
...
...
R-package/man/lgb.plot.importance.Rd
View file @
8a4c5494
...
@@ -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, 10
L
)
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")
...
...
R-package/tests/testthat/test_lgb.plot.importance.R
0 → 100644
View file @
8a4c5494
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
())
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment