Closed smingerson closed 2 years ago
I also see this error when using parsnip::set_engine("xgboost", params = list(eval_metric = "aucpr"))
without setting the objective
argument. I came across this error after updating parsnip to 0.1.5 from 0.1.4, and tune::tune_grid
started failing. (tidymodels
and the other individual packages were also updated in that time i.e. {workflows}
, {tune}
).
this test is passing: https://github.com/tidymodels/parsnip/blob/cb086385a90227eacfce2f06ed58ff2d4e17bb29/tests/testthat/test_boost_tree_xgboost.R#L169
spec <-
boost_tree() %>%
set_engine("xgboost", objective = "reg:pseudohubererror") %>%
set_mode("regression")
xgb_fit <- spec %>% fit(mpg ~ ., data = mtcars)
but if objective
is not a string (I guess this is the reason for labelling this as a feature request instead of a bug) it fails like in OPs code. Additionally, if one adds anything else to set_engine
it fails with the same error -- are the ...
all added to the same vector?
library(xgboost)
library(parsnip)
library(workflows)
mod <- boost_tree("classification") %>%
set_engine(
"xgboost",
objective = "binary:logistic",
params = list(eval_metric = "aucpr") # <- added this and changed the data to be a classification problem
)
dt <- data.frame(
x = rnorm(15),
y = rnorm(15) + rnorm(15, 0, .05),
target = as.factor(rbinom(15, 1, 0.5))
)
wf <- workflow() %>%
add_model(mod) %>%
add_formula(target ~ x + y)
fitted <- fit(wf, data = dt)
predict(fitted, new_data = dt)
#> Error in switch(object$params$objective, `binary:logitraw` = stats::binomial()$linkinv(res), : EXPR must be a length 1 vector
Created on 2021-04-20 by the reprex package (v2.0.0)
To fix it I had to change my code to:
mod <- boost_tree("classification") %>%
set_engine(
"xgboost",
params = list(
eval_metric = "aucpr",
objective = "binary:logistic" # <- MUST be present
)
)
the objective must be explicitly declared if params
is used, otherwise object$params$objective
is NULL
. Not sure if this is expected behavior i.e. the default was dropped.
There are similar reports here as well.
Related to #774.
This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.
Currently, passing a custom objective function causes an error downstream when predicting. This happens in
xgb_pred()
when usingswitch()
off of the objective (usually a character string) to modify the output ofpredict.xgb.Booster()
.