You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#' @description The \code{lgb.plot.tree} function creates a DiagrammeR plot of a single LightGBM tree.
2
+
#' @title Plot LightGBM trees.
3
+
#' @description The \code{lgb.plot.tree} function creates a DiagrammeR plot of one or more LightGBM trees.
4
4
#' @param model a \code{lgb.Booster} object.
5
-
#' @param tree an integer specifying the tree to plot. This is 1-based, so e.g. a value of '7' means 'the 7th tree' (tree_index=6 in LightGBM's underlying representation).
5
+
#' @param tree An integer vector of tree indices that should be visualized IMPORTANT:
6
+
#' the tree index in lightgbm is zero-based, i.e. use tree = 0 for the first tree in a model.
6
7
#' @param rules a list of rules to replace the split values with feature levels.
8
+
#' @param render a logical flag for whether the graph should be rendered (see Value).
9
+
#' @param plot_width the width of the diagram in pixels.
10
+
#' @param plot_height the height of the diagram in pixels.
7
11
#'
8
12
#' @return
9
-
#' The \code{lgb.plot.tree} function creates a DiagrammeR plot.
13
+
#' When \code{render = TRUE}:
14
+
#' returns a rendered graph object which is an \code{htmlwidget} of class \code{grViz}.
15
+
#' Similar to ggplot objects, it needs to be printed to see it when not running from command line.
16
+
#'
17
+
#' When \code{render = FALSE}:
18
+
#' silently returns a graph object which is of DiagrammeR's class \code{dgr_graph}.
19
+
#' This could be useful if one wants to modify some of the graph attributes
20
+
#' before rendering the graph with \code{\link[DiagrammeR]{render_graph}}.
10
21
#'
11
22
#' @details
12
-
#' The \code{lgb.plot.tree} function creates a DiagrammeR plot of a single LightGBM tree. The tree is extracted from the model and displayed as a directed graph. The nodes are labelled with the feature, split value, gain, cover and value. The edges are labelled with the decision type and split value.
23
+
#' The \code{lgb.plot.tree} function creates a DiagrammeR plot of a single LightGBM tree.
24
+
#' The tree is extracted from the model and displayed as a directed graph.
25
+
#' The nodes are labelled with the feature, split value, gain, count and value.
26
+
#' The edges are labelled with the decision type and split value.
13
27
#'
14
28
#' @examples
15
29
#' \donttest{
16
-
#' # EXAMPLE: use the LightGBM example dataset to build a model with a single tree
stop("lgb.plot.tree: Has to be an integer numeric")
122
+
# tree must be integer or numeric
123
+
if (!inherits(tree, c('integer','numeric'))) {
124
+
stop(sprintf("lgb.plot.tree: 'tree' must only contain integers."))
57
125
}
58
-
# tree must be integer
59
-
if (tree%%1!=0) {
60
-
stop("lgb.plot.tree: Has to be an integer numeric")
126
+
#all elements of tree must be integers
127
+
if (!all(tree%%1L==0L)) {
128
+
stop(sprintf("lgb.plot.tree: 'tree' must only contain integers."))
61
129
}
62
130
# extract data.table model structure
63
131
modelDT<- lgb.model.dt.tree(model)
64
-
# check that tree is less than or equal to the maximum tree index in the model
65
-
if (tree> max(modelDT$tree_index) ||tree<1) {
66
-
warning("lgb.plot.tree: Value of 'tree' should be between 1 and the total number of trees in the model (", max(modelDT$tree_index), "). Got: ", tree, ".")
67
-
stop("lgb.plot.tree: Invalid tree number")
132
+
# check that all values of tree are greater than zero and less than or equal to the maximum tree index in the model
133
+
if (!all(tree>=0L&tree<= max(modelDT$tree_index))) {
134
+
stop(
135
+
"lgb.plot.tree: All values of 'tree' should be between 0 and the total number of trees in the model minus one ("
136
+
, max(modelDT$tree_index)
137
+
, ")."
138
+
)
68
139
}
69
-
# filter modelDT to just the rows for the selected tree
70
-
modelDT<-modelDT[tree_index==tree, ]
71
-
# change the column names to shorter more diagram friendly versions
140
+
# filter modelDT to just the rows for the selected trees
141
+
modelDT<-modelDT[tree_index%in%tree]
142
+
# change some column names to shorter and more diagram friendly versions
0 commit comments