本文介绍了如何从 Python 中的混淆矩阵获得精度、召回率和 f 度量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 并且有一些混淆矩阵.我想通过多类分类中的混淆矩阵来计算精度和召回率以及 f 度量.我的结果日志不包含 y_truey_pred,只包含混淆矩阵.

I'm using Python and have some confusion matrixes. I'd like to calculate precisions and recalls and f-measure by confusion matrixes in multiclass classification. My result logs don't contain y_true and y_pred, just contain confusion matrix.

你能告诉我如何从多类分类中的混淆矩阵中得到这些分数吗?

Could you tell me how to get these scores from confusion matrix in multiclass classification?

推荐答案

让我们考虑 MNIST 数据分类的情况(10 个类),其中对于 10,000 个样本的测试集,我们得到以下混淆矩阵 cm(Numpy数组):

Let's consider the case of MNIST data classification (10 classes), where for a test set of 10,000 samples we get the following confusion matrix cm (Numpy array):

array([[ 963,    0,    0,    1,    0,    2,   11,    1,    2,    0],
       [   0, 1119,    3,    2,    1,    0,    4,    1,    4,    1],
       [  12,    3,  972,    9,    6,    0,    6,    9,   13,    2],
       [   0,    0,    8,  975,    0,    2,    2,   10,   10,    3],
       [   0,    2,    3,    0,  953,    0,   11,    2,    3,    8],
       [   8,    1,    0,   21,    2,  818,   17,    2,   15,    8],
       [   9,    3,    1,    1,    4,    2,  938,    0,    0,    0],
       [   2,    7,   19,    2,    2,    0,    0,  975,    2,   19],
       [   8,    5,    4,    8,    6,    4,   14,   11,  906,    8],
       [  11,    7,    1,   12,   16,    1,    1,    6,    5,  949]])

为了获得精度&回想一下(每个类),我们需要计算每个类的 TP、FP 和 FN.我们不需要 TN,但我们也会计算它,因为它将帮助我们进行健全性检查.

In order to get the precision & recall (per class), we need to compute the TP, FP, and FN per class. We don't need TN, but we will compute it, too, as it will help us for our sanity check.

真正的正数只是对角线元素:

The True Positives are simply the diagonal elements:

# numpy should have already been imported as np
TP = np.diag(cm)
TP
# array([ 963, 1119,  972,  975,  953,  818,  938,  975,  906,  949])

误报是各列的总和,减去对角元素(即 TP 元素):

The False Positives are the sum of the respective column, minus the diagonal element (i.e. the TP element):

FP = np.sum(cm, axis=0) - TP
FP
# array([50, 28, 39, 56, 37, 11, 66, 42, 54, 49])

同样,假负数是各行的总和,减去对角线(即 TP)元素:

Similarly, the False Negatives are the sum of the respective row, minus the diagonal (i.e. TP) element:

FN = np.sum(cm, axis=1) - TP
FN
# array([17, 16, 60, 35, 29, 74, 20, 53, 68, 60])

现在,True Negatives 有点棘手;让我们首先考虑一下真正的负值究竟意味着什么,例如 0 类:它意味着所有被正确识别为 不是 0.所以,本质上我们应该做的是删除相应的行 &混淆矩阵中的列,然后总结所有剩余的元素:

Now, the True Negatives are a little trickier; let's first think what exactly a True Negative means, with respect to, say class 0: it means all the samples that have been correctly identified as not being 0. So, essentially what we should do is remove the corresponding row & column from the confusion matrix, and then sum up all the remaining elements:

num_classes = 10
TN = []
for i in range(num_classes):
    temp = np.delete(cm, i, 0)    # delete ith row
    temp = np.delete(temp, i, 1)  # delete ith column
    TN.append(sum(sum(temp)))
TN
# [8970, 8837, 8929, 8934, 8981, 9097, 8976, 8930, 8972, 8942]

让我们做一个完整性检查:对于每个类,TP、FP、FN 和 TN 的总和必须等于我们测试集的大小(这里是 10,000):让我们确认这确实如此:

Let's make a sanity check: for each class, the sum of TP, FP, FN, and TN must be equal to the size of our test set (here 10,000): let's confirm that this is indeed the case:

l = 10000
for i in range(num_classes):
    print(TP[i] + FP[i] + FN[i] + TN[i] == l)

结果是

True
True
True
True
True
True
True
True
True
True

计算完这些量后,现在可以直接获得精度 &每班回忆:

Having calculated these quantities, it is now straightforward to get the precision & recall per class:

precision = TP/(TP+FP)
recall = TP/(TP+FN)

在这个例子中是

precision
# array([ 0.95064166,  0.97558849,  0.96142433,  0.9456838 ,  0.96262626,
#         0.986731  ,  0.93426295,  0.95870206,  0.94375   ,  0.9509018])

recall
# array([ 0.98265306,  0.98590308,  0.94186047,  0.96534653,  0.97046843,
#         0.91704036,  0.97912317,  0.94844358,  0.9301848 ,  0.94053518])

类似地,我们可以计算相关的数量,比如特异性(回想一下,敏感性与召回率是一回事):

Similarly we can compute related quantities, like specificity (recall that sensitivity is the same thing with recall):

specificity = TN/(TN+FP)

我们示例的结果:

specificity
# array([0.99445676, 0.99684151, 0.9956512 , 0.99377086, 0.99589709,
#        0.99879227, 0.99270073, 0.99531877, 0.99401728, 0.99455011])

您现在应该能够针对任何大小的混淆矩阵计算这些数量.

You should now be able to compute these quantities virtually for any size of your confusion matrix.

这篇关于如何从 Python 中的混淆矩阵获得精度、召回率和 f 度量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 16:21