【问题标题】:error in random forest hyperparam tuning using tidymodel framework in R使用 R 中的 tidymodel 框架进行随机森林超参数调整的错误
【发布时间】:2020-07-03 10:49:36
【问题描述】:

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

我错过了什么?

【问题讨论】:

    标签: r random-forest tidymodels


    【解决方案1】:

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

    【讨论】:

    • 谢谢迈克。这可以解决我的第一个错误。但是在网格中训练模型时仍然出现 seocnd 错误
    【解决方案2】:

    我在以下链接中查看了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)
      }
    

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

    【讨论】:

      猜你喜欢
      • 2019-05-01
      • 2021-03-23
      • 2021-06-09
      • 2016-05-11
      • 2021-11-27
      • 1970-01-01
      • 2018-03-11
      • 2023-04-09
      • 1970-01-01
      相关资源
      最近更新 更多