【发布时间】:2020-07-12 16:25:48
【问题描述】:
首先,感谢您阅读我的问题 - 我希望这是正确的地方。
我正在从头开始对混淆矩阵的敏感性、特异性和精度计算进行编码。我有以下 4 个类的混淆矩阵。
True Class
1 2 3 4
1 [[ 0 1 3 0]
Predicted 2 [ 0 181 23 0]
Class 3 [ 0 17 53 14]
4 [ 0 3 22 77]]
当我使用 Sklearn.metrics.classification_report 这是我得到的:
precision recall f1-score support
0.00 0.00 0.00 4
0.89 0.89 0.89 204
0.52 0.63 0.57 84
0.85 0.75 0.80 102
但是,对于精度和召回率,我得到(即精度和召回率的值被翻转):
precision recall
0.0 nan
0.887 0.896
0.631 0.524
0.755 0.846
对于每个类,我计算以下真阳性、假阳性、真阴性和假阴性:
class Tp Fp Tn Fn
1 0 4 390 0
2 181 23 169 21
3 53 31 262 48
4 77 25 278 14
我使用的公式 (https://en.wikipedia.org/wiki/Confusion_matrix) 是:
sensitivity/recall = true_positives / (true_positives + false_negatives)
precision = true_positives/(true_positives+false_positives)
我哪里错了,肯定不是sklearn的分类问题,我是不是看错了什么?
编辑:我的函数用于计算精度和召回值,给定来自 sklearn.metrics.confusion_matrix 的混淆矩阵和类号列表,例如类 1-3:[1,2,3] 类。
def calc_precision_recall(conf_matrix, class_labels):
# for each class
for i in range(len(class_labels)):
# calculate true positives
true_positives =(conf_matrix[i, i])
# false positives
false_positives = (conf_matrix[i, :].sum() - true_positives)
# false negatives
false_negatives = 0
for j in range(len(class_labels)):
false_negatives += conf_matrix[j, i]
false_negatives -= true_positives
# and finally true negatives
true_negatives = (conf_matrix.sum() - false_positives - false_negatives - true_positives)
# print calculated values
print(
"Class label", class_labels[i],
"T_positive", true_positives,
"F_positive", false_positives,
"T_negative", true_negatives,
"F_negative", false_negatives,
"\nSensitivity/recall", true_positives / (true_positives + false_negatives),
"Specificity", true_negatives / (true_negatives + false_positives),
"Precision", true_positives/(true_positives+false_positives), "\n"
)
return
【问题讨论】:
标签: python machine-learning scikit-learn confusion-matrix