Closed coforfe closed 8 years ago
According to modelLookup("xgbTree")
there seems to be support for passing just these parameters to xgb from caret:
nrounds
max_depth
eta
gamma
colsample_bytree
min_child_weight
objective
and eval_metric
you're trying to pass it are probably not supported.
Also, I believe the metric
that you may custom define in caret is just used by caret to select the best model and again is not passed to xgb as an objective for training purposes.
xgb must be using its default values compatible with the parameters it does receive
It seems there may be a way to do what you're asking as described here: http://topepo.github.io/caret/custom_models.html
Thanks for your reply, but I think that is not fully accurate.
verbose=FALSE
which is a specific gbm paramerter:set.seed(825)
gbmFit1 <- train(Class ~ ., data = training,
method = "gbm",
trControl = fitControl,
## This last option is actually one
## for gbm() that passes through
verbose = FALSE)
gbmFit1
I have already used this feature to perform a more model fine-tuning with parameters that are not in the training for xgboost as well as other type of models: ranger, gbm, etc. But it's true that for a metric parameter perhaps I am mistaken.
Thanks, Carlos.
Sorry for the late response...
While the three dots do point to the xgb.train
call, we automatically set objective
to be either "binary:logistic
", "multi:softprob
", or "reg:linear
" depending on the case. I would expect that passing eval_metric
to train
will send it to xgb.train
without issue.
For now, you can make a copy of the model code using getModelInfo
and change objective
using a custom method.
For some models (including gbm
), we make these default choices but let the user override the values. The code is a little more complex but it is doable so I'll add that to the "to do" list. This is a better long-term solution.
Max
Thanks again Max for the clarifications and the references.
Carlos.
Hi Max,
I wonder if it is possible to set objective = "rank:pairwise"
in caret method: "xgbTree"? Or only binary:logistic
, multi:softprob
and reg:linear
can be passed?
Thanks!
Note the the link to "a custom method" was broken as of 2024. I believe the new link should go to "Chapter 13 Using Your Own Model in train" https://topepo.github.io/caret/using-your-own-model-in-train.html. Can @topepo confirm?
Would you consider changing the behavior of train(method = "xgbTree")
in https://github.com/topepo/caret/blob/master/models/files/xgbTree.R and other xgb models to set default objective
argument only when it is not specified in train()
?
To make the objective
argument effective in train()
for xgboost
models, I followed the minimal example at https://topepo.github.io/caret/using-your-own-model-in-train.html#Illustration6 and referenced the existing model info of xgbTree objects https://github.com/topepo/caret/blob/master/models/files/xgbTree.R.
xgb_custom <- getModelInfo("xgbTree", regex = FALSE)[[1]]
xgb_custom$fit <- function(x, y, wts, param, lev, last, classProbs, ...) {
if(!inherits(x, "xgb.DMatrix"))
x <- xgboost::xgb.DMatrix(x, label = y, missing = NA) else
xgboost::setinfo(x, "label", y)
if (!is.null(wts))
xgboost::setinfo(x, 'weight', wts)
out <- xgboost::xgb.train(
list(eta = param$eta,
max_depth = param$max_depth,
gamma = param$gamma,
colsample_bytree = param$colsample_bytree,
min_child_weight = param$min_child_weight,
subsample = param$subsample),
data = x,
nrounds = param$nrounds,
# objective = "reg:squarederror",
...)
out
}
Then I can use the newly defined method as in train(y ~ ., data = data, method = xgb_custom, objective = "reg:tweedie", eval_metric = "mae", base_score = median(data$y))
to use other objective functions.
Hello,
With the following example, I would like to understand if it is allowed to pass within
train()
extra parameters related to a newobjective
function and a neweval_metric
.Which produces this result:
Now, I define a new
objective
and aeval_metric
(both are parameters that can be defined forxgboost
), samexgbGrid
and samedataset
:Which produces an error:
This error could be related to the
objective
(??). Without that parameter there model runs and gets:Which basically yields the same
Accuracy
as the first version with noxgboost
metric.I know that in
trainControl()
you can define your ownmetric
. But, is it not possible to pass the metric already defined in the function (xgboost
in this case) with a parameter?.And regarding the error that appears when defined the
objective
, what could be its explanation ?.Thanks, Carlos.