ROCR 包......我没有得到什么?
ROCR package... what am I not getting?
我正在使用 R 中的 ROCR 包测试一个简单的案例。基本上,这是我的代码。我有一组真实值,对于每个值,我有一组预测,如果预测在 |2| 内,我的标签为 1的真值,否则为 0,如下所示:
ID<- c(1,2,3,4,5)
preds<-c(6,3,2,1,4)
truevals<- c(8,4,2,1,7)
df<-data.frame(ID, preds,truevals)
df<- mutate(df, labels = ifelse(abs(df$preds - df$truevals) < 2, 1, 0))
predtest<-prediction(df$preds, df$labels)
auc <- performance(predtest, 'auc')
但是我计算的AUC是0,即
> auc@y.values[[1]]
[1] 0
我的问题是,我做错了什么?明明有些分类是"correct",那么为什么AUC要为零呢?我不明白什么?我的值按 ID 排列,即我假设它们是 ID 为 1 到 5 的人的测量值。是否有一些我没有考虑的排序问题?谢谢
您的数据显然是可分离的。 pred
1 到 3 使用 label
1 和 4 和 6 使用标签 0。
那应该给你一个等于 1 的 AUC,这与 0 的 AUC 是一样的。这只是一个参考问题。
这是一个例子:
library(ROCR)
ID = c(1,2,3,4,5)
preds = c(6,3,2,1,4)
truevals = c(8,4,2,1,7)
df = data.frame(ID, preds,truevals)
df = mutate(df, labels = ifelse(abs(df$preds - df$truevals) < 2, 1, 0))
#Changing the labels is just a matter of reference
#the algorithm is oblivious to their meaning
df$labels = 1 - df$labels
predtest = prediction(df$preds, df$labels)
auc = performance(predtest, "auc")
输出:
> auc@yvalues[[1]]
[1] 1
尽管切换标签会引发有关泄漏的问题,但我认为这不在问题范围内。
编辑:
AUC 是可分离性的度量,它是您将随机正面实例排名高于随机负面实例的概率。 ROC 曲线只是 x:1-特异性和 y:Sensitivity,给定不同的阈值 class 化对你的预测。
所以,关于:
[...] if I have a an arbitrary set of values and a set of of predictions of
those values, how do I get an ROC curve? I'm really confused. I assume
the closer the prediction, the better? I'm just not sure how to do
this. I don't know how to assign classes to the true values. Don't
there need to be rankings of some sort???
您有一组二进制分类数据和一个连续预测变量。现在在预测器中设置一个阈值,class 验证高于该阈值的观察值一个 class 或另一个 class。测量特异性和灵敏度,并在曲线中标记该点。尝试其他阈值(改变 Sens 和 Spec 的可能性是有限的)并在曲线上绘制这些点。这就是 ROC 曲线。
AUC 将越高,您的 classes 与预测变量的分离度越大。叠加的越多AUC越低
要了解发生了什么,请绘制数据的箱线图:
boxplot(preds ~ labels, data = df)
请注意 0
class 的预测 比 1
class 的预测高。
现在看看维基百科对AUC的定义:
[The AUC] is equal to the probability that a classifier will rank a
randomly chosen positive instance higher than a randomly chosen
negative one (assuming 'positive' ranks higher than 'negative'). (1)
现在按照惯例,1
s 通常被认为是阳性,而 0
s 是阴性。正如我们刚刚看到的,您的 1
s,现在是阳性,排名 低于 低于阴性(0
s),因此它们更高的概率为 0。
您有 3 个选项:
一个。如果你的 1
是负数,ROCR 有一个 label.ordering
参数:
predtest <- prediction(df$preds, df$labels, label.ordering = c(1, 0))
auc <- performance(predtest, 'auc')
auc@y.values[[1]]
[1] 1
乙。如果你的 1
确实是积极的,你可以反转你的预测,使积极更高(注意 df$labels
前面的 -
符号):
predtest <- prediction(df$preds, -df$labels)
auc <- performance(predtest, 'auc')
auc@y.values[[1]]
[1] 1
C。您还可以颠倒 AUC 的定义,使 classifier 将随机选择的正面实例排名 低于 的概率变为随机选择的负面实例。 ROCR
不支持此功能,但其他软件包支持,甚至可能会自动为您选择此功能。
说到底,重要的不是你的AUC是高于还是低于0.5,而是离对角线有多远。如果它低于0.5,或者"worse than random",你只需要反转你的解释就可以比随机表现得更好。
我正在使用 R 中的 ROCR 包测试一个简单的案例。基本上,这是我的代码。我有一组真实值,对于每个值,我有一组预测,如果预测在 |2| 内,我的标签为 1的真值,否则为 0,如下所示:
ID<- c(1,2,3,4,5)
preds<-c(6,3,2,1,4)
truevals<- c(8,4,2,1,7)
df<-data.frame(ID, preds,truevals)
df<- mutate(df, labels = ifelse(abs(df$preds - df$truevals) < 2, 1, 0))
predtest<-prediction(df$preds, df$labels)
auc <- performance(predtest, 'auc')
但是我计算的AUC是0,即
> auc@y.values[[1]]
[1] 0
我的问题是,我做错了什么?明明有些分类是"correct",那么为什么AUC要为零呢?我不明白什么?我的值按 ID 排列,即我假设它们是 ID 为 1 到 5 的人的测量值。是否有一些我没有考虑的排序问题?谢谢
您的数据显然是可分离的。 pred
1 到 3 使用 label
1 和 4 和 6 使用标签 0。
那应该给你一个等于 1 的 AUC,这与 0 的 AUC 是一样的。这只是一个参考问题。
这是一个例子:
library(ROCR)
ID = c(1,2,3,4,5)
preds = c(6,3,2,1,4)
truevals = c(8,4,2,1,7)
df = data.frame(ID, preds,truevals)
df = mutate(df, labels = ifelse(abs(df$preds - df$truevals) < 2, 1, 0))
#Changing the labels is just a matter of reference
#the algorithm is oblivious to their meaning
df$labels = 1 - df$labels
predtest = prediction(df$preds, df$labels)
auc = performance(predtest, "auc")
输出:
> auc@yvalues[[1]]
[1] 1
尽管切换标签会引发有关泄漏的问题,但我认为这不在问题范围内。
编辑: AUC 是可分离性的度量,它是您将随机正面实例排名高于随机负面实例的概率。 ROC 曲线只是 x:1-特异性和 y:Sensitivity,给定不同的阈值 class 化对你的预测。
所以,关于:
[...] if I have a an arbitrary set of values and a set of of predictions of those values, how do I get an ROC curve? I'm really confused. I assume the closer the prediction, the better? I'm just not sure how to do this. I don't know how to assign classes to the true values. Don't there need to be rankings of some sort???
您有一组二进制分类数据和一个连续预测变量。现在在预测器中设置一个阈值,class 验证高于该阈值的观察值一个 class 或另一个 class。测量特异性和灵敏度,并在曲线中标记该点。尝试其他阈值(改变 Sens 和 Spec 的可能性是有限的)并在曲线上绘制这些点。这就是 ROC 曲线。
AUC 将越高,您的 classes 与预测变量的分离度越大。叠加的越多AUC越低
要了解发生了什么,请绘制数据的箱线图:
boxplot(preds ~ labels, data = df)
请注意 0
class 的预测 比 1
class 的预测高。
现在看看维基百科对AUC的定义:
[The AUC] is equal to the probability that a classifier will rank a randomly chosen positive instance higher than a randomly chosen negative one (assuming 'positive' ranks higher than 'negative'). (1)
现在按照惯例,1
s 通常被认为是阳性,而 0
s 是阴性。正如我们刚刚看到的,您的 1
s,现在是阳性,排名 低于 低于阴性(0
s),因此它们更高的概率为 0。
您有 3 个选项:
一个。如果你的 1
是负数,ROCR 有一个 label.ordering
参数:
predtest <- prediction(df$preds, df$labels, label.ordering = c(1, 0))
auc <- performance(predtest, 'auc')
auc@y.values[[1]]
[1] 1
乙。如果你的 1
确实是积极的,你可以反转你的预测,使积极更高(注意 df$labels
前面的 -
符号):
predtest <- prediction(df$preds, -df$labels)
auc <- performance(predtest, 'auc')
auc@y.values[[1]]
[1] 1
C。您还可以颠倒 AUC 的定义,使 classifier 将随机选择的正面实例排名 低于 的概率变为随机选择的负面实例。 ROCR
不支持此功能,但其他软件包支持,甚至可能会自动为您选择此功能。
说到底,重要的不是你的AUC是高于还是低于0.5,而是离对角线有多远。如果它低于0.5,或者"worse than random",你只需要反转你的解释就可以比随机表现得更好。