【问题标题】:How to get the each class results seperately in multiclass confusion matrix如何在多类混淆矩阵中分别获得每个类的结果
【发布时间】:2022-12-30 00:10:17
【问题描述】:

我在这里有实际的课程和 res 课程 - https://extendsclass.com/csv-editor.html#46eaa9e

我想计算每个 A、N、O 类的敏感性、特异性和阳性预测率。这是我的代码

这是代码

from sklearn.metrics import multilabel_confusion_matrix
import numpy as np

mcm = multilabel_confusion_matrix(act_class, pred_class)

tps = mcm[:, 1, 1]
tns = mcm[:, 0, 0]

recall      = tps / (tps + mcm[:, 1, 0])         # Sensitivity
specificity = tns / (tns + mcm[:, 0, 1])         # Specificity
precision   = tps / (tps + mcm[:, 0, 1])         # PPV

print(recall)
print(specificity)
print(precision)
print(classification_report(act_class, pred_class))

这给了我这样的结果

[0.31818182 0.96186441        nan        nan]
[0.99576271 0.86363636 0.86092715 0.99337748]
[0.95454545 0.96186441 0.         0.        ]
              precision    recall  f1-score   support

           A       0.95      0.32      0.48        66
           N       0.96      0.96      0.96       236
           O       0.00      0.00      0.00         0
           ~       0.00      0.00      0.00         0

    accuracy                           0.82       302
   macro avg       0.48      0.32      0.36       302
weighted avg       0.96      0.82      0.86       302

这里的问题是——我无法清楚地推断出 A、N、O 类中的每一个的 sensitivity, specificity, pos predictivity 是什么。

【问题讨论】:

    标签: machine-learning scikit-learn classification confusion-matrix


    【解决方案1】:

    这可能会更快地从视觉上解释:

    默认情况下,标签应按排序顺序出现(对于您的问题:ANO~)。

    如果您想要不同的顺序,您可以使用 labels= 参数指定一个。以下有两个类,并按以下方式排序:[3, 2]

    from sklearn.metrics import multilabel_confusion_matrix
    from sklearn.metrics import classification_report
    
    y_true = [2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3]
    y_pred = [2, 2, 2, 3, 3, 2, 2, 2, 3, 3, 3, 3]
    
    mcm = multilabel_confusion_matrix(y_true, y_pred, labels=[3, 2])
    tps = mcm[:, 1, 1]
    precision = tps / (tps + mcm[:, 0, 1])
    
    print(precision)
    print(f"Precision class 3: {precision[0]}. Precision class 2: {precision[1]}")
    print(classification_report(y_true, y_pred, labels=[3, 2]))
    

    输出:

    [0.66666667 0.5       ]
    Precision class 3: 0.6666666666666666. Precision class 2: 0.5
                  precision    recall  f1-score   support
    
               3       0.67      0.57      0.62         7
               2       0.50      0.60      0.55         5
    
        accuracy                           0.58        12
       macro avg       0.58      0.59      0.58        12
    weighted avg       0.60      0.58      0.59        12
    

    【讨论】:

      猜你喜欢
      • 2017-09-25
      • 1970-01-01
      • 2021-11-11
      • 2019-01-20
      • 2018-10-21
      • 2014-02-08
      • 2020-06-17
      • 2021-08-01
      • 1970-01-01
      相关资源
      最近更新 更多