【问题标题】:How to compute AUC with ROCR package如何使用 ROCR 包计算 AUC
【发布时间】:2017-05-22 06:46:29
【问题描述】:

我已经拟合了一个 SVM 模型并使用 ROCR 包创建了 ROC 曲线。如何计算曲线下面积 (AUC)?

set.seed(1)
tune.out=tune(svm ,Negative~.-Positive, data=trainSparse, kernel ="radial",ranges=list(cost=c(0.1,1,10,100,1000),gamma=c(0.5,1,2,3,4) ))
summary(tune.out)
best=tune.out$best.model

##prediction on the test set
ypred = predict(best,testSparse, type = "class")
table(testSparse$Negative,ypred)

###Roc curve
yhat.opt = predict(best,testSparse,decision.values = TRUE)
fitted.opt = attributes(yhat.opt)$decision.values
rocplot(fitted.opt,testSparse ["Negative"], main = "Test Data")## 

【问题讨论】:

标签: r machine-learning roc auc


【解决方案1】:

试试这个:

tune.out=tune(svm ,Negative~.-Positive, data=trainSparse, kernel ="radial",
              ranges=list(cost=c(0.1,1,10,100,1000),gamma=c(0.5,1,2,3,4), 
              probability = TRUE)) # train svm with probability option true
summary(tune.out)
best=tune.out$best.model
yhat.opt = predict(best,testSparse,probability = TRUE)

# Roc curve
library(ROCR)
# choose the probability column carefully, it may be 
# probabilities[,1] or probabilities[,2], depending on your factor levels 
pred <- prediction(attributes(yhat.opt)$probabilities[,2], testSparse$Negative) 
perf <- performance(pred,"tpr","fpr")
plot(perf,colorize=TRUE)

【讨论】:

  • 什么是df?当我运行该命令时,它出现: df$Negative
  • df 是原始数据帧(您将其分为两部分,训练和测试),您无需担心,只需确保变量 Negative 是一个因素。跨度>
  • 好吧,因为我做了这个: tweets$Negative= as.factor(tweets$Sent
【解决方案2】:

您的示例似乎并不完整,因此我似乎无法运行它并相应地对其进行更改,但请尝试按照以下方式插入一些内容:

...
prediction.obj <- prediction(...)
perf <- performance(prediction.obj, measure = "auc")
print("AUC: ", perf@y.values)

您可以在sandipan's code 之后附加它,这样您就可以单独获得情节。

请参阅performance 的 ROCR 手册,第 5 页:ftp://ftp.auckland.ac.nz/pub/software/CRAN/doc/packages/ROCR.pdf

"auc"performance 可以产生的可能措施之一。

【讨论】:

    【解决方案3】:

    ROCR 包中的prediction 方法开始。

    pred_ROCR <- prediction(df$probabilities, df$target)
    

    在情节中获得 ROC:

    roc_ROCR <- performance(pred_ROCR, measure = "tpr", x.measure = "fpr")
    plot(roc_ROCR, main = "ROC curve", colorize = T)
    abline(a = 0, b = 1)
    

    并获得 AUC 值:

      auc_ROCR <- performance(pred_ROCR, measure = "auc")
      auc_ROCR <- auc_ROCR@y.values[[1]]
    

    【讨论】:

      【解决方案4】:

      计算 AUC

      # Outcome Flag & Predicted probability
      roc_val <-roc(testing.label,gbmPred) 
      
      plot(roc_val,col='blue')
      
      auc(roc_val)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-12
        • 2022-01-13
        • 2012-05-11
        • 2022-06-13
        • 2017-01-19
        • 1970-01-01
        • 2014-08-07
        • 1970-01-01
        相关资源
        最近更新 更多