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')[源代码]#

构建显示主要分类指标的文本报告。

阅读更多的 User Guide .

参数:
y_true1D类数组或标签指示符数组/稀疏矩阵

地面真相(正确)目标值。

y_pred1D类数组或标签指示符数组/稀疏矩阵

分类器返回的估计目标。

labels形状类似数组(n_labels,),默认值=无

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

target_names形状类似数组(n_labels,),默认值=无

与标签匹配的可选显示名称(相同顺序)。

sample_weight形状类似数组(n_samples,),默认=无

样本重量。

digitsint,默认=2

用于格式化输出浮点值的位数。当 output_dictTrue ,这将被忽略并且返回的值不会四舍五入。

output_dict布尔,默认=假

如果为True,则返回输出为dict。

Added in version 0.20.

zero_division{“warn”,0.0,1.0,NP.nan},默认=“warn”

设置存在零除时返回的值。如果设置为“warn”,则该值为0,但也会引发警告。

Added in version 1.3: np.nan 添加了选项。

返回:
report字符串或dict

每个班级的精确度、召回率、F1分数的文本摘要。如果select_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-测度和支持度。

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