r-使用插入符号在随机林中显式设置ntree和mtry



我正试图将树的数量和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网格。

ntree不能是随机林的tuneGrid的一部分,只能是mtry(请参阅此处每个模型的调整参数的详细目录(;你只能通过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)

最新更新