【问题标题】:ploting the mtry and n_tree values along with the accuracy绘制 mtry 和 n_tree 值以及准确性
【发布时间】:2019-09-30 09:56:33
【问题描述】:

我必须用所有 3 个值在图上绘制 ntree 和 mtry 超参数值以及精度,并找出这 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)

   }
}

【问题讨论】:

  • 为什么需要绘制它?你想最大限度地提高准确性,对吧? which(accuracy == max(accuracy))
  • 是的,我想最大化 mtry 和 ntree 的已知组合加上可视化图形太适合探索
  • 我们没有你的数据,也不知道你正在使用什么包(有几个包有函数randomForest)来运行你的代码,我们看不到任何输出,所以我们所能做的就是猜测,直到有一个完整的例子。请重读How to Ask

标签: r plot random-forest


【解决方案1】:

正如您所写的那样,您的循环将不起作用,您将改为捕获随机森林,每个森林都尝试了 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)
   }

}

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

apply(accuracy, 1, max) 

您甚至可以绘制ntried 的每个级别的最大值:

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

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

which(apply(accuracy, 1, max) == max(apply(accuracy, 1, max)))

【讨论】:

  • 我只希望列索引为所有第 1000 个行索引的最大值
  • a13
  • 我明白了。是的,您可以执行apply(accuracy, 1, function(x) which(x == max(x))) 来返回一个向量,其中包含行i 的最大值的列索引到nrow(accuracy)
猜你喜欢
  • 2021-12-12
  • 2022-10-05
  • 2017-06-13
  • 2012-04-17
  • 1970-01-01
  • 2020-10-12
  • 1970-01-01
  • 2016-08-22
  • 1970-01-01
相关资源
最近更新 更多