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

precision score does not match with metrics formula

如何根据这个混淆矩阵手动计算分数?

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

我从 :

得到这个例子

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


更新:

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

问题是,混淆矩阵中的 TPFP 被调换了。

如二元分类example中所述,标签解释如下:

真阴性 expected=0, predicted=0

真阳性 expected=1, predicted=1

假阴性 expected=1, predicted=0

误报 expected=0, predicted=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

所以措施是符合预期的。

记录了混淆矩阵 here

By definition a confusion matrix is such that is equal to the number of observations known to be in group but predicted to be in group. Thus in binary classification, the count of true negatives is C 0,0 , false negatives is C 1,0, true positives C 1,1 is and false positives is C 0,1.

这导致以下结果:

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]]

所以措施再次确定,只是混淆矩阵中的位置不是通常的左上角TP

补救方法很简单,只需手动交换 TPTN

(results[0][0],results[1][1]) = (results[1][1],results[0][0])
print(results)

[[ 60   1]
 [  8 102]]

来自您提到的博客,

你可以看到真阳性(阳性通常表示为 1)是第四象限。我知道常见的混淆矩阵示例会将真阳性作为第一象限,但在博客中它是相反的。

因此精度得分计算为 60/61 = 0.9836。