【问题标题】:Setting ntree and mtry explicitly in Random Forest with caret使用插入符号在随机森林中显式设置 ntree 和 mtry
【发布时间】:2021-07-05 12:10:07
【问题描述】:

我正在尝试使用插入符号将树的数量和 mtry 显式传递给随机森林算法:

library(caret)
library(randomForest)
repGrid<-expand.grid(.mtry=c(4),.ntree=c(350))
controlRep <- trainControl(method="cv",number = 5)

rfClassifierRep <- train(label~ .,
                      data=overallDataset,
                      method="rf",
                      metric="Accuracy",
                      trControl=controlRep,
                      tuneGrid = repGrid,)

我收到此错误:

Error: The tuning parameter grid should have columns mtry

我先尝试了更明智的方法:

rfClassifierRep <- train(label~ .,
                      data=overallDataset,
                      method="rf",
                      metric="Accuracy",
                      trControl=controlRep,
                      ntree=350,
                      mtry=4,
                      tuneGrid = repGrid,)

但这导致了一个错误,指出我的超参数太多。这就是我尝试制作 1x1 网格的原因。

【问题讨论】:

    标签: r machine-learning random-forest r-caret


    【解决方案1】:

    ntree 不能是 Random Forest 的 tuneGrid 的一部分,只能是 mtry(请参阅每个模型的调整参数的详细目录 here);您只能通过train 传递它。反之,既然你调优了mtry,后者就不能成为train的一部分。

    总而言之,这里正确的组合是:

    repGrid <- expand.grid(.mtry=c(4))  # no ntree
    
    rfClassifierRep <- train(label~ .,
                          data=overallDataset,
                          method="rf",
                          metric="Accuracy",
                          trControl=controlRep,
                          ntree=350, 
                          # no mtry
                          tuneGrid = repGrid,)
    

    【讨论】:

      猜你喜欢
      • 2012-12-07
      • 2018-06-12
      • 2016-08-10
      • 2019-08-24
      • 2016-04-10
      • 2020-07-07
      • 2015-10-08
      • 2020-07-23
      • 2015-07-14
      相关资源
      最近更新 更多