| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/xgb.importance.R |
| 3 |
\name{xgb.importance} |
| 4 |
\alias{xgb.importance} |
| 5 |
\title{Feature importance} |
| 6 |
\usage{ |
| 7 |
xgb.importance( |
| 8 |
model = NULL, |
| 9 |
feature_names = getinfo(model, "feature_name"), |
| 10 |
trees = NULL |
| 11 |
) |
| 12 |
} |
| 13 |
\arguments{ |
| 14 |
\item{model}{Object of class \code{xgb.Booster}.} |
| 15 |
|
| 16 |
\item{feature_names}{Character vector used to overwrite the feature names |
| 17 |
of the model. The default is \code{NULL} (use original feature names).} |
| 18 |
|
| 19 |
\item{trees}{An integer vector of (base-1) tree indices that should be included |
| 20 |
into the importance calculation (only for the "gbtree" booster). |
| 21 |
The default (\code{NULL}) parses all trees. |
| 22 |
It could be useful, e.g., in multiclass classification to get feature importances |
| 23 |
for each class separately.} |
| 24 |
} |
| 25 |
\value{ |
| 26 |
A \code{data.table} with the following columns: |
| 27 |
|
| 28 |
For a tree model: |
| 29 |
\itemize{ |
| 30 |
\item \code{Features}: Names of the features used in the model. |
| 31 |
\item \code{Gain}: Fractional contribution of each feature to the model based on |
| 32 |
the total gain of this feature's splits. Higher percentage means higher importance. |
| 33 |
\item \code{Cover}: Metric of the number of observation related to this feature. |
| 34 |
\item \code{Frequency}: Percentage of times a feature has been used in trees. |
| 35 |
} |
| 36 |
|
| 37 |
For a linear model: |
| 38 |
\itemize{ |
| 39 |
\item \code{Features}: Names of the features used in the model. |
| 40 |
\item \code{Weight}: Linear coefficient of this feature. |
| 41 |
\item \code{Class}: Class label (only for multiclass models). For objects of class \code{xgboost} (as |
| 42 |
produced by \code{\link[=xgboost]{xgboost()}}), it will be a \code{factor}, while for objects of class \code{xgb.Booster} |
| 43 |
(as produced by \code{\link[=xgb.train]{xgb.train()}}), it will be a zero-based integer vector. |
| 44 |
} |
| 45 |
|
| 46 |
If \code{feature_names} is not provided and \code{model} doesn't have \code{feature_names}, |
| 47 |
the index of the features will be used instead. Because the index is extracted from the model dump |
| 48 |
(based on C++ code), it starts at 0 (as in C/C++ or Python) instead of 1 (usual in R). |
| 49 |
} |
| 50 |
\description{ |
| 51 |
Creates a \code{data.table} of feature importances. |
| 52 |
} |
| 53 |
\details{ |
| 54 |
This function works for both linear and tree models. |
| 55 |
|
| 56 |
For linear models, the importance is the absolute magnitude of linear coefficients. |
| 57 |
To obtain a meaningful ranking by importance for linear models, the features need to |
| 58 |
be on the same scale (which is also recommended when using L1 or L2 regularization). |
| 59 |
} |
| 60 |
\examples{ |
| 61 |
# binary classification using "gbtree": |
| 62 |
data("ToothGrowth") |
| 63 |
x <- ToothGrowth[, c("len", "dose")] |
| 64 |
y <- ToothGrowth$supp |
| 65 |
model_tree_binary <- xgboost( |
| 66 |
x, y, |
| 67 |
nrounds = 5L, |
| 68 |
nthreads = 1L, |
| 69 |
booster = "gbtree", |
| 70 |
max_depth = 2L |
| 71 |
) |
| 72 |
xgb.importance(model_tree_binary) |
| 73 |
|
| 74 |
# binary classification using "gblinear": |
| 75 |
model_tree_linear <- xgboost( |
| 76 |
x, y, |
| 77 |
nrounds = 5L, |
| 78 |
nthreads = 1L, |
| 79 |
booster = "gblinear", |
| 80 |
learning_rate = 0.3 |
| 81 |
) |
| 82 |
xgb.importance(model_tree_linear) |
| 83 |
|
| 84 |
# multi-class classification using "gbtree": |
| 85 |
data("iris") |
| 86 |
x <- iris[, c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")] |
| 87 |
y <- iris$Species |
| 88 |
model_tree_multi <- xgboost( |
| 89 |
x, y, |
| 90 |
nrounds = 5L, |
| 91 |
nthreads = 1L, |
| 92 |
booster = "gbtree", |
| 93 |
max_depth = 3 |
| 94 |
) |
| 95 |
# all classes clumped together: |
| 96 |
xgb.importance(model_tree_multi) |
| 97 |
# inspect importances separately for each class: |
| 98 |
num_classes <- 3L |
| 99 |
nrounds <- 5L |
| 100 |
xgb.importance( |
| 101 |
model_tree_multi, trees = seq(from = 1, by = num_classes, length.out = nrounds) |
| 102 |
) |
| 103 |
xgb.importance( |
| 104 |
model_tree_multi, trees = seq(from = 2, by = num_classes, length.out = nrounds) |
| 105 |
) |
| 106 |
xgb.importance( |
| 107 |
model_tree_multi, trees = seq(from = 3, by = num_classes, length.out = nrounds) |
| 108 |
) |
| 109 |
|
| 110 |
# multi-class classification using "gblinear": |
| 111 |
model_linear_multi <- xgboost( |
| 112 |
x, y, |
| 113 |
nrounds = 5L, |
| 114 |
nthreads = 1L, |
| 115 |
booster = "gblinear", |
| 116 |
learning_rate = 0.2 |
| 117 |
) |
| 118 |
xgb.importance(model_linear_multi) |
| 119 |
} |