-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[R-package] add a tree plotting function #6729
Open
fboudry
wants to merge
9
commits into
microsoft:master
Choose a base branch
from
fboudry:R-tree-plot
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6862821
Added lgb.plot.tree function.
fboudry 0a7ea0e
Added tests.
fboudry 5206b11
Merge branch 'microsoft:master' into R-tree-plot
fboudry 757dc84
Added review suggestions.
fboudry 55aba68
Updated tests. (based on R-package/tests/testthat/test_lgb.model.dt.t…
fboudry 85ff97a
Corrected error (missing comma) in the selected tree check (L66).
fboudry b4b648a
Corrected tests.
fboudry ed62441
Added DiagrammeR in CI, github and Azure workflows as well as in the …
fboudry 2710705
Update R-package/DESCRIPTION
jameslamb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Updated tests. (based on R-package/tests/testthat/test_lgb.model.dt.t…
…ree.R) Now tests regressions, binary, multiclass classification and ranks.
- Loading branch information
commit 55aba68c7fac9273fc9e125eac8faa0aef222c41
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,102 @@ | ||
test_that("lgb.plot.tree works as expected"){ | ||
data(agaricus.train, package = "lightgbm") | ||
train <- agaricus.train | ||
dtrain <- lgb.Dataset(train$data, label = train$label) | ||
# define model parameters and build a single tree | ||
model <- lgb.train( | ||
params = list( | ||
objective = "regression" | ||
, num_threads = .LGB_MAX_THREADS | ||
) | ||
, data = dtrain | ||
, nrounds = 1L | ||
, verbose = .LGB_VERBOSITY | ||
NROUNDS <- 10L | ||
MAX_DEPTH <- 3L | ||
N <- nrow(iris) | ||
X <- data.matrix(iris[2L:4L]) | ||
FEAT <- colnames(X) | ||
NCLASS <- nlevels(iris[, 5L]) | ||
|
||
model_reg <- lgb.train( | ||
params = list( | ||
objective = "regression" | ||
, num_threads = .LGB_MAX_THREADS | ||
, max.depth = MAX_DEPTH | ||
) | ||
# plot the tree and compare to the tree table | ||
# trees start from 0 in lgb.model.dt.tree | ||
tree_table <- lgb.model.dt.tree(model) | ||
expect_true({ | ||
lgb.plot.tree(model, 0) | ||
}, regexp = "lgb.plot.tree: Value of 'tree' should be between 1 and the total number of trees in the model") | ||
} | ||
, data = lgb.Dataset(X, label = iris[, 1L]) | ||
, verbose = .LGB_VERBOSITY | ||
, nrounds = NROUNDS | ||
) | ||
|
||
model_binary <- lgb.train( | ||
params = list( | ||
objective = "binary" | ||
, num_threads = .LGB_MAX_THREADS | ||
, max.depth = MAX_DEPTH | ||
) | ||
, data = lgb.Dataset(X, label = iris[, 5L] == "setosa") | ||
, verbose = .LGB_VERBOSITY | ||
, nrounds = NROUNDS | ||
) | ||
|
||
model_multiclass <- lgb.train( | ||
params = list( | ||
objective = "multiclass" | ||
, num_threads = .LGB_MAX_THREADS | ||
, max.depth = MAX_DEPTH | ||
, num_classes = NCLASS | ||
) | ||
, data = lgb.Dataset(X, label = as.integer(iris[, 5L]) - 1L) | ||
, verbose = .LGB_VERBOSITY | ||
, nrounds = NROUNDS | ||
) | ||
|
||
test_that("lgb.plot.tree fails when a non existing tree is selected"){ | ||
data(agaricus.train, package = "lightgbm") | ||
train <- agaricus.train | ||
dtrain <- lgb.Dataset(train$data, label = train$label) | ||
# define model parameters and build a single tree | ||
model <- lgb.train( | ||
params = list( | ||
objective = "regression" | ||
, num_threads = .LGB_MAX_THREADS | ||
) | ||
, data = dtrain | ||
, nrounds = 1L | ||
, verbose = .LGB_VERBOSITY | ||
model_rank <- lgb.train( | ||
params = list( | ||
objective = "lambdarank" | ||
, num_threads = .LGB_MAX_THREADS | ||
, max.depth = MAX_DEPTH | ||
, lambdarank_truncation_level = 3L | ||
) | ||
# plot the tree and compare to the tree table | ||
# trees start from 0 in lgb.model.dt.tree | ||
tree_table <- lgb.model.dt.tree(model) | ||
expect_error({ | ||
lgb.plot.tree(model, 999) | ||
}, regexp = "lgb.plot.tree: Value of 'tree' should be between 1 and the total number of trees in the model") | ||
, data = lgb.Dataset( | ||
X | ||
, label = as.integer(iris[, 1L] > 5.8) | ||
, group = rep(10L, times = 15L) | ||
) | ||
, verbose = .LGB_VERBOSITY | ||
, nrounds = NROUNDS | ||
) | ||
|
||
models <- list( | ||
reg = model_reg | ||
, bin = model_binary | ||
, multi = model_multiclass | ||
, rank = model_rank | ||
) | ||
|
||
for (model_name in names(models)){ | ||
model <- models[[model_name]] | ||
expected_n_trees <- NROUNDS | ||
if (model_name == "multi") { | ||
expected_n_trees <- NROUNDS * NCLASS | ||
} | ||
df <- as.data.frame(lgb.model.dt.tree(model)) | ||
df_list <- split(df, f = df$tree_index, drop = TRUE) | ||
df_leaf <- df[!is.na(df$leaf_index), ] | ||
df_internal <- df[is.na(df$leaf_index), ] | ||
|
||
test_that("lgb.plot.tree fails when a non existing tree is selected", { | ||
expect_error({ | ||
lgb.plot.tree(model, 0) | ||
}, regexp = "lgb.plot.tree: Value of 'tree' should be between 1 and the total number of trees in the model") | ||
}) | ||
test_that("lgb.plot.tree fails when a non existing tree is selected", { | ||
expect_error({ | ||
lgb.plot.tree(model, 999) | ||
}, regexp = "lgb.plot.tree: Value of 'tree' should be between 1 and the total number of trees in the model") | ||
}) | ||
test_that("lgb.plot.tree fails when a non numeric tree is selected", { | ||
expect_error({ | ||
lgb.plot.tree(model, "a") | ||
}, regexp = "lgb.plot.tree: Has to be an integer numeric") | ||
}) | ||
test_that("lgb.plot.tree fails when a non integer tree is selected", { | ||
expect_error({ | ||
lgb.plot.tree(model, 1.5) | ||
}, regexp = "lgb.plot.tree: Has to be an integer numeric") | ||
}) | ||
test_that("lgb.plot.tree fails when a non lgb.Booster model is passed", { | ||
expect_error({ | ||
lgb.plot.tree(1, 0) | ||
}, regexp = "lgb.plot.tree: model should be an 'lgb.Booster'") | ||
}) | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For every use of
expect_error()
here, please check for the specific error you are expecting, like this:https://github.com/microsoft/LightGBM/blob/83c0ff3de1925b0e2d4831a9ccb6ffc196aa795b/R-package/tests/testthat/test_lgb.importance.R#L33-35
That way, the test will be able to catch the case where some other unexpected issue causes this code path to fail.