我发现irr
包在计算weighted kappa
时有两个大错误
请告诉我这两个bug是否真的存在,或者我误解了什么。
您可以使用以下示例复制这些bug
第一个错误:需要更正混淆矩阵中的标签类型。
我有两对疾病程度的分数(从0到100,而0是健康的,100是病重的)
在label_test.csv
(您只需将数据复制并粘贴到磁盘上即可执行以下测试):
0
1
1
1
0
14
53
3
在pred_test.csv
中:
0
1
1
0
3
4
54
6
在script_r.R
中:
library(irr)
label <- read.csv('label_test.csv',header=FALSE)
pred <- read.csv('pred_test.csv',header=FALSE)
kapp <- kappa2(data.frame(label,pred),"unweighted")
kappa <- getElement(kapp,"value")
print(kappa) # output: 0.245283
w_kapp <- kappa2(data.frame(label,pred),"equal")
weighted_kappa <- getElement(w_kapp,"value")
print(weighted_kappa) # output: 0.443038
当我使用Python
计算kappa
和weighted_kappa
时,在script_python.py
中:
from sklearn.metrics import cohen_kappa_score
label = pd.read_csv(label_file, header=None).to_numpy()
pred = pd.read_csv(pred_file, header=None).to_numpy()
kappa = cohen_kappa_score(label.astype(int), pred.astype(int))
print(kappa) # output: 0.24528301886792447
weighted_kappa = cohen_kappa_score(label.astype(int), pred.astype(int), weights='linear', labels=np.array(list(range(100))) )
print(weighted_kappa) # output: 0.8359908883826879
我们可以发现由{R
中的irr
包中的weighted_kappa
是错误的。详情如下
在调试过程中,我们会发现R
中irr
的混淆矩阵是:
我们可以发现订单错了。标签的顺序应该从[0, 1, 14, 3, 4, 53, 54, 6]
更改为[0, 1, 3, 4, 6, 14, 53, 54]
,就像在Python中一样。似乎irr
包使用了基于字符串的排序方法而不是基于整数的排序方法,这将把14
放在3
的前面。这个错误可以而且应该很容易纠正
第二个错误:R中的混淆矩阵不完整
在我的pred_test.csv
和label_test.csv
中,值不能覆盖从0到100的所有可能值。因此,来自R
的irr
中的默认混淆矩阵将丢失数据中未出现的值。这应该是固定的
让我们看另一个例子
在pred_test.csv
中,让我们将标签从54
更改为99
。然后,我们再次运行script_r.R
和script_python.py
。结果是:
In R:
kappa: 0.245283
weighted_kappa: 0.443038
In Python:
kappa: 0.24528301886792447
weighted_kappa: 0.592891760904685
我们可以发现R
中的irr
中的weighted_kappa
没有任何变化。但是{irr
又犯了一个错误
原因是sklearn
可以让我们将full labels
传递给混淆矩阵,这样混淆矩阵的形状将是100*100,但是在irr
中,混淆矩阵的标签是从label
和pred
的唯一值计算出来的,这将丢失许多其他可能的值。此错误将在此处为53
和99
分配相同的权重。因此,最好在irr
包中提供一个选项,让custumer提供custumlabels
,就像他们在sklearn
from Python
中所做的那样
我已经给该软件包的作者发了电子邮件,他说他将在下一次更新中修复该漏洞
详情如下:
作者的解决方案行不通,因为在kappa2函数的code中,它将您的评级转换为矩阵,一旦您将因子转换为矩阵,级别将丢失,这是一行:
您可以在数据上尝试,它将转换为字符:
同样的结果:
我建议使用
DescTools
,您只需要使用R中的table()
函数提供混淆矩阵,并正确声明上述因子:相关问题 更多 >
编程相关推荐