简体   繁体   English

无法使用 Tidy 模型调整我的随机森林(带护林员)中的 mtry 参数 R

[英]Cannot tune the mtry parameter in my random forest (with ranger) using Tidy models R

I am trying to tune the parameters for a random forest model using tune() and the Tidy model environment in R. I am using ranger as the engine and this is a classification model, but I cannot tune the mtry parameter.我正在尝试使用 tune() 和 R 中的 Tidy model 环境调整随机森林 model 的参数。我使用 ranger 作为引擎,这是一个分类 model,但我无法调整 mtry 参数。

I tried:我试过:

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

set.seed(3)
grid <- grid_random(parameters(random_forest), size = 20)

It works perfectly fine for trees and and min_n parameters, but if I try to use mtry = tune(), then create a grid, then I get the following error message:它适用于树和 min_n 参数,但如果我尝试使用 mtry = tune(),然后创建网格,则会收到以下错误消息:

Error in grid_random() : : These arguments contains unknowns: mtry . See the grid_random() Error in : : These arguments contains unknowns: mtry . See the . See the finalize()` function. Traceback: . See the finalize()` function。回溯:

  1. grid_random(parameters(random_forest), size = 20) grid_random(参数(random_forest),大小= 20)
  2. grid_random.parameters(parameters(random_forest), size = 20) grid_random.parameters(参数(random_forest),大小= 20)
  3. make_random_grid(,,,params. size = size. original = original. . filter = {. { . filter . } . }) make_random_grid(,,,params.size = size.original = original..filter = {. {.filter.}.})
  4. validate_params(..., call = call) validate_params(..., call = call)
  5. rlang::abort(paste0("These arguments contains unknowns: ", paste0(" ", . bad_param, " ", collapse = ","), ". See the finalize() function."), . call = call) rlang::abort(paste0("这些 arguments 包含未知数:", paste0(" ", . bad_param, " ", collapse = ","), "。参见finalize() function。"), . call = call)
  6. signal_abort(cnd, .file)` signal_abort(cnd, .file)`

I have tried grid_random and grid_regular and different values for size or levels, but it will only work if I remove mtry as it doesn't to recognise it as a valid argument.我已经尝试过 grid_random 和 grid_regular 以及不同的大小或级别值,但只有在我删除 mtry 时它才会起作用,因为它不会将其识别为有效参数。

The values that the mtry hyperparameter of the model can take on depends on the training data . model 的mtry超参数可以取的值取决于训练数据 Before you give some training data to the parameters, it is not known what would be good values for mtry .在为参数提供一些训练数据之前,不知道什么是mtry的好值。 You can finalize() the parameters by passing in some of your training data:您可以通过传入一些训练数据来finalize()参数:

library(tidymodels)

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

set.seed(3)
extract_parameter_set_dials(rf_spec)
#> Collection of 3 parameters for tuning
#> 
#>  identifier  type    object
#>        mtry  mtry nparam[?]
#>       trees trees nparam[+]
#>       min_n min_n nparam[+]
#> 
#> Model parameters needing finalization:
#>    # Randomly Selected Predictors ('mtry')
#> 
#> See `?dials::finalize` or `?dials::update.parameters` for more information.

extract_parameter_set_dials(rf_spec) %>%
  finalize(mtcars)  ## imagine mtcars was your training data
#> Collection of 3 parameters for tuning
#> 
#>  identifier  type    object
#>        mtry  mtry nparam[+]
#>       trees trees nparam[+]
#>       min_n min_n nparam[+]

Created on 2023-02-01 with reprex v2.0.2创建于 2023-02-01,使用reprex v2.0.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM