判断分类性能classification_report

在分类任务中,可以用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')
主要参数:
y_true:目标值,格式是列表
y_pred:预测值,格式是列表
labels:类别的取值范围,最后打印出来的就是labels里面的分类。格式是列表。
target_names:在打印报告中显示别名。例如y_true和y_pred都是0和1,但是现在该报告中显示具体的类别名字,就可以把target_names设为['name 0', 'name 1'],注意它的顺序和y_pred的顺序一样。
digits:输出浮点值的位数。

例子:

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

你可能感兴趣的:(判断分类性能classification_report)