使用 numpy 或 pansas 从 3x3 混淆矩阵中提取准确度、精确度、召回率等常见度量的简便方法?

Easy way to extract common measures such as accuracy, precision, recall from 3x3 confusion matrix with numpy or pansas?

enter image description here

你好,有谁知道如何加快这个过程。仅使用 numpy 或 pandas 从 3x3 混淆矩阵中提取准确度、精确度、召回率、f 分数等常见度量的简单方法?有什么建议么?我附上一张我的结果图片。

cm =confusion_matrix.to_numpy()
acuracy_0 = np.round_(cm[0][0]/cm[3][3], 2)
acuracy_1 = np.round_(cm[1][1]/cm[3][3], 2)
acuracy_2 = np.round_(cm[2][2]/cm[3][3], 2)
acuracy = (acuracy_0, acuracy_1, acuracy_2)
col1 = np.array(acuracy)

precision_0 = np.round_(cm[0][0]/cm[3][0], 2)
precision_1 = np.round_(cm[1][1]/cm[3][1], 2)
precision_2 = np.round_(cm[2][2]/cm[3][2], 2)
precision = (precision_0, precision_1, precision_2)
col2 = np.array(precision)

recall_0 = np.round_(cm[0][0]/cm[0][3], 2)
recall_1 = np.round_(cm[1][1]/cm[1][3], 2)
recall_2 = np.round_(cm[2][2]/cm[2][3], 2)
recall = (recall_0, recall_1, recall_2)
col3 = np.array(recall)

f_score_0 = np.round_((2*precision_0*recall_0)/(precision_0 + recall_0), 2)
f_score_1 = np.round_((2*precision_1*recall_1)/(precision_1 + recall_1), 2)
f_score_2 = np.round_((2*precision_2*recall_2)/(precision_2 + recall_2), 2)
f_score = (f_score_0, f_score_1, f_score_2)
col4 = np.array(f_score)

d = {'Acuracy': col1, 'Precision':col2, 'Recall':col3, 'F1_score':col4}
measurs = pd.DataFrame(data=d)
measurs.index.name = 'Class_Label'
measurs

为了它的价值,像这样的代码

accuracy_0 = cm[0][0]/cm[3][3]
accuracy_1 = cm[1][1]/cm[3][3]
accuracy_2 = cm[2][2]/cm[3][3]
accuracy = (accuracy_0, accuracy_1, accuracy_2)

可以换成更简洁的

accuracy = cm.diagonal()[:-1]/cm[-1,-1]

因此您可以将代码重写为

cm = confusion_matrix.to_numpy()
diag = cm.diagonal()[:-1]

accuracy  = diag / cm[ -1, -1]
precision = diag / cm[  3,:-1]
recall    = diag / cm[:-1,  3]
f_score   = 2 * precision * recall / (precision + recall)

out = pd.DataFrame({'Accuracy': accuracy, 
                    'Precision': precision, 
                    'Recall': recall,
                    'F-score': f_score}).round(2)

由于您正在使用 pandas 数据框,您可以:

# df is your confusion_matrix
# these are your classes
classes = df.columns[:-1]

# the true positives
true_positives = np.diagonal(df.loc[classes, classes])

accuracy = true_positives / df.loc['All','All']
precisions = true_positives / df.loc['All', classes]
recalls = true_positives / df.loc[classes, 'All']
fscores = 2 * precisions * recalls / (precisions + recalls)

out = pd.DataFrame({'accuracy': accuracy, 
                    'precisions': precisions, 
                    'recall': recalls,
                    'fscore': fscores
                   }, index=classes           # in case your classes are different from 0,1,2,...
                   ).round(2)                 # round all at once

输出:

   accuracy  precisions  recall  fscore
0      0.17        0.38    0.67    0.48
1      0.14        0.38    0.31    0.34
2      0.08        0.43    0.27    0.33