在 R 中使用 tidymodel 框架调整随机森林超参数时出错

error in random forest hyperparam tuning using tidymodel framework in R

我正在尝试使用 tidymodels 框架为随机森林回归问题找到正确的参数。

下面是我的代码:

#create recepie on the preped house train data
rf_rec <-
  recipe(log_sale_price ~. , data = house_train_treebased)

#give model spec
rf_mod <-
  rand_forest(mtry = tune(), num.trees = tune()) %>%
  set_engine("ranger")

#create Search grid
rf_grid <- expand.grid(mtry = c(1:30), num.trees = seq(from = 500, to = 1000, by = 100))

#create samples for cross validation
folds <- vfold_cv(house_train_treebased, v = 25)

#create models with grid search
rf_res <-
  tune_grid(rf_rec, model = rf_mod, resamples = folds ,  grid = rf_grid)

我收到以下错误:

> rf_mod <-
+   rand_forest(mtry = tune(), num.trees = tune()) %>%
+   set_engine("ranger")
Error in rand_forest(mtry = tune(), num.trees = tune()) : 
  unused argument (num.trees = tune())
rf_res <-
+   tune_grid(rf_rec, model = rf_mod, resamples = folds ,  grid = rf_grid)
Error: Internal error: `check_installs()` should have caught an `unknown` mode.

我错过了什么?

假设您正在使用 parsnip 包,函数调用 rand_forest 有参数 trees,但您指定的参数 num.trees 无法识别。尝试将 num.trees = tune() 替换为 trees = tune().

我看了下面的githublink

https://rdrr.io/github/tidymodels/tune/src/R/checks.R

check_metrics <- function(x, object) {
  mode <- workflows::pull_workflow_spec(object)$mode

  if (is.null(x)) {
    switch(
      mode,
      regression = {
        x <- yardstick::metric_set(rmse, rsq)
      },
      classification = {
        x <- yardstick::metric_set(roc_auc, accuracy)
      },
      unknown = {
        rlang::abort("Internal error: `check_installs()` should have caught an `unknown` mode.")
      },
      rlang::abort("Unknown `mode` for parsnip model.")
    )

    return(x)
  }

我没有提供模式,即如果我想要回归或分类。