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

时间:2020-03-22 22:27:24

标签: r random-forest tidymodels

我正在尝试使用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.

我想念什么?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我在以下链接中查看了github

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)
  }

我没有提供模式,即是否需要回归或分类。