Debian Codegraph @

man/xgb.plot.importance.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/xgb.ggplot.R, R/xgb.plot.importance.R
3 \name{xgb.ggplot.importance}
4 \alias{xgb.ggplot.importance}
5 \alias{xgb.plot.importance}
6 \title{Plot feature importance}
7 \usage{
8 xgb.ggplot.importance(
9 importance_matrix = NULL,
10 top_n = NULL,
11 measure = NULL,
12 rel_to_first = FALSE,
13 n_clusters = seq_len(10),
14 ...
15 )
16
17 xgb.plot.importance(
18 importance_matrix = NULL,
19 top_n = NULL,
20 measure = NULL,
21 rel_to_first = FALSE,
22 left_margin = 10,
23 cex = NULL,
24 plot = TRUE,
25 ...
26 )
27 }
28 \arguments{
29 \item{importance_matrix}{A \code{data.table} as returned by \code{\link[=xgb.importance]{xgb.importance()}}.}
30
31 \item{top_n}{Maximal number of top features to include into the plot.}
32
33 \item{measure}{The name of importance measure to plot.
34 When \code{NULL}, 'Gain' would be used for trees and 'Weight' would be used for gblinear.}
35
36 \item{rel_to_first}{Whether importance values should be represented as relative to
37 the highest ranked feature, see Details.}
38
39 \item{n_clusters}{A numeric vector containing the min and the max range
40 of the possible number of clusters of bars.}
41
42 \item{...}{Other parameters passed to \code{\link[graphics:barplot]{graphics::barplot()}}
43 (except \code{horiz}, \code{border}, \code{cex.names}, \code{names.arg}, and \code{las}).
44 Only used in \code{xgb.plot.importance()}.}
45
46 \item{left_margin}{Adjust the left margin size to fit feature names.
47 When \code{NULL}, the existing \code{par("mar")} is used.}
48
49 \item{cex}{Passed as \code{cex.names} parameter to \code{\link[graphics:barplot]{graphics::barplot()}}.}
50
51 \item{plot}{Should the barplot be shown? Default is \code{TRUE}.}
52 }
53 \value{
54 The return value depends on the function:
55 \itemize{
56 \item \code{xgb.plot.importance()}: Invisibly, a "data.table" with \code{n_top} features sorted
57 by importance. If \code{plot = TRUE}, the values are also plotted as barplot.
58 \item \code{xgb.ggplot.importance()}: A customizable "ggplot" object.
59 E.g., to change the title, set \code{+ ggtitle("A GRAPH NAME")}.
60 }
61 }
62 \description{
63 Represents previously calculated feature importance as a bar graph.
64 \itemize{
65 \item \code{xgb.plot.importance()} uses base R graphics, while
66 \item \code{xgb.ggplot.importance()} uses "ggplot".
67 }
68 }
69 \details{
70 The graph represents each feature as a horizontal bar of length proportional to the
71 importance of a feature. Features are sorted by decreasing importance.
72 It works for both "gblinear" and "gbtree" models.
73
74 When \code{rel_to_first = FALSE}, the values would be plotted as in \code{importance_matrix}.
75 For a "gbtree" model, that would mean being normalized to the total of 1
76 ("what is feature's importance contribution relative to the whole model?").
77 For linear models, \code{rel_to_first = FALSE} would show actual values of the coefficients.
78 Setting \code{rel_to_first = TRUE} allows to see the picture from the perspective of
79 "what is feature's importance contribution relative to the most important feature?"
80
81 The "ggplot" backend performs 1-D clustering of the importance values,
82 with bar colors corresponding to different clusters having similar importance values.
83 }
84 \examples{
85 data(agaricus.train)
86
87 ## Keep the number of threads to 2 for examples
88 nthread <- 2
89 data.table::setDTthreads(nthread)
90
91 model <- xgboost(
92 agaricus.train$data, factor(agaricus.train$label),
93 nrounds = 2,
94 max_depth = 3,
95 nthreads = nthread
96 )
97
98 importance_matrix <- xgb.importance(model)
99 xgb.plot.importance(
100 importance_matrix, rel_to_first = TRUE, xlab = "Relative importance"
101 )
102
103 gg <- xgb.ggplot.importance(
104 importance_matrix, measure = "Frequency", rel_to_first = TRUE
105 )
106 gg
107 gg + ggplot2::ylab("Frequency")
108
109 }
110 \seealso{
111 \code{\link[graphics:barplot]{graphics::barplot()}}
112 }