Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
tune r包(tidymodels)中的tune_网格函数出错-错误:“parameters”对象具有必需的列_R_Tidyverse_Hyperparameters_Tidymodels - Fatal编程技术网

tune r包(tidymodels)中的tune_网格函数出错-错误:“parameters”对象具有必需的列

tune r包(tidymodels)中的tune_网格函数出错-错误:“parameters”对象具有必需的列,r,tidyverse,hyperparameters,tidymodels,R,Tidyverse,Hyperparameters,Tidymodels,这是关于堆栈溢出的第一个问题。我已经尽了最大努力使用reprex r包创建了一个像样的reprex。任何反馈都将不胜感激。下面是: 我相信错误在我的代码的最底层 我一直在尝试学习如何使用tidymodels r包套件,但在尝试使用tune_grid来调整树的数量和随机林的最小值时遇到了一个错误。我有点关注这里的博客帖子 据我所知,您可以使用工作流将配方和模型捆绑在一起,然后通过某种重采样(如cv)将其输入tune_grid函数以优化超参数。但我肯定是在什么地方出了问题,因为tune_grid函数

这是关于堆栈溢出的第一个问题。我已经尽了最大努力使用reprex r包创建了一个像样的reprex。任何反馈都将不胜感激。下面是:

我相信错误在我的代码的最底层

我一直在尝试学习如何使用tidymodels r包套件,但在尝试使用tune_grid来调整树的数量和随机林的最小值时遇到了一个错误。我有点关注这里的博客帖子

据我所知,您可以使用工作流将配方和模型捆绑在一起,然后通过某种重采样(如cv)将其输入tune_grid函数以优化超参数。但我肯定是在什么地方出了问题,因为tune_grid函数没有成功运行

这是我的密码:

#install.packages("pacman")
#install.packages("ranger")

pacman::p_load(tidyverse, # all the things
               tidymodels, workflows, tune # tidy ml
)



dat <- ggplot2::mpg %>% 
    mutate(
        trans2 = if_else(str_detect(trans, "auto"), "auto", "manual")
        ) %>% 
    select(-trans)

head(dat)
#> # A tibble: 6 x 11
#>   manufacturer model displ  year   cyl drv     cty   hwy fl    class   trans2
#>   <chr>        <chr> <dbl> <int> <int> <chr> <int> <int> <chr> <chr>   <chr> 
#> 1 audi         a4      1.8  1999     4 f        18    29 p     compact auto  
#> 2 audi         a4      1.8  1999     4 f        21    29 p     compact manual
#> 3 audi         a4      2    2008     4 f        20    31 p     compact manual
#> 4 audi         a4      2    2008     4 f        21    30 p     compact auto  
#> 5 audi         a4      2.8  1999     6 f        16    26 p     compact auto  
#> 6 audi         a4      2.8  1999     6 f        18    26 p     compact manual

dat_split <- initial_split(dat, prop = 3/4, strata = trans2)

dat_split
#> <Training/Validation/Total>
#> <176/58/234>

dat_train <- training(dat_split)

dat_cv <- vfold_cv(dat_train, strata = trans2)

dat_cv
#> #  10-fold cross-validation using stratification 
#> # A tibble: 10 x 2
#>    splits           id    
#>  * <named list>     <chr> 
#>  1 <split [158/18]> Fold01
#>  2 <split [158/18]> Fold02
#>  3 <split [158/18]> Fold03
#>  4 <split [158/18]> Fold04
#>  5 <split [158/18]> Fold05
#>  6 <split [158/18]> Fold06
#>  7 <split [158/18]> Fold07
#>  8 <split [158/18]> Fold08
#>  9 <split [160/16]> Fold09
#> 10 <split [160/16]> Fold10

dat_recipe <- recipe(trans2 ~ ., data = dat) %>% 
    step_normalize(all_numeric()) %>% 
    step_dummy(all_nominal())

dat_recipe
#> Data Recipe
#> 
#> Inputs:
#> 
#>       role #variables
#>    outcome          1
#>  predictor         10
#> 
#> Operations:
#> 
#> Centering and scaling for all_numeric
#> Dummy variables from all_nominal

rf_model <- rand_forest() %>% 
    set_args(mtry = 4, trees = tune(), min_n = tune()) %>% 
    set_engine("ranger") %>% 
    set_mode("classification")

rf_model
#> Random Forest Model Specification (classification)
#> 
#> Main Arguments:
#>   mtry = 4
#>   trees = tune()
#>   min_n = tune()
#> 
#> Computational engine: ranger

rf_workflow <- workflow() %>% 
    add_recipe(dat_recipe) %>% 
    add_model(rf_model)

rf_workflow
#> == Workflow ===========================================================================================================================
#> Preprocessor: Recipe
#> Model: rand_forest()
#> 
#> -- Preprocessor -----------------------------------------------------------------------------------------------------------------------
#> 2 Recipe Steps
#> 
#> * step_normalize()
#> * step_dummy()
#> 
#> -- Model ------------------------------------------------------------------------------------------------------------------------------
#> Random Forest Model Specification (classification)
#> 
#> Main Arguments:
#>   mtry = 4
#>   trees = tune()
#>   min_n = tune()
#> 
#> Computational engine: ranger

rf_grid <- rf_model %>%
    parameters() %>% 
    grid_max_entropy(size = 10)

rf_grid
#> # A tibble: 10 x 2
#>    trees min_n
#>    <int> <int>
#>  1  1014    12
#>  2   737    37
#>  3   339    22
#>  4  1728     2
#>  5  1951    30
#>  6  1673    18
#>  7     9    40
#>  8   966    26
#>  9   345     5
#> 10  1440    39

rf_tune_cv <- rf_workflow %>% 
    tune_grid(resamples = dat_cv,
              grid = rf_grid,
              metrics = metric_set(accuracy, roc_auc)
    )
#> x Fold01: model  1/10: Error: A `parameters` object has required columns.
#> Missing ...
#> x Fold01: model  2/10: Error: A `parameters` object has required columns.
#> Missing ...
#> x Fold01: model  3/10: Error: A `parameters` object has required columns.
#> Missing ...
#> x Fold01: model  4/10: Error: A `parameters` object has required columns.
#> Missing ...
#> x Fold01: model  5/10: Error: A `parameters` object has required columns.
#> Missing ...
#> x Fold01: model  6/10: Error: A `parameters` object has required columns.
#> Missing ...
#> x Fold01: model  7/10: Error: A `parameters` object has required columns.
#> Missing ...

您是否正在使用dplyr的开发版本,即dplyr 1.0?目前这首曲子还没有。我们将更新tune以修复此问题。同时,您可以重新安装到CRAN dplyr,所有这些都应该可以正常工作!我从CRAN重新安装了dplyr,不再有任何问题。这似乎是唯一的问题。非常感谢。仅供参考,旧dplyr有错误。一个是与结果相关的,不是一个因素,它是dat中的特征,但它也通过步骤_dummy被制成虚拟变量。您可能应该使用step_dummyall_nominal,-all_结果。