gpt4 book ai didi

r - 在 R : calculate Area Under Precision/Recall Curve (AUPR)?

转载 作者:行者123 更新时间:2023-12-04 10:31:58 36 4
gpt4 key购买 nike

假设我有两个矩阵:A 表示标签矩阵,B 表示 A 的对应预测概率矩阵。现在我想根据矩阵 A 和 B 计算 AUPR(Area Under Precision/Recall Curve)。对于普通 AUC(Area Under ROC Curve),R中有很多包,比如ROCR,pROC,可以直接计算AUC值,但是目前R中有哪些包可以计算AUPR呢?或者你能帮助给出计算 AUPR 的方法吗?
这是两个示例矩阵:

> pp
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 0.01792 0.00155 -0.00140 0.00522 0.01320 0.22506 0.00454
[2,] 0.05883 0.11256 0.82862 0.12406 0.08298 -0.00392 0.30724
[3,] 0.00743 0.06357 0.14500 0.00213 0.00545 0.03452 0.11189
[4,] 0.02571 0.01460 0.01108 0.00494 0.01246 0.11880 0.05504
[5,] 0.02407 0.00961 0.00720 0.00382 0.01039 0.10974 0.04512

> ll
D00040 D00066 D00067 D00075 D00088 D00094 D00105
hsa190 0 0 0 0 0 1 0
hsa2099 0 1 1 0 0 0 1
hsa2100 0 0 0 0 0 0 1
hsa2101 0 0 0 0 0 0 0
hsa2103 0 0 0 0 0 0 0
pp是真实标签的预测概率矩阵 ll矩阵,和 ll只是标签矩阵。

提前致谢。

最佳答案

我首先将预测分数和类别从矩阵转换为向量。

有一个“PRROC”包,它提供了与“ROCR”类似的生成ROC和PRC的功能,并且还给出了PRC的AUC。

具体来说,我正在使用数据 ROCR.simple以“ROCR”包为例。

library(PRROC)
library(ROCR)
data("ROCR.simple")
scores <- data.frame(ROCR.simple$predictions, ROCR.simple$labels)
pr <- pr.curve(scores.class0=scores[scores$ROCR.simple.labels=="1",]$ROCR.simple.predictions,
scores.class1=scores[scores$ROCR.simple.labels=="0",]$ROCR.simple.predictions,
curve=T)

请注意,在此函数中,“ score.class0 ”需要是 的分数。正类(这有点令人困惑,因为我个人认为 0 为负数,1 为正数)。所以我交换了0和1的顺序。

这样PR曲线和AUC都保存在 pr中多变的。
pr

Precision-recall curve

Area under curve (Integral):
0.7815038

Area under curve (Davis & Goadrich):
0.7814246

Curve for scores from 0.005422562 to 0.9910964
( can be plotted with plot(x) )

然后,您可以使用 plot(pr) 绘制中国。或使用 ggplot:
y <- as.data.frame(pr$curve)
ggplot(y, aes(y$V1, y$V2))+geom_path()+ylim(0,1)

得到的曲线与ROCR包制作的曲线相同。

enter image description here
enter image description here

关于r - 在 R : calculate Area Under Precision/Recall Curve (AUPR)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25020788/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com