【问题标题】:Error in performance(pred, "tpr", "fpr")性能错误(pred、“tpr”、“fpr”)
【发布时间】:2019-01-11 23:36:13
【问题描述】:

我正在尝试使用 R 中的 ROCR 包绘制 ROC 曲线,但遇到以下错误:

Error in performance(pred, "tpr", "fpr") : 
Assertion on 'pred' failed: Must have class 'Prediction', but has class 'prediction'

这是我用来进行性能段调用的代码:

#convert actual and predicted labels to numeric
predicted<-as.numeric(as.character(test$Class))
actual<-as.numeric(as.character(test$overall_satisfaction))

#generate confusion matrix and label positive class
library(caret)
confusionMatrix(predicted,actual,positive="1")

混淆矩阵输出看起来很好。但是,在下一段中,ROCR 中的性能函数会引发错误,因此 ROC 曲线没有绘制出来。

#ROC curve
library(ROCR)
pred<-prediction(predicted, actual)
perf<-performance(pred,"tpr","fpr")
plot(perf,col="red", main="mlr_parameters_ROC")
abline(0,1, lty = 8, col = "grey")

我无法弄清楚上面的代码有什么问题。有人可以帮忙吗?

【问题讨论】:

    标签: r roc


    【解决方案1】:

    上面的代码似乎无法访问 ROCR 包中的性能函数,这就是我看到错误的原因。

    我保持其他一切不变,但解决问题如下:

    perf<-ROCR::performance(pred,"tpr","fpr")
    

    ROC 曲线现在画得很好!

    【讨论】:

    • 您可能加载了另一个定义performance 函数的包。你介意告诉我们哪一个,因为这可以帮助其他可能遇到同样错误的人吗?如果你输入print(performance),你应该在输出的末尾得到一个提示。
    • 您现在获得了 ROCR 的性能函数。很可能您当时附加了一个冲突的包,但现在不再附加。
    【解决方案2】:

    我今天也遇到了同样的问题。这是我的印刷品(表现):

    > print(performance)
    function (pred, measures, task = NULL, model = NULL, feats = NULL) 
    {
        if (!is.null(pred)) 
            assertClass(pred, classes = "Prediction")
        measures = checkMeasures(measures, pred$task.desc)
        res = vnapply(measures, doPerformanceIteration, pred = pred, 
            task = task, model = model, td = NULL, feats = feats)
        setNames(res, extractSubList(measures, "id"))
    }
    <bytecode: 0x0000000024d441e0>
    <environment: namespace:mlr>
    

    所以看起来 mlr 是与 ROCR 在 performance() 上发生冲突的库

    【讨论】:

      猜你喜欢
      • 2015-08-09
      • 1970-01-01
      • 2015-08-28
      • 2019-08-22
      • 2014-08-05
      • 2013-09-01
      • 2018-11-12
      • 2020-12-30
      • 2020-12-28
      相关资源
      最近更新 更多