Debian Codegraph @

man/predict.xgboost.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/xgboost.R
3 \name{predict.xgboost}
4 \alias{predict.xgboost}
5 \title{Compute predictions from XGBoost model on new data}
6 \usage{
7 \method{predict}{xgboost}(
8 object,
9 newdata,
10 type = "response",
11 base_margin = NULL,
12 iteration_range = NULL,
13 validate_features = TRUE,
14 ...
15 )
16 }
17 \arguments{
18 \item{object}{An XGBoost model object of class \code{xgboost}, as produced by function \code{\link[=xgboost]{xgboost()}}.
19
20 Note that there is also a lower-level \code{\link[=predict.xgb.Booster]{predict.xgb.Booster()}} method for models of class
21 \code{xgb.Booster} as produced by \code{\link[=xgb.train]{xgb.train()}}, which can also be used for \code{xgboost} class models as
22 an alternative that performs fewer validations and post-processings.}
23
24 \item{newdata}{Data on which to compute predictions from the model passed in \code{object}. Supported
25 input classes are:
26 \itemize{
27 \item Data Frames (class \code{data.frame} from base R and subclasses like \code{data.table}).
28 \item Matrices (class \code{matrix} from base R).
29 \item Sparse matrices from package \code{Matrix}, either as class \code{dgRMatrix} (CSR) or \code{dgCMatrix} (CSC).
30 \item Sparse vectors from package \code{Matrix}, which will be interpreted as containing a single
31 observation.
32 }
33
34 In the case of data frames, if there are any categorical features, they should be of class
35 \code{factor} and should have the same levels as the \code{factor} columns of the data from which the model
36 was constructed. Any columns with type other than \code{factor} will be interpreted as numeric.
37
38 If there are named columns and the model was fitted to data with named columns, they will be
39 matched by name by default (see \code{validate_features}).}
40
41 \item{type}{Type of prediction to make. Supported options are:
42 \itemize{
43 \item \code{"response"}: will output model predictions on the scale of the response variable (e.g.
44 probabilities of belonging to the last class in the case of binary classification). Result will
45 be either a numeric vector with length matching to rows in \code{newdata}, or a numeric matrix with
46 shape \verb{[nrows(newdata), nscores]} (for objectives that produce more than one score per
47 observation such as multi-class classification or multi-quantile regression).
48 \item \code{"raw"}: will output the unprocessed boosting scores (e.g. log-odds in the case of objective
49 \code{binary:logistic}). Same output shape and type as for \code{"response"}.
50 \item \code{"class"}: will output the class with the highest predicted probability, returned as a \code{factor}
51 (only applicable to classification objectives) with length matching to rows in \code{newdata}.
52 \item \code{"leaf"}: will output the terminal node indices of each observation across each tree, as an
53 integer matrix of shape \verb{[nrows(newdata), ntrees]}, or as an integer array with an extra one or
54 two dimensions, up to \verb{[nrows(newdata), ntrees, nscores, n_parallel_trees]} for models that
55 produce more than one score per tree and/or which have more than one parallel tree (e.g.
56 random forests).
57
58 Only applicable to tree-based boosters (not \code{gblinear}).
59 \item \code{"contrib"}: will produce per-feature contribution estimates towards the model score for a
60 given observation, based on SHAP values. The contribution values are on the scale of
61 untransformed margin (e.g., for binary classification, the values are log-odds deviations from
62 the baseline).
63
64 Output will be a numeric matrix with shape \verb{[nrows, nfeatures+1]}, with the intercept being the
65 last feature, or a numeric array with shape \verb{[nrows, nscores, nfeatures+1]} if the model
66 produces more than one score per observation.
67 \item \code{"interaction"}: similar to \code{"contrib"}, but computing SHAP values of contributions of
68 interaction of each pair of features. Note that this operation might be rather expensive in
69 terms of compute and memory.
70
71 Since it quadratically depends on the number of features, it is recommended to perform
72 selection of the most important features first.
73
74 Output will be a numeric array of shape \verb{[nrows, nfeatures+1, nfeatures+1]}, or shape
75 \verb{[nrows, nscores, nfeatures+1, nfeatures+1]} (for objectives that produce more than one score
76 per observation).
77 }}
78
79 \item{base_margin}{Base margin used for boosting from existing model (raw score that gets added to
80 all observations independently of the trees in the model).
81
82 If supplied, should be either a vector with length equal to the number of rows in \code{newdata}
83 (for objectives which produces a single score per observation), or a matrix with number of
84 rows matching to the number rows in \code{newdata} and number of columns matching to the number
85 of scores estimated by the model (e.g. number of classes for multi-class classification).}
86
87 \item{iteration_range}{Sequence of rounds/iterations from the model to use for prediction, specified by passing
88 a two-dimensional vector with the start and end numbers in the sequence (same format as R's \code{seq} - i.e.
89 base-1 indexing, and inclusive of both ends).
90
91 For example, passing \code{c(1,20)} will predict using the first twenty iterations, while passing \code{c(1,1)} will
92 predict using only the first one.
93
94 If passing \code{NULL}, will either stop at the best iteration if the model used early stopping, or use all
95 of the iterations (rounds) otherwise.
96
97 If passing "all", will use all of the rounds regardless of whether the model had early stopping or not.
98
99 Not applicable to \code{gblinear} booster.}
100
101 \item{validate_features}{Validate that the feature names in the data match to the feature names
102 in the column, and reorder them in the data otherwise.
103
104 If passing \code{FALSE}, it is assumed that the feature names and types are the same,
105 and come in the same order as in the training data.
106
107 Be aware that this only applies to column names and not to factor levels in categorical columns.
108
109 Note that this check might add some sizable latency to the predictions, so it's
110 recommended to disable it for performance-sensitive applications.}
111
112 \item{...}{Not used.}
113 }
114 \value{
115 Either a numeric vector (for 1D outputs), numeric matrix (for 2D outputs), numeric array
116 (for 3D and higher), or \code{factor} (for class predictions). See documentation for parameter \code{type}
117 for details about what the output type and shape will be.
118 }
119 \description{
120 Predict values on data based on XGBoost model.
121 }
122 \examples{
123 data("ToothGrowth")
124 y <- ToothGrowth$supp
125 x <- ToothGrowth[, -2L]
126 model <- xgboost(x, y, nthreads = 1L, nrounds = 3L, max_depth = 2L)
127 pred_prob <- predict(model, x[1:5, ], type = "response")
128 pred_raw <- predict(model, x[1:5, ], type = "raw")
129 pred_class <- predict(model, x[1:5, ], type = "class")
130
131 # Relationships between these
132 manual_probs <- 1 / (1 + exp(-pred_raw))
133 manual_class <- ifelse(manual_probs < 0.5, levels(y)[1], levels(y)[2])
134
135 # They should match up to numerical precision
136 round(pred_prob, 6) == round(manual_probs, 6)
137 pred_class == manual_class
138 }