0

我正在尝试在 Rstudio 中使用 XGBoost。我有很多因子变量,可以在数据描述中看到。

资料说明

我使用 step_string2factor 和 step_dummy 将所有因子变量转换为虚拟变量,以便 XGBoost 可以工作。然而,在我的最后一块中,我得到了错误:

x Fold1:预处理器 1/1,模型 1/10:xgboost::xgb.DMatrix(x, label = y, missing = NA) 中的错误:'data' 具有类 'character' 和长度 3666440。'data' 接受一个数...

奇怪的是,XGBoost 在使用 step_dummy 时在其他配方中工作,我不理解给定的错误,因为我的数据没有字符类。

xgb_recipe <- recipe(Conversion ~ .,
                        data = Data_train_balanced) %>%
              step_string2factor(all_nominal()) %>%
              step_dummy(Jaar, Aantal, Verzekering, Retentie, Correct_Emailadres_flag,
                         Klant_Email_Ja_Nee, Klant_Bellen_Ja_Nee, Klant_Mailen_Ja_Nee, Topsegment, Regio_Naam,
                         Month, Weeks_split, Max_Loss, one_hot = TRUE)
xgb_model_tune <- boost_tree(trees = tune(),
                             tree_depth = tune(),
                             learn_rate = tune(),
                             stop_iter = 200) %>%
                  set_mode("classification") %>%
                  set_engine("xgboost")
xgb_tune_wf <- workflow() %>%
  add_recipe(xgb_recipe) %>%
  add_model(xgb_model_tune)

xgb_tune_wf
class_metrics
# Create tuning grid
set.seed(0132)
xgb_grid <- expand.grid(trees = 250 * 1:10,
                        learn_rate = c(0.1, 0.01),
                        tree_depth = 1:5)
xgb_grid

# Perform the grid search based on our created cross-validation (cv_folds)
xgb_tune_res <- tune_grid(
  xgb_tune_wf,
  resamples = cv_folds,
  grid = xgb_grid,
  metrics = class_metrics
)
4

0 回答 0