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
1bf7bbd0
"...AutoBuildImmortalWrt.git" did not exist on "e4196e950c1f037bb3eea88b73ba276f00c39830"
Commit
1bf7bbd0
authored
Mar 01, 2017
by
Yachen Yan
Committed by
Guolin Ke
Mar 01, 2017
Browse files
Add Feature Importance Plot Function (#328)
parent
8e190f59
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
99 additions
and
0 deletions
+99
-0
R-package/NAMESPACE
R-package/NAMESPACE
+1
-0
R-package/R/lgb.plot.importance.R
R-package/R/lgb.plot.importance.R
+50
-0
R-package/man/lgb.plot.importance.Rd
R-package/man/lgb.plot.importance.Rd
+48
-0
No files found.
R-package/NAMESPACE
View file @
1bf7bbd0
...
...
@@ -20,6 +20,7 @@ export(lgb.importance)
export(lgb.interprete)
export(lgb.load)
export(lgb.model.dt.tree)
export(lgb.plot.importance)
export(lgb.save)
export(lgb.train)
export(lightgbm)
...
...
R-package/R/lgb.plot.importance.R
0 → 100644
View file @
1bf7bbd0
#' Plot feature importance 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 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}.
#'
#' @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.
#'
#' @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.
#'
#' @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)
#' model <- lgb.train(params, dtrain, 20)
#'
#' tree_imp <- lgb.importance(model, percentage = TRUE)
#' lgb.plot.importance(tree_imp, top_n = 10, measure = "Gain")
#'
#' @export
lgb.plot.importance
<-
function
(
tree_imp
,
top_n
=
10
,
measure
=
"Gain"
,
left_margin
=
10
,
cex
=
NULL
)
{
measure
<-
match.arg
(
measure
,
choices
=
c
(
"Gain"
,
"Cover"
,
"Frequency"
),
several.ok
=
FALSE
)
top_n
<-
min
(
top_n
,
nrow
(
tree_imp
))
tree_imp
<-
tree_imp
[
order
(
abs
(
get
(
measure
)),
decreasing
=
TRUE
),][
1
:
top_n
,]
if
(
is.null
(
cex
))
{
cex
<-
2.5
/
log2
(
1
+
top_n
)
}
op
<-
par
(
no.readonly
=
TRUE
)
on.exit
(
par
(
op
))
par
(
mar
=
op
$
mar
%>%
magrittr
::
inset
(
.
,
2
,
left_margin
))
tree_imp
[
.N
:
1
,
barplot
(
height
=
get
(
measure
),
names.arg
=
Feature
,
horiz
=
TRUE
,
main
=
"Feature Importance"
,
xlab
=
measure
,
cex.names
=
cex
,
las
=
1
)]
invisible
(
tree_imp
)
}
R-package/man/lgb.plot.importance.Rd
0 → 100644
View file @
1bf7bbd0
%
Generated
by
roxygen2
:
do
not
edit
by
hand
%
Please
edit
documentation
in
R
/
lgb
.
plot
.
importance
.
R
\
name
{
lgb
.
plot
.
importance
}
\
alias
{
lgb
.
plot
.
importance
}
\
title
{
Plot
feature
importance
as
a
bar
graph
}
\
usage
{
lgb
.
plot
.
importance
(
tree_imp
,
top_n
=
10
,
measure
=
"Gain"
,
left_margin
=
10
,
cex
=
NULL
)
}
\
arguments
{
\
item
{
tree_imp
}{
a
\
code
{
data
.
table
}
returned
by
\
code
{\
link
{
lgb
.
importance
}}.}
\
item
{
top_n
}{
maximal
number
of
top
features
to
include
into
the
plot
.}
\
item
{
measure
}{
the
name
of
importance
measure
to
plot
,
can
be
"Gain"
,
"Cover"
or
"Frequency"
.}
\
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
}.}
}
\
value
{
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
.
}
\
description
{
Plot
previously
calculated
feature
importance
:
Gain
,
Cover
and
Frequency
,
as
a
bar
graph
.
}
\
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
.
}
\
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
)
model
<-
lgb
.
train
(
params
,
dtrain
,
20
)
tree_imp
<-
lgb
.
importance
(
model
,
percentage
=
TRUE
)
lgb
.
plot
.
importance
(
tree_imp
,
top_n
=
10
,
measure
=
"Gain"
)
}
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