在 ROCR (R) 中绘制 precision@k 和 recall@k
Plotting precision@k and recall@k in ROCR (R)
我正在使用 ROCR 包评估 R 中的二元分类器。我的分类器为目标 0/1 标签输出一个介于 0 和 1 之间的分数。
我想绘制精度和召回@k,但找不到实现它的方法。在不指定 x 轴度量的情况下调用 performance()
绘制按分数截止的精度值:
library(ROCR)
#df <- a two-dimensional dataframe with prediction scores and actual labels of my classifier
pred <- prediction(df$score, df$label)
pr_curve <- performance(pred, measure="prec")
对于 k 处的精度(或召回率),我需要根据每个预测的等级绘制精度,按分数降序排列:
pred <- prediction(df$score, df$label)
pr_curve <- performance(pred, measure="prec", x.measure="rank") #but there seems to be no "rank" in ROCR!
有没有办法在 ROCR 中做到这一点?如果不是这种情况,我愿意使用替代库。
加载库并定义训练集和测试集:
library(mlbench)
library(e1071)
library(ROCR)
data(BreastCancer)
df = BreastCancer
idx = sample(1:nrow(df),150)
trn = df[idx,]
test = df[-idx,]
拟合朴素贝叶斯
fit = naiveBayes(Class ~ .,data=trn)
在性能手册中写道,
Precision/recall graphs: measure="prec", x.measure="rec".
绘图精确召回:
pred = prediction(predict(fit,test,type="raw")[,2],test$Class)
#plot to see it is working correctly:
plot(performance(pred,measure="prec",x.measure="rec"))
现在你的案例在 K 点做,我们也可以从头开始做精确召回:
#combine prob, predicted labels, and actual labels
res = data.frame(prob=predict(fit,test,type="raw")[,2],
predicted_label=predict(fit,test),
label = test$Class)
res = res[order(res$prob,decreasing=TRUE),]
res$rank = 1:nrow(res)
# calculate recall, which is the number of actual classes we get back
res$recall = cumsum(res$label=="malignant")/sum(res$label=="malignant")
# precision, number of malignant cases we predicted correctly
res$precision = cumsum(res$label=="malignant")/res$rank
# check the two plots
par(mfrow=c(1,2))
plot(performance(pred,measure="prec",x.measure="rec"))
plot(res$recall,res$precision,type="l")
现在你已经正确了,在 K 处获取或绘制精度很简单:
par(mfrow=c(1,2))
with(res,
plot(rank,precision,main="self-calculated",type="l"))
plot(pred@n.pos.pred[[1]],
pred@tp[[1]]/(pred@fp[[1]]+pred@tp[[1]]),
type="l",main="from RORC")
我不知道使用 .plot.performance 函数的方法。但是您可以使用存储在预测对象下的变量。 pred@tp 是真阳性,pred@fp 是假阳性,所以 tp / fp+fp 给出精度,pred@n.pos.pred 给出等级。
我正在使用 ROCR 包评估 R 中的二元分类器。我的分类器为目标 0/1 标签输出一个介于 0 和 1 之间的分数。
我想绘制精度和召回@k,但找不到实现它的方法。在不指定 x 轴度量的情况下调用 performance()
绘制按分数截止的精度值:
library(ROCR)
#df <- a two-dimensional dataframe with prediction scores and actual labels of my classifier
pred <- prediction(df$score, df$label)
pr_curve <- performance(pred, measure="prec")
对于 k 处的精度(或召回率),我需要根据每个预测的等级绘制精度,按分数降序排列:
pred <- prediction(df$score, df$label)
pr_curve <- performance(pred, measure="prec", x.measure="rank") #but there seems to be no "rank" in ROCR!
有没有办法在 ROCR 中做到这一点?如果不是这种情况,我愿意使用替代库。
加载库并定义训练集和测试集:
library(mlbench)
library(e1071)
library(ROCR)
data(BreastCancer)
df = BreastCancer
idx = sample(1:nrow(df),150)
trn = df[idx,]
test = df[-idx,]
拟合朴素贝叶斯
fit = naiveBayes(Class ~ .,data=trn)
在性能手册中写道,
Precision/recall graphs: measure="prec", x.measure="rec".
绘图精确召回:
pred = prediction(predict(fit,test,type="raw")[,2],test$Class)
#plot to see it is working correctly:
plot(performance(pred,measure="prec",x.measure="rec"))
现在你的案例在 K 点做,我们也可以从头开始做精确召回:
#combine prob, predicted labels, and actual labels
res = data.frame(prob=predict(fit,test,type="raw")[,2],
predicted_label=predict(fit,test),
label = test$Class)
res = res[order(res$prob,decreasing=TRUE),]
res$rank = 1:nrow(res)
# calculate recall, which is the number of actual classes we get back
res$recall = cumsum(res$label=="malignant")/sum(res$label=="malignant")
# precision, number of malignant cases we predicted correctly
res$precision = cumsum(res$label=="malignant")/res$rank
# check the two plots
par(mfrow=c(1,2))
plot(performance(pred,measure="prec",x.measure="rec"))
plot(res$recall,res$precision,type="l")
现在你已经正确了,在 K 处获取或绘制精度很简单:
par(mfrow=c(1,2))
with(res,
plot(rank,precision,main="self-calculated",type="l"))
plot(pred@n.pos.pred[[1]],
pred@tp[[1]]/(pred@fp[[1]]+pred@tp[[1]]),
type="l",main="from RORC")
我不知道使用 .plot.performance 函数的方法。但是您可以使用存储在预测对象下的变量。 pred@tp 是真阳性,pred@fp 是假阳性,所以 tp / fp+fp 给出精度,pred@n.pos.pred 给出等级。