精度和召回率(如果不是二进制)

数据挖掘 机器学习 混淆矩阵
2021-09-30 01:21:40

我必须为一个大学项目计算精度和召回率,以测量分类输出的质量(使用 sklearn)。说这将是我的结果:

y_true = [0, 1, 2, 1, 1]
y_pred = [0, 2, 1, 2, 1]

混淆矩阵:
[1 0 0]
[0 1 2]
[0 1 0]

我已经阅读过它,并且该定义在二进制设置中对我来说是有意义的,但是对于 3 个标签,我发现很难解释精度/召回率。
如果我使用 sklearn.metrics.precision/recall_score 它给我 0.4 两者(平均 = 微)

现在对于精度来说,这有点道理,因为 5 个中有 2 个被正确分类。但是我在解释 0.4 的召回结果时遇到了问题。

3个回答

sklearn.metrics.classification_report提供所有类的精度和召回率以及 F 分数和支持。在您的 3 个类的情况下,它可能会被证明是有帮助的。

from sklearn.metrics import recall_score

如果您随后致电(或直接阅读此处recall_score.__dir__的文档),您会看到召回是

召回率tp / (tp + fn)tp真阳性fn数和假阴性数的比率

如果你去他们定义的地方micro,它会说

'micro':通过计算总的真阳性、假阴性和假阳性来全局计算指标

在这里,真正的积极因素是 2 (对角线上各项的总和,也称为迹线),而假阴性加上假阳性(非对角项)的总和是 3.

作为 2/5=.4,召回(使用micro参数average)确实是.4.

请注意,使用micro、精度和召回率是相同的。实际上,以下内容不返回任何内容:

from numpy import random
from sklearn.metrics import recall_score, precision_score
for i in range(100):
    y_pred = random.randint(0, 3, 5)
    y_true = random.randint(0, 3, 5)
    if recall_score(y_pred, y_true, average='micro') != precision_score(y_pred, y_true, average='micro'):
        print(i)

您还可以使用PyCM lib 进行多类混淆矩阵分析。

你的问题 :

>>> print(cm)
Predict          0        1        2        
Actual
0                1        0        0        
1                0        1        2        
2                0        1        0        




Overall Statistics : 

95% CI                                                           (-0.02941,0.82941)
Bennett_S                                                        0.1
Chi-Squared                                                      6.66667
Chi-Squared DF                                                   4
Conditional Entropy                                              0.55098
Cramer_V                                                         0.8165
Cross Entropy                                                    1.52193
Gwet_AC1                                                         0.13043
Joint Entropy                                                    1.92193
KL Divergence                                                    0.15098
Kappa                                                            0.0625
Kappa 95% CI                                                     (-0.60846,0.73346)
Kappa No Prevalence                                              -0.2
Kappa Standard Error                                             0.34233
Kappa Unbiased                                                   0.03226
Lambda A                                                         0.5
Lambda B                                                         0.66667
Mutual Information                                               0.97095
Overall_ACC                                                      0.4
Overall_RACC                                                     0.36
Overall_RACCU                                                    0.38
PPV_Macro                                                        0.5
PPV_Micro                                                        0.4
Phi-Squared                                                      1.33333
Reference Entropy                                                1.37095
Response Entropy                                                 1.52193
Scott_PI                                                         0.03226
Standard Error                                                   0.21909
Strength_Of_Agreement(Altman)                                    Poor
Strength_Of_Agreement(Cicchetti)                                 Poor
Strength_Of_Agreement(Fleiss)                                    Poor
Strength_Of_Agreement(Landis and Koch)                           Slight
TPR_Macro                                                        0.44444
TPR_Micro                                                        0.4

Class Statistics :

Classes                                                          0                       1                       2                       
ACC(Accuracy)                                                    1.0                     0.4                     0.4                     
BM(Informedness or bookmaker informedness)                       1.0                     -0.16667                -0.5                    
DOR(Diagnostic odds ratio)                                       None                    0.5                     0.0                     
ERR(Error rate)                                                  0.0                     0.6                     0.6                     
F0.5(F0.5 score)                                                 1.0                     0.45455                 0.0                     
F1(F1 score - harmonic mean of precision and sensitivity)        1.0                     0.4                     0.0                     
F2(F2 score)                                                     1.0                     0.35714                 0.0                     
FDR(False discovery rate)                                        0.0                     0.5                     1.0                     
FN(False negative/miss/type 2 error)                             0                       2                       1                       
FNR(Miss rate or false negative rate)                            0.0                     0.66667                 1.0                     
FOR(False omission rate)                                         0.0                     0.66667                 0.33333                 
FP(False positive/type 1 error/false alarm)                      0                       1                       2                       
FPR(Fall-out or false positive rate)                             0.0                     0.5                     0.5                     
G(G-measure geometric mean of precision and sensitivity)         1.0                     0.40825                 0.0                     
LR+(Positive likelihood ratio)                                   None                    0.66667                 0.0                     
LR-(Negative likelihood ratio)                                   0.0                     1.33333                 2.0                     
MCC(Matthews correlation coefficient)                            1.0                     -0.16667                -0.40825                
MK(Markedness)                                                   1.0                     -0.16667                -0.33333                
N(Condition negative)                                            4                       2                       4                       
NPV(Negative predictive value)                                   1.0                     0.33333                 0.66667                 
P(Condition positive)                                            1                       3                       1                       
POP(Population)                                                  5                       5                       5                       
PPV(Precision or positive predictive value)                      1.0                     0.5                     0.0                     
PRE(Prevalence)                                                  0.2                     0.6                     0.2                     
RACC(Random accuracy)                                            0.04                    0.24                    0.08                    
RACCU(Random accuracy unbiased)                                  0.04                    0.25                    0.09                    
TN(True negative/correct rejection)                              4                       1                       2                       
TNR(Specificity or true negative rate)                           1.0                     0.5                     0.5                     
TON(Test outcome negative)                                       4                       3                       3                       
TOP(Test outcome positive)                                       1                       2                       2                       
TP(True positive/hit)                                            1                       1                       0                       
TPR(Sensitivity, recall, hit rate, or true positive rate)        1.0                     0.33333                 0.0