classification_report#

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_true1d array-like, or label indicator array / sparse matrix

真实(正确)目标值。稀疏矩阵仅在目标为 多标签 类型时受支持。

y_pred1d array-like, or label indicator array / sparse matrix

分类器返回的估计目标。稀疏矩阵仅在目标为 多标签 类型时受支持。

labels数组状,形状为 (n_labels,),默认=None

可选的标签索引列表,包含在报告中。

target_names数组状,形状为 (n_labels,),默认=None

可选的显示名称,对应标签(顺序相同)。

sample_weightshape 为 (n_samples,) 的 array-like, default=None

样本权重。

digits整数,默认=2

格式化输出浮点数值的位数。当 output_dictTrue 时,此参数将被忽略,返回的值不会被四舍五入。

output_dict布尔值,默认=False

如果为 True,则返回字典格式的输出。

0.20 版本新增。

zero_division{“warn”, 0.0, 1.0, np.nan}, default=”warn”

当出现零除法时返回的值。如果设置为“warn”,则此行为如同 0,但同时会发出警告。

版本 1.3 中的新增: 添加了 np.nan 选项。

返回:
report字符串或字典

每个类别的精确率、召回率、F1 分数的文本摘要。如果 output_dict 为 True,则返回字典。字典的结构如下:

{'label 1': {'precision':0.5,
             'recall':1.0,
             'f1-score':0.67,
             'support':1},
 'label 2': { ... },
  ...
}

报告的平均值包括宏平均(每种标签的未加权平均值)、加权平均(每种标签的按支持度加权平均值)和样本平均(仅用于多标签分类)。微平均(总真阳性、假阴性和假阳性的平均值)仅对多标签或子集类别的多类分类显示,因为否则它等同于准确率,并且对所有指标都相同。有关平均值的更多详细信息,请参阅 precision_recall_fscore_support

请注意,在二分类中,正类的召回率也称为“灵敏度”;负类的召回率称为“特异度”。

另请参阅

precision_recall_fscore_support

计算每个类别的精确度、召回率、F-measure 和支持度。

confusion_matrix

计算混淆矩阵以评估分类的准确性。

multilabel_confusion_matrix

计算每个类别或样本的混淆矩阵。

示例

>>> from sklearn.metrics import classification_report
>>> y_true = [0, 1, 2, 2, 2]
>>> y_pred = [0, 0, 2, 2, 1]
>>> target_names = ['class 0', 'class 1', 'class 2']
>>> print(classification_report(y_true, y_pred, target_names=target_names))
              precision    recall  f1-score   support

     class 0       0.50      1.00      0.67         1
     class 1       0.00      0.00      0.00         1
     class 2       1.00      0.67      0.80         3

    accuracy                           0.60         5
   macro avg       0.50      0.56      0.49         5
weighted avg       0.70      0.60      0.61         5

>>> y_pred = [1, 1, 0]
>>> 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       1.00      0.67      0.80         3
   macro avg       0.33      0.22      0.27         3
weighted avg       1.00      0.67      0.80         3