首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:调优参数网格应该有列mtry

错误:调优参数网格应该有列mtry
EN

Stack Overflow用户
提问于 2022-08-31 23:13:47
回答 1查看 85关注 0票数 2

我试图在R中使用caret来训练一个随机森林模型,我想通过使用expand.grid函数来调优参数以获得最佳值。但是,我一直收到这样的错误:

代码语言:javascript
复制
Error: The tuning parameter grid should have columns mtry

这是我的密码。我在这里使用的数据称为scoresWithResponse

代码语言:javascript
复制
ctrlCV = trainControl(method = 'cv', number = 10 , classProbs = TRUE , savePredictions = TRUE, summaryFunction = twoClassSummary )

rfGRID = expand.grid(interaction.depth = c(2, 3, 5, 6, 7, 8, 10),
                     n.trees = c(50,75,100,125,150,200,250), 
                     shrinkage = seq(.005, .2,.005),
                     n.minobsinnode = c(5,7,10, 12 ,15, 20),
                    nodesize = c(1:10),
                    mtry = c(1:10))

RF_loop_trn = c()
RF_loop_tst = c()

for(i in (1:5)){
  print(i)
  
  IND = createDataPartition(y = scoresWithResponse$response, p=0.75, list = FALSE)
  scoresWithResponse.trn = scoresWithResponse[IND, ]
  scoresWithResponse.tst = scoresWithResponse[-IND,]
  
  rfFit = train(response~., data = scoresWithResponse.trn,
                importance = TRUE,
                method = "rf",
                metric="ROC",
                trControl = ctrlCV,
                tuneGrid = rfGRID,
                classProbs = TRUE,
                summaryFunction = twoClassSummary
  )
  
  
  RF_loop_trn[i] = auc(roc(scoresWithResponse.trn$response,predict(rfFit,scoresWithResponse.trn, type='prob')[,1]))
  RF_loop_tst[i] = ahaveroc(scoresWithResponse.tst$response,predict(rfFit,scoresWithResponse.tst, type='prob')[,1]))
  
}

在调查了一段时间后,有一些建议,例如从github重新下载插入符号包,在expand.grid中的每个参数之前添加一个expand.grid,只在mtry参数之前添加一个点(类似于.mtry),将mtry添加到train函数中,而不是expand.grid。我试过所有这些,它们都会产生同样的错误。

我应该在哪里以及如何添加mtry参数?是什么导致了这个错误?

EN

回答 1

Stack Overflow用户

发布于 2022-09-01 11:01:46

我还不能发表评论,所以我会回复

您是否尝试将mtry参数直接插入到train函数中?例如:

代码语言:javascript
复制
rfGRID = expand.grid(interaction.depth = c(2, 3, 5, 6, 7, 8, 10),
                     n.trees = c(50,75,100,125,150,200,250), 
                     shrinkage = seq(.005, .2,.005),
                     n.minobsinnode = c(5,7,10, 12 ,15, 20),
                    nodesize = c(1:10)
                    )


rfFit = train(response~., data = scoresWithResponse.trn,
                importance = TRUE,
                method = "rf",
                metric="ROC",
                trControl = ctrlCV,
                tuneGrid = rfGRID,
                classProbs = TRUE,
                summaryFunction = twoClassSummary,
                mtry = 1000

  )
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73562782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档