我试图在使用Caret的Random Forest算法中显式地传入树的数量和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)
但这导致了一个错误,指出我有太多的超参数。这就是为什么我尝试创建一个1×1的网格。
回答:
ntree
不能作为Random Forest的tuneGrid
的一部分,只有mtry
可以(参见每个模型的调参参数详细目录这里);你只能通过train
传入它。反过来,既然你调整了mtry
,后者就不能是train
的一部分。
总的来说,这里正确的组合是:
repGrid <- expand.grid(.mtry=c(4)) # no ntreerfClassifierRep <- train(label~ ., data=overallDataset, method="rf", metric="Accuracy", trControl=controlRep, ntree=350, # no mtry tuneGrid = repGrid)