绘制mtry和n_tree值以及精度

时间:2019-05-13 12:27:33

标签: r plot random-forest

我必须在图形上用所有3个值绘制ntree和mtry超参数vlues以及精度,并找出这3个值的交集值以最终确定通过范围内的最佳值

for(i in seq(1,100))
{    
    ntried[i]<-i
    for (j in seq(1,10)) 
    {
    mtried[j]<-j    
    rf_mod1 <- randomForest(target~., data = heartdb, ntree = 
    ntried[i] , mtry= mtried[j] , importance= TRUE )

    t<-predict(rf_mod1,data = heartdb)
    p<-table(actual =heartdb$target, prediction = t )
    accuracy[i]<-sum(diag(p))/sum(p)

   }
}

1 个答案:

答案 0 :(得分:1)

按照书面规定,您的循环将不起作用,您将改为捕获每个都尝试了10棵树的随机森林。试试这个

unhanded exception has occurred: No Idp with entity id http://xxxxx not found

现在,每行精度包含10个精度。按行搜索最大值:

accuracy <- matrix(rep( 0, times = 1000), ncol = 10)
for(i in seq(1,100))
{    
    ntried <- i
    for (j in seq(1,10)) 
    {
    mtried <- j    
    rf_mod1[j] <- randomForest(target~., data = heartdb, ntree = 
    ntried , mtry= mtried , importance= TRUE )

    t<-predict(rf_mod1,data = heartdb)
    p<-table(actual =heartdb$target, prediction = t )
    accuracy[i,j]<-sum(diag(p))/sum(p)
   }

}

您甚至可以绘制apply(accuracy, 1, max) 的每个级别的最大值:

ntried

最后,您可以调用以下命令返回包含所有1000个林的最大精度的行(ntried级别):

plot(seq(1,100), apply(accuracy, 1, max))