Description Usage Arguments Value Examples
Fit and evaluate xgboost model with data.table as input data.
Model are trained (including all preprocessing steps) on train part and
evaluated on validation part according to split
indicator variable.
1 2 3 4 |
data |
data.table with all input data. |
target |
Target variable name (character). |
split |
Indicator variable with 1 corresponds to observations in validation dataset. |
preproc_fun |
Preprocessing function which takes data.table |
params |
1-row data.table with tunable hyperparameters. |
args |
List with parameters unchangeable during tuning. |
metrics |
Vector of metric functions names. |
return_val_preds |
If |
return_model_obj |
If |
train_on_all_data |
If |
... |
Other parameters for |
data.table with optimal number of iterations (if early stopping is used)
and all metrics calculated for validation part of the data. It also contains
predictions for validation data if return_val_preds = TRUE
and
model object if return_model_obj = TRUE
.
If train_on_all_data = TRUE
, only model object will be returned.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | # Input data
dt <- as.data.table(mtcars)
# data.table with resamples
splits <- resampleR::cv_base(dt, "hp")
# data.table with tunable model hyperparameters
xgb_grid <- CJ(
max_depth = c(6, 8),
eta = 0.025,
colsample_bytree = 0.9,
subsample = 0.8,
gamma = 0,
min_child_weight = c(3, 5),
alpha = 0,
lambda = 1
)
# Non-tunable parameters for xgboost
xgb_args <- list(
nrounds = 500,
early_stopping_rounds = 10,
booster = "gbtree",
eval_metric = "rmse",
objective = "reg:linear",
verbose = 0
)
# Dumb preprocessing function
# Real function will contain imputation, feature engineering etc.
# with all statistics computed on train folds and applied to validation fold
preproc_fun_example <- function(data) return(data[])
xgb_fit(data = dt,
target = "hp",
split = splits[, split_1],
preproc_fun = preproc_fun_example,
params = xgb_grid[1, ],
args = xgb_args,
metrics = c("rmse", "mae"))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.