【问题标题】:How to pick best ntree value from a caret grid search?如何从插入符号网格搜索中选择最佳 ntree 值?
【发布时间】:2020-07-30 22:14:58
【问题描述】:

我已手动调整参数以找到最佳 ntree:

bestMtry <- 3
control <- trainControl(method = 'repeatedcv',
                                number = 10,
                                repeats = 3,
                                search = 'grid')


storeMaxtrees <- list()
tuneGrid <- expand.grid(.mtry = bestMtry)
for (ntree in c(1000, 1500, 2000)) {
  set.seed(291)
  rf.maxtrees <- train(survived ~ .,
                       data = trainingSet,
                       method = "rf",
                       metric = "Accuracy",
                       tuneGrid = tuneGrid,
                       trControl = control,
                       importance = TRUE,
                       nodesize = 14,
                       maxnodes = 24,
                       ntree = ntree)
  key <- toString(ntree)
  storeMaxtrees[[key]] <- rf.maxtrees
}
resultsTree <- resamples(storeMaxtrees)
summary(resultsTree)

输出:

Call:
summary.resamples(object = resultsTree)

Models: 1000, 1500, 2000 
Number of resamples: 30 

Accuracy 
          Min.   1st Qu.    Median      Mean   3rd Qu.      Max. NA's
1000 0.7865169 0.8181818 0.8305031 0.8335064 0.8498787 0.8764045    0
1500 0.7865169 0.8181818 0.8305031 0.8319913 0.8522727 0.8764045    0
2000 0.7865169 0.8181818 0.8305031 0.8327446 0.8522727 0.8764045    0

Kappa 
          Min.   1st Qu.    Median      Mean   3rd Qu.      Max. NA's
1000 0.2700461 0.4243663 0.4786274 0.4753027 0.5252316 0.6281808    0
1500 0.2700461 0.4218811 0.4710053 0.4705338 0.5270828 0.6281808    0
2000 0.2700461 0.4218811 0.4786274 0.4721715 0.5270828 0.6281808    0

从输出中,我可以理解 2000 是基于 Accuracy 和 Kappa 的 ntree 的最佳值。我想动态存储 ntree (2000) 的最佳值。有没有类似best_ntree &lt;- resultsTree.bestTune 的方法?

【问题讨论】:

  • 我的插入符号知识已经生疏(好久没用了),但不是rf.maxtrees$finalModel$ntree
  • 可能是单行代码的最佳解决方案。但是,运行这条线后我得到了错误的值。 [1] 2000。不应该是1000吗?

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


【解决方案1】:

您可以存储来自 summary() 调用的结果,例如:

bestMtry <- 3
control <- trainControl(method = 'repeatedcv',number = 5)
data = MASS::Pima.tr                                

storeMaxtrees <- list()
tuneGrid <- expand.grid(.mtry = bestMtry)
for (ntree in c(1000, 1500, 2000)) {
  set.seed(291)
  rf.maxtrees <- train(type ~ .,
                       data = data,
                       method = "rf",
                       metric = "Accuracy",
                       tuneGrid = tuneGrid,
                       trControl = control,
                       importance = TRUE,
                       nodesize = 14,
                       maxnodes = 24,
                       ntree = ntree)
  key <- toString(ntree)
  storeMaxtrees[[key]] <- rf.maxtrees
}
resultsTree <- resamples(storeMaxtrees)

我们可以取平均准确率最高的那个:

res = summary(resultsTree)
res$models[which.max(res$statistics$Accuracy[,"Mean"])]
[1] "1500"

您可以将我示例中的 1500 转换为数字...

【讨论】:

  • res$models[which.max(res$statistics$Accuracy[,"Mean"])] i.imgur.com/7EPsp9E.png, i.imgur.com/zvWI9Zq.png 的任何想法都为 Null,为什么?
  • 嗨@user1896653,很抱歉我对尝试其他东西感到困惑,它应该是 res = summary(resultsTree)。查看更新的答案
  • 是的,它正在工作!只是一个困惑,参数的准确性是否有可能是最好的,但 Kappa 不是?我的意思是我们只在这里考虑“准确性”。会完全安全吗?
  • 通常准确率和 kappa 对于平衡数据集非常相似。当它不平衡时,例如 80:20,您不能使用 Accuracy,因为您预测所有都是多数,并且仍然具有 80% 的准确度。 Kappa 可能更合适。
  • 我在考虑什么时候准确度可能会更好......通常更多的是你需要的......所以它再次归结为班级的平衡,例如如果它像 60:40。 .但是您的总体目标是正确预测,然后准确性将满足您的需求
猜你喜欢
  • 1970-01-01
  • 2019-05-12
  • 1970-01-01
  • 1970-01-01
  • 2021-10-16
  • 2015-09-25
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
相关资源
最近更新 更多