問題描述
在多分類的時候,用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。