精度得分与指标公式不匹配

时间:2018-12-30 10:52:33

标签: python scikit-learn confusion-matrix

如何基于此混淆矩阵手动计算分数?

enter image description here

在这种情况下,精度分数应该是多少? tp /(tp + fp)转换为99%(102/103)。对?但精度得分仅为98.36%。如果以下分数是正确的,为什么精度分数不匹配? (准确率正确为94.73%(162/171)

enter image description here

我从:

https://towardsdatascience.com/grid-search-for-model-tuning-3319b259367e


更新:

如果我要获得此图所示的输出,标签顺序应该是什么?

enter image description here

2 个答案:

答案 0 :(得分:1)

问题是,混淆矩阵中的fooTP被交换了。

如二进制example中所述,标签的解释如下:

真否定 FP

真阳性 expected=0, predicted=0

假阴性 expected=1, predicted=1

假阳性 expected=1, predicted=0

以您的示例为例:

expected=0, predicted=1

所以措施符合预期。

混淆矩阵已记录here

  

根据定义,混淆矩阵等于已知在组中但预计在组中的观察次数。   因此,在二元分类中,真实负数为C 0,0   ,假阴性为C 1,0,真阳性为C 1,1,假阳性为C 0,1。

这将导致以下结果:

##              TN       TP       FN       FP
expected =  [0]*102 + [1]*60 + [1]*8 + [0]*1
predicted = [0]*102 + [1]*60 + [0]*8 + [1]*1 

print ("precision " + '{:.16f}'.format(precision_score(expected, predicted)))
print ("recall    " + '{:.16f}'.format(recall_score(expected, predicted)))
print ("accuracy  " + '{:.16f}'.format(accuracy_score(expected, predicted)))

precision 0.9836065573770492
recall    0.8823529411764706
accuracy  0.9473684210526315

所以这些措施再次可以,只是混淆矩阵中的位置不是通常的位置,results = confusion_matrix(expected, predicted) print('TN ' ,results[0][0]) print('TP ' ,results[1][1]) print('FN ' ,results[1][0]) print('FP ' ,results[0][1]) print(results) TN 102 TP 60 FN 8 FP 1 [[102 1] [ 8 60]] 位于左上角。

解决方法很简单,就像手动交换TPTP

TN

答案 1 :(得分:0)

从您提到的博客中

enter image description here

您会看到真正的正数(通常将正数表示为1)是第四象限。我了解常见的混淆矩阵示例会将True正值作为第一象限,但是在博客中却是相反的。

因此,准确度得分计算应为60/61 = 0.9836。