如何使用ROCR包计算AUC

时间:2017-01-07 16:36:37

标签: r machine-learning roc auc

我已经安装了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")## 

4 个答案:

答案 0 :(得分:5)

您的示例似乎并不完整,因此我似乎无法运行并相应地更改它,但尝试插入以下内容:

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

你可以在sandipan's code之后追加它,它只为你提供情节。

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

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

答案 1 :(得分:4)

prediction包中的ROCR方法开始。

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]]

答案 2 :(得分: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)

enter image description here

答案 3 :(得分:0)

计算AUC

# Outcome Flag & Predicted probability
roc_val <-roc(testing.label,gbmPred) 

plot(roc_val,col='blue')

auc(roc_val)