mac*_*nny 8 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")##
Run Code Online (Sandbox Code Playgroud)
Dan*_*Dan 10
prediction从ROCR包中的方法开始。
pred_ROCR <- prediction(df$probabilities, df$target)
Run Code Online (Sandbox Code Playgroud)
在情节中获得ROC:
roc_ROCR <- performance(pred_ROCR, measure = "tpr", x.measure = "fpr")
plot(roc_ROCR, main = "ROC curve", colorize = T)
abline(a = 0, b = 1)
Run Code Online (Sandbox Code Playgroud)
并获得AUC值:
auc_ROCR <- performance(pred_ROCR, measure = "auc")
auc_ROCR <- auc_ROCR@y.values[[1]]
Run Code Online (Sandbox Code Playgroud)
您的示例似乎并不完整,因此我似乎无法运行并相应地对其进行更改,但是请尝试插入以下内容:
...
prediction.obj <- prediction(...)
perf <- performance(prediction.obj, measure = "auc")
print("AUC: ", perf@y.values)
Run Code Online (Sandbox Code Playgroud)
您可以将其附加在sandipan的代码之后,从而单独获得图表。
请参阅performance第5页的ROCR手册:ftp : //ftp.auckland.ac.nz/pub/software/CRAN/doc/packages/ROCR.pdf
"auc"是可能performance产生的可能措施之一。
尝试这个:
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14896 次 |
| 最近记录: |