【问题标题】:R - Caret - Using ROC instead of accuracy in model trainingR - Caret - 在模型训练中使用 ROC 而不是准确性
【发布时间】:2014-12-06 01:50:43
【问题描述】:

您好,我的名字是 Abhi,我正在使用插入符号来构建基于 gbm 树的模型。但是,我想使用 roc 作为我的指标,而不是准确性

这是我目前的代码

myTuneGrid <- expand.grid(n.trees = 500,interaction.depth = 11,shrinkage = 0.1)
fitControl <- trainControl(method = "repeatedcv", number = 7,repeats = 1, verboseIter = FALSE,returnResamp = "all",classProbs = TRUE)
myModel <- train(Cover_Type ~ .,data = modelData,method = "gbm",trControl = fitControl,tuneGrid = myTuneGrid,metric='roc')

但是,当我运行此代码时,我会收到警告

Warning message:
In train.default(x, y, weights = w, ...) :
The metric "roc" was not in the result set. Accuracy will be used instead.

如何强制我的模型使用 roc 而不是准确度。我在这里做错了什么?

【问题讨论】:

  • caret website 上有使用插入符号用于 gbm 模型的示例。乍一看,我怀疑您的警告消息是由于未将 twoClassSummary 指定为 trainControl 中的摘要函数,并且可能未将“roc”大写为“ROC”
  • 将我的 trainControl 更改为 trainControl(method = "repeatedcv", number = 7,metric = 'roc',summaryFunction=twoClassSummary,repeats = 1, verboseIter = FALSE,returnResamp = "all",classProbs =真的)但仍然没有运气
  • 您能否确认是否可以在不显示警告消息的情况下运行以下gist?它只不过是来自插入符号网站的演示,带有您的附加网格和匹配参数。最好检查一下是否安装了“pROC”包。
  • 我的最终变量有 7 个类而不是 2 个。当我用 multiClassSummary 替换 twoClassSummary 时,代码运行良好。我在线获得了 multiClassSummary 的代码
  • 很高兴您解决了问题。你可以回答你自己的问题。请为其他感兴趣的用户提供链接。

标签: r r-caret gbm


【解决方案1】:

【讨论】:

    【解决方案2】:

    如果您在 trainControl 中指定 twoClassSummary() 并在您的代码中使用 metric="ROC"(而不是 method="roc"),它应该可以工作:

    df = iris
    df$Species =factor(ifelse(df$Species=="versicolor","v","o"))
    
    fitControl <- trainControl(method = "cv",returnResamp = "all",
    classProbs = TRUE,summaryFunction = twoClassSummary)
    
    myModel <- train(Species ~ .,data = df,method = "gbm",trControl = fitControl,metric='ROC')
    
    Stochastic Gradient Boosting 
    
    150 samples
      4 predictor
      2 classes: 'o', 'v' 
    
    No pre-processing
    Resampling: Cross-Validated (10 fold) 
    Summary of sample sizes: 135, 135, 135, 135, 135, 135, ... 
    Resampling results across tuning parameters:
    
      interaction.depth  n.trees  ROC    Sens  Spec
      1                   50      0.988  0.98  0.92
      1                  100      0.980  0.97  0.94
      1                  150      0.972  0.96  0.94
      2                   50      0.984  0.97  0.94
      2                  100      0.976  0.96  0.92
      2                  150      0.960  0.97  0.92
      3                   50      0.984  0.97  0.94
      3                  100      0.968  0.98  0.92
      3                  150      0.968  0.96  0.92
    
    Tuning parameter 'shrinkage' was held constant at a value of 0.1
    
    Tuning parameter 'n.minobsinnode' was held constant at a value of 10
    ROC was used to select the optimal model using the largest value.
    The final values used for the model were n.trees = 50, interaction.depth =
     1, shrinkage = 0.1 and n.minobsinnode = 10.
    

    【讨论】:

      猜你喜欢
      • 2016-03-09
      • 1970-01-01
      • 2020-11-25
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      • 2018-06-25
      • 2017-10-09
      • 1970-01-01
      相关资源
      最近更新 更多