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

标签 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

最佳答案

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

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

真阴性 预期=0,预测=0

真阳性 预期=1,预测=1

漏报 预期=1,预测=0

误报 预期=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

所以这些措施符合预期。

混淆矩阵已记录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]]

关于python - 精度分数与指标公式不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53977031/

相关文章:

python - 为什么我不能在 Python 中设置全局变量?

python-3.x - 线性回归中的混淆矩阵

algorithm - 如何计算分类错误率

python - ValueError 时间数据与格式不匹配

python - Selenium 像崩溃一样向下滚动 Facebook 页面

python - 如何使用 python 在 Mac OS X 中获取环境变量?

python - LabelEncoder 将不同的值编码为相同的值

python - 使用 5 倍交叉验证时,在高度不平衡的数据中混淆 F1 分数 和 AUC 分数

python - 编写一个 fasttext 自定义转换器

machine-learning - 来自 Scikit_Learn 混淆矩阵和 Scikit_Learn Recall_Score 的灵敏度不匹配