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')[source]#
生成显示主要分类指标的文本报告。
在用户指南中阅读更多信息。
- 参数:
- y_true1d array-like, 或标签指示器数组/稀疏矩阵
真实(正确)的目标值。
- y_pred1d array-like, 或标签指示器数组/稀疏矩阵
分类器返回的估计目标。
- labels形状为 (n_labels,) 的 array-like, 默认=None
报告中可选包含的标签索引列表。
- target_names形状为 (n_labels,) 的 array-like, 默认=None
与标签匹配的可选显示名称(顺序相同)。
- sample_weight形状为 (n_samples,) 的 array-like, 默认=None
样本权重。
- digitsint, 默认=2
格式化输出浮点值的位数。当
output_dict
为True
时,此参数将被忽略,返回的值将不会被四舍五入。- output_dictbool, 默认=False
如果为 True,则以字典形式返回输出。
0.20 版本新增。
- zero_division{“warn”, 0.0, 1.0, np.nan}, 默认=”warn”
设置发生零除时返回的值。如果设置为“warn”,则其作用为 0,但也会引发警告。
1.3 版本新增: 添加了
np.nan
选项。
- 返回:
- reportstr 或 dict
各类别精确度、召回率、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