You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用R的caret训练随机森林遇调参错误:需含mtry列

问题:caret训练随机森林调参报错 Error: The tuning parameter grid should have columns mtry

报错信息:

Error: The tuning parameter grid should have columns mtry

用户代码:

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重新下载caret包、在expand.grid的参数前加.、仅给mtry加.前缀(如.mtry)、将mtry移至train函数中,但均出现相同错误。


错误原因与解决办法

错误原因

你构建的调参网格rfGRID混入了梯度提升树(gbm)的专属参数,比如interaction.depth、n.trees、shrinkage、n.minobsinnode,这些参数和随机森林(rf)完全不兼容。Caret中method="rf"对应的调参参数只有mtry,其他参数(如nodesize)属于模型的固定参数,不能放在tuneGrid里作为调参列。

当Caret解析你的调参网格时,大量无关参数干扰了它对当前模型(rf)对应调参列的识别,最终报错提示找不到mtry列。

解决步骤

  1. 重构正确的调参网格
    只保留随机森林对应的调参参数mtry,示例:

    rfGRID = expand.grid(mtry = c(1:10))
    

    如果需要设置nodesize等固定参数,直接在train函数中通过额外参数传入,不需要放在tuneGrid里:

    rfFit = train(response~., data = scoresWithResponse.trn,
                  importance = TRUE,
                  method = "rf",
                  metric="ROC",
                  trControl = ctrlCV,
                  tuneGrid = rfGRID,
                  classProbs = TRUE,
                  summaryFunction = twoClassSummary,
                  nodesize = 5  # 这里设置固定的nodesize值
    )
    
  2. 修正测试集AUC计算的笔误
    代码中ahaveroc是拼写错误,应该和训练集一样用auc(roc(...)):

    RF_loop_tst[i] = auc(roc(scoresWithResponse.tst$response,predict(rfFit,scoresWithResponse.tst, type='prob')[,1]))
    
  3. 可选:扩展固定参数设置
    随机森林的其他参数(如树的数量ntree)同样作为固定参数传入train函数即可:

    rfFit = train(response~., data = scoresWithResponse.trn,
                  importance = TRUE,
                  method = "rf",
                  metric="ROC",
                  trControl = ctrlCV,
                  tuneGrid = rfGRID,
                  classProbs = TRUE,
                  summaryFunction = twoClassSummary,
                  ntree = 200,  # 固定树的数量
                  nodesize = 5
    )
    

内容的提问来源于stack exchange,提问作者Programming Noob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 19:15:43