【问题标题】:Run randomForest with many mtry values使用许多 mtry 值运行 randomForest
【发布时间】:2015-12-27 06:12:31
【问题描述】:

我是randomForest R 包的新用户。我想为 mtry =1,5,7 的迭代运行 randomForest 分类。

例如,我想运行mtry =1 100 次和mtry=2 100 次。输出应显示每次运行的袋外错误(mtry =1 为 100 个结果,mtry =2 为 100 个结果)。

我只能编写1次运行的代码,我不知道如何运行mtry的不同值的代码迭代。

rf  <-  randomForest(class_name  ~  .,  data=tr,ntree=1000, 
                     importance=TRUE, proximity=TRUE, mtry=2)

【问题讨论】:

    标签: r random-forest


    【解决方案1】:

    这是一种使用内置 iris 数据框的快速而肮脏的方法。下面的代码给出了每个mtry 值的最终OOB 错误率。

    mtry = 1:4
    oob = data.frame()
    
    # Loop over each value of mtry and store result in a data frame
    for (i in mtry) {
    
      rf1 <- randomForest(Species ~ ., data=iris, ntree=100, mtry=i) 
    
      result = data.frame(mtry=i, 
                          OOB=rf1[["err.rate"]][nrow(rf1[["err.rate"]]),"OOB"])
      oob = rbind(oob, result)
    } 
    
    oob
         mtry        OOB
    OOB     1 0.04666667
    OOB1    2 0.04000000
    OOB2    3 0.04000000
    OOB3    4 0.04000000
    

    要保留mtry 的每个值中的所有ntree OOB 错误,只需更改以下内容:

    OOB=rf1[["err.rate"]][nrow(rf1[["err.rate"]]),"OOB"])
    

    到这里:

    OOB=rf1[["err.rate"]][ ,"OOB"])
    

    【讨论】:

    • 感谢 eipi10。因为我想重复运行 mtry =1、2,3(100 次),所以每个 mtry 应该有 100 个结果。我将复制功能与您的代码结合使用,但它不起作用。请帮忙。
    • 我的答案的最后一部分显示了如何为 mtry 的每个值获取所有 100 个 OOB 值。您只需从 for 循环中的代码中删除 nrow(rf1[["err.rate"]]) 即可。
    猜你喜欢
    • 2019-11-17
    • 1970-01-01
    • 2012-05-17
    • 2020-08-19
    • 1970-01-01
    • 2011-08-06
    • 2023-02-13
    • 2015-06-23
    • 2023-03-06
    相关资源
    最近更新 更多