代码之家  ›  专栏  ›  技术社区  ›  Chris

计算roc度量,插入符号包-r

  •  0
  • Chris  · 技术社区  · 6 年前

    我有这个密码:

    model_nn <- train(
      Y ~ ., training,
      method = "nnet",
      metric = "ROC",
      trControl = trainControl(
        method = "cv", 
        number = 10,
        verboseIter = TRUE,
        classProbs = TRUE,
        summaryFunction = twoClassSummary
      )
    )
    
    nnprediction <- predict(model_nn, testing)
    cmnn <-confusionMatrix(nnprediction,testing$Y)
    print(cmnn)
    

    这很管用。然而,我无法评估ROC度量性能与融合矩阵命令有多好。如何评估它,以尝试不同的变量和/或机器学习算法来改善ROC性能?

    附:因变量是两个类的因子。

    1 回复  |  直到 6 年前
        1
  •  1
  •   desertnaut SKZI    6 年前

    只是打字 model_nn 将为您提供培训期间使用的不同设置的AUC分数;下面是一个示例,使用 iris 数据(2类):

    library(caret)
    library(nnet)
    
    data(iris)
    iris_reduced <- iris[1:100,]
    iris_reduced <- droplevels(iris_reduced, "virginica")
    
    model_nn <- train(
      Species ~ ., iris_reduced,
      method = "nnet",
      metric = "ROC",
      trControl = trainControl(
        method = "cv", 
        number = 5,
        verboseIter = TRUE,
        classProbs = TRUE,
        summaryFunction = twoClassSummary
      )
    )
    
    model_nn
    

    结果:

    Neural Network 
    
    100 samples
      4 predictors
      2 classes: 'setosa', 'versicolor' 
    
    No pre-processing
    Resampling: Cross-Validated (5 fold) 
    Summary of sample sizes: 80, 80, 80, 80, 80 
    Resampling results across tuning parameters:
    
      size  decay  ROC  Sens  Spec
      1     0e+00  1.0  1.0   1   
      1     1e-04  0.8  0.8   1   
      1     1e-01  1.0  1.0   1   
      3     0e+00  1.0  1.0   1   
      3     1e-04  1.0  1.0   1   
      3     1e-01  1.0  1.0   1   
      5     0e+00  1.0  1.0   1   
      5     1e-04  1.0  1.0   1   
      5     1e-01  1.0  1.0   1   
    
    ROC was used to select the optimal model using  the largest value.
    The final values used for the model were size = 1 and decay = 0.1.
    

    顺便说一句,这里的“roc”这个词有些误导:返回的当然不是roc(它是 曲线 ,而不是数字),而是roc曲线下的面积,即auc(使用 metric='AUC' 在里面 trainControl 具有相同的效果)。