在分類任務(wù)中,可以用classification_report生成每個類別的precision,recall和F1值。
方法:
sklearn.metrics.classification_report(y_true, y_pred, labels=None, target_names=None, sample_weight=None, digits=2, output_dict=False, zero_division='warn')
主要參數(shù):
y_true:目標(biāo)值,格式是列表
y_pred:預(yù)測值,格式是列表
labels:類別的取值范圍,最后打印出來的就是labels里面的分類。格式是列表。
target_names:在打印報告中顯示別名。例如y_true和y_pred都是0和1,但是現(xiàn)在該報告中顯示具體的類別名字,就可以把target_names設(shè)為['name 0', 'name 1'],注意它的順序和y_pred的順序一樣。
digits:輸出浮點值的位數(shù)。
例子:
from sklearn.metrics import classification_report
y_true = [1, 0, 1, 2, 2]
y_pred = [0, 1, 1, 2, 1]
target_names = ['label 1', 'label 0', 'label 2']
print(classification_report(y_true, y_pred, target_names=target_names,labels=[0, 1, 2]))
precision recall f1-score support
label 1 0.00 0.00 0.00 1
label 0 0.33 0.50 0.40 2
label 2 1.00 0.50 0.67 2
accuracy 0.40 5
macro avg 0.44 0.33 0.36 5
weighted avg 0.53 0.40 0.43 5
官方參考文檔:
https://scikit-learn.org/stable/modules/generated/sklearn.metrics.classification_report.html