一张图中的多条 ROC 曲线 ROCR [英] Multiple ROC curves in one plot ROCR
问题描述
是否可以使用 ROCR 包在同一图中绘制不同分类器的 roc 曲线?我试过了:
Is it possible to plot the roc curve for diffrent classifiers in the same plot using the ROCR package? I've tried:
>plot(perf.neuralNet, colorize=TRUE)
>lines(perf.randomForest)
但我明白了:
错误 en as.double(y) :无法将类型S4"强制转换为double"类型的向量
Error en as.double(y) : cannot coerce type 'S4' to vector of type 'double'
谢谢!
推荐答案
您的 lines
方法的问题是没有用于对象的通用 S4
行函数ROCR
包中定义的 performance
类.但是您可以像使用附加的 add = TRUE
参数一样使用通用绘图函数.例如,这部分来自 ?plot.performance
的示例页面:
The problem with your lines
-approach is that there is no generic S4
lines function for an object of class performance
defined in the ROCR
package. But you can use the generic plot function as you did with an additional add = TRUE
argument. For example this is partly from the example page of ?plot.performance
:
library(ROCR)
data(ROCR.simple)
pred <- prediction( ROCR.simple$predictions, ROCR.simple$labels )
pred2 <- prediction(abs(ROCR.simple$predictions +
rnorm(length(ROCR.simple$predictions), 0, 0.1)),
ROCR.simple$labels)
perf <- performance( pred, "tpr", "fpr" )
perf2 <- performance(pred2, "tpr", "fpr")
plot( perf, colorize = TRUE)
plot(perf2, add = TRUE, colorize = TRUE)
或者,您可以将所有预测存储在一个矩阵中,并在一个矩阵中执行所有后续步骤:
OR, you can store all your predictions in a matrix and do all the subsequent steps in one:
preds <- cbind(p1 = ROCR.simple$predictions,
p2 = abs(ROCR.simple$predictions +
rnorm(length(ROCR.simple$predictions), 0, 0.1)))
pred.mat <- prediction(preds, labels = matrix(ROCR.simple$labels,
nrow = length(ROCR.simple$labels), ncol = 2) )
perf.mat <- performance(pred.mat, "tpr", "fpr")
plot(perf.mat, colorize = TRUE)
顺便说一句,如果你出于某种原因真的想使用 lines
来绘制连续的 ROC 曲线,你将不得不做某事.像这样:
Btw, if you for some reason really wanted to use lines
to plot consecutive ROC curves you would have to do sth. like this:
plot(perf)
lines(perf2@x.values[[1]], perf2@y.values[[1]], col = 2)
这篇关于一张图中的多条 ROC 曲线 ROCR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!