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
8e077e0d
Unverified
Commit
8e077e0d
authored
Jul 20, 2020
by
James Lamb
Committed by
GitHub
Jul 20, 2020
Browse files
[R-package] add test on importance types (#3231)
* [R-package] add test on importance types * linting
parent
9f52282d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
0 deletions
+62
-0
R-package/tests/testthat/test_lgb.Booster.R
R-package/tests/testthat/test_lgb.Booster.R
+62
-0
No files found.
R-package/tests/testthat/test_lgb.Booster.R
View file @
8e077e0d
...
@@ -385,3 +385,65 @@ test_that("Booster$update() throws an informative error if you provide a non-Dat
...
@@ -385,3 +385,65 @@ test_that("Booster$update() throws an informative error if you provide a non-Dat
)
)
},
regexp
=
"lgb.Booster.update: Only can use lgb.Dataset"
,
fixed
=
TRUE
)
},
regexp
=
"lgb.Booster.update: Only can use lgb.Dataset"
,
fixed
=
TRUE
)
})
})
context
(
"save_model"
)
test_that
(
"Saving a model with different feature importance types works"
,
{
set.seed
(
708L
)
data
(
agaricus.train
,
package
=
"lightgbm"
)
data
(
agaricus.test
,
package
=
"lightgbm"
)
train
<-
agaricus.train
test
<-
agaricus.test
bst
<-
lightgbm
(
data
=
as.matrix
(
train
$
data
)
,
label
=
train
$
label
,
num_leaves
=
4L
,
learning_rate
=
1.0
,
nrounds
=
2L
,
objective
=
"binary"
,
save_name
=
tempfile
(
fileext
=
".model"
)
)
expect_true
(
lgb.is.Booster
(
bst
))
.feat_importance_from_string
<-
function
(
model_string
)
{
file_lines
<-
strsplit
(
model_string
,
"\n"
)[[
1L
]]
start_indx
<-
which
(
grepl
(
"^feature_importances\\:$"
,
file_lines
))
+
1L
blank_line_indices
<-
which
(
file_lines
==
""
)
end_indx
<-
blank_line_indices
[
blank_line_indices
>
start_indx
][
1L
]
-
1L
importances
<-
file_lines
[
start_indx
:
end_indx
]
return
(
importances
)
}
GAIN_IMPORTANCE
<-
1L
model_string
<-
bst
$
save_model_to_string
(
feature_importance_type
=
GAIN_IMPORTANCE
)
expect_equal
(
.feat_importance_from_string
(
model_string
)
,
c
(
"odor=none=4010"
,
"stalk-root=club=1163"
,
"stalk-root=rooted=573"
,
"stalk-surface-above-ring=silky=450"
,
"spore-print-color=green=397"
,
"gill-color=buff=281"
)
)
SPLIT_IMPORTANCE
<-
0L
model_string
<-
bst
$
save_model_to_string
(
feature_importance_type
=
SPLIT_IMPORTANCE
)
expect_equal
(
.feat_importance_from_string
(
model_string
)
,
c
(
"odor=none=1"
,
"gill-color=buff=1"
,
"stalk-root=club=1"
,
"stalk-root=rooted=1"
,
"stalk-surface-above-ring=silky=1"
,
"spore-print-color=green=1"
)
)
UNSUPPORTED_IMPORTANCE
<-
2L
expect_error
({
model_string
<-
bst
$
save_model_to_string
(
feature_importance_type
=
UNSUPPORTED_IMPORTANCE
)
},
"Unknown importance type"
)
})
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