| predict.xgboost | R Documentation |
Predict values on data based on XGBoost model.
## S3 method for class 'xgboost'
predict(
object,
newdata,
type = "response",
base_margin = NULL,
iteration_range = NULL,
validate_features = TRUE,
...
)
object |
An XGBoost model object of class Note that there is also a lower-level |
newdata |
Data on which to compute predictions from the model passed in
In the case of data frames, if there are any categorical features, they should be of class
If there are named columns and the model was fitted to data with named columns, they will be
matched by name by default (see |
type |
Type of prediction to make. Supported options are:
|
base_margin |
Base margin used for boosting from existing model (raw score that gets added to all observations independently of the trees in the model). If supplied, should be either a vector with length equal to the number of rows in |
iteration_range |
Sequence of rounds/iterations from the model to use for prediction, specified by passing
a two-dimensional vector with the start and end numbers in the sequence (same format as R's For example, passing If passing If passing "all", will use all of the rounds regardless of whether the model had early stopping or not. Not applicable to |
validate_features |
Validate that the feature names in the data match to the feature names in the column, and reorder them in the data otherwise. If passing Be aware that this only applies to column names and not to factor levels in categorical columns. Note that this check might add some sizable latency to the predictions, so it's recommended to disable it for performance-sensitive applications. |
... |
Not used. |
Either a numeric vector (for 1D outputs), numeric matrix (for 2D outputs), numeric array
(for 3D and higher), or factor (for class predictions). See documentation for parameter type
for details about what the output type and shape will be.
data("ToothGrowth")
y <- ToothGrowth$supp
x <- ToothGrowth[, -2L]
model <- xgboost(x, y, nthreads = 1L, nrounds = 3L, max_depth = 2L)
pred_prob <- predict(model, x[1:5, ], type = "response")
pred_raw <- predict(model, x[1:5, ], type = "raw")
pred_class <- predict(model, x[1:5, ], type = "class")
# Relationships between these
manual_probs <- 1 / (1 + exp(-pred_raw))
manual_class <- ifelse(manual_probs < 0.5, levels(y)[1], levels(y)[2])
# They should match up to numerical precision
round(pred_prob, 6) == round(manual_probs, 6)
pred_class == manual_class
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.