【问题标题】:Random Forest - how mtry is larger than total number of independent variable?随机森林 - mtry 如何大于自变量的总数?
【发布时间】:2020-06-12 23:14:15
【问题描述】:

1) 我尝试回归随机森林来训练包含 4 个自变量的 185 行。 2 个分类变量各有 3 个水平和 13 个水平。另外 2 个变量是数值连续变量。

我尝试了 10 倍交叉验证的 RF,重复 4 次。 (我没有缩放因变量,这就是 RMSE 如此之大的原因。)

我猜 mtry 大于 4 的原因是分类变量总共有 3+13= 16 个级别。但如果是这样,为什么它不包括数字变量编号?

185 samples
4 predictor

No pre-processing
Resampling: Cross-Validated (10 fold, repeated 4 times) 
Summary of sample sizes: 168, 165, 166, 167, 166, 167, ... 
Resampling results across tuning parameters:

  mtry  RMSE      Rsquared   MAE    
   2    16764183  0.7843863  9267902
   9     9451598  0.8615202  3977457
  16     9639984  0.8586409  3813891

RMSE was used to select the optimal model using the smallest value.
The final value used for the model was mtry = 9.

请帮助我理解 mtry。

2) 另外,每一折的样本量是 168,165,166,....,为什么样本量会发生变化?

sample sizes: 168, 165, 166, 167, 166, 167

非常感谢。

【问题讨论】:

    标签: r random-forest r-caret interpretation


    【解决方案1】:

    您是正确的,因为有 16 个变量可供采样,因此 mtry 的最大值为 16。

    插入符号选择的值基于两个参数,在训练中,有一个 tuneLength 选项,默认为 3:

    tuneLength = ifelse(trControl$method == "none", 1, 3)
    

    这意味着它测试三个值。对于randomForest,你有mtry,默认是:

    getModelInfo("rf")[[1]]$grid
    function(x, y, len = NULL, search = "grid"){
                        if(search == "grid") {
                          out <- data.frame(mtry = caret::var_seq(p = ncol(x), 
                                                                  classification = is.factor(y), 
                                                                  len = len))
                        } else {
                          out <- data.frame(mtry = unique(sample(1:ncol(x), size = len, replace = TRUE)))
                        }
                        out
                      }
    

    因为你有 16 列,所以它变成:

    var_seq(16,len=3)
    [1]  2  9 16
    

    您可以通过设置来测试您选择的 mtry:

    library(caret)
    trCtrl = trainControl(method="repeatedcv",repeats=4,number=10)
    # we test 2,4,6..16
    trg = data.frame(mtry=seq(2,16,by=2))
    # some random data for example
    df = data.frame(y=rnorm(200),x1 = sample(letters[1:13],200,replace=TRUE),
    x2=sample(LETTERS[1:3],200,replace=TRUE),x3=rpois(200,10),x4=runif(200))
    
    #fit
    mdl = train(y ~.,data=df,tuneGrid=trg,trControl =trCtrl)
    
    Random Forest 
    
    200 samples
      4 predictor
    
    No pre-processing
    Resampling: Cross-Validated (10 fold, repeated 4 times) 
    Summary of sample sizes: 180, 180, 180, 180, 180, 180, ... 
    Resampling results across tuning parameters:
    
      mtry  RMSE      Rsquared    MAE      
       2    1.120216  0.04448700  0.8978851
       4    1.157185  0.04424401  0.9275939
       6    1.172316  0.04902991  0.9371778
       8    1.186861  0.05276752  0.9485516
      10    1.193595  0.05490291  0.9543479
      12    1.200837  0.05608624  0.9574420
      14    1.205663  0.05374614  0.9621094
      16    1.210783  0.05537412  0.9665665
    
    RMSE was used to select the optimal model using the smallest value.
    The final value used for the model was mtry = 2.
    

    【讨论】:

      猜你喜欢
      • 2019-08-24
      • 2017-08-29
      • 1970-01-01
      • 1970-01-01
      • 2014-09-19
      • 2021-03-23
      • 2012-12-07
      • 2013-07-25
      • 2017-08-29
      相关资源
      最近更新 更多