美文网首页
打印classification_report的时候有时候会出现

打印classification_report的时候有时候会出现

作者: 波洛的汽车电子世界 | 来源:发表于2022-07-13 15:12 被阅读0次
    • 问题描述
      在多分类的时候,用sklearn的classification_report会发现有的会打印出micro avg,有的则是accuracy。

    • 例子展示

      • 例子1
    y_pred = [1, 1, 2]
    y_true = [1, 1, 1]
    print(classification_report(y_true, y_pred, labels=[1, 2]))
                     precision    recall  f1-score   support
    
              1       1.00      0.67      0.80         3
              2       0.00      0.00      0.00         0
    
       accuracy                           0.67         3
      macro avg       0.50      0.33      0.40         3
    weighted avg       1.00      0.67      0.80         3
    
    
    • 例子2
    y_pred = [1, 1, 2]
    y_true = [1, 1, 1]
    print(classification_report(y_true, y_pred, labels=[1, 2, 3]))
    
                    precision    recall  f1-score   support
    
               1       1.00      0.67      0.80         3
               2       0.00      0.00      0.00         0
               3       0.00      0.00      0.00         0
    
       micro avg       0.67      0.67      0.67         3
       macro avg       0.33      0.22      0.27         3
    weighted avg       1.00      0.67      0.80         3
    
    • 例子3
    y_pred = [1, 1, 2]
    y_true = [1, 1, 1]
    print(classification_report(y_true, y_pred, labels=[1, 0]))      
    
    
                  precision    recall  f1-score   support
    
               1       1.00      0.67      0.80         3
               0       0.00      0.00      0.00         0
    
       micro avg       1.00      0.67      0.80         3
       macro avg       0.50      0.33      0.40         3
    weighted avg       1.00      0.67      0.80         3
    
    
    • 解决方法和分析

    例子1:labels里面的值和y_pred的值一样,打印出accuracy
    例子2:labels里面有的值没有在y_pred出现
    例子3:y_pred的值没有在labels里面出现
    labels里面的值是我们最后想要打印出precision和recall的类别名字。如果labels和y_pred的取值范围一样,就会打印accuracy,如果不一样,就会打印micro avg。

    相关文章

      网友评论

          本文标题:打印classification_report的时候有时候会出现

          本文链接:https://www.haomeiwen.com/subject/fattirtx.html