打印classification_report的時候有時候會出現(xiàn)accuracy,有時候會打印出Micro avg

  • 問題描述
    在多分類的時候,用sklearn的classification_report會發(fā)現(xiàn)有的會打印出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出現(xiàn)
例子3:y_pred的值沒有在labels里面出現(xiàn)
labels里面的值是我們最后想要打印出precision和recall的類別名字。如果labels和y_pred的取值范圍一樣,就會打印accuracy,如果不一樣,就會打印micro avg。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容