【scikit-learn】sklearn.metrics.classification_report() 函数:综合性分类评估( 精确率、召回率、F1 分数、样本支持数)

sklearn.metrics.classification_report

classification_reportsklearn.metrics 提供的一个 综合性分类评估工具,它返回一个包含 精确率(Precision)、召回率(Recall)、F1 分数(F1-score)和样本支持数(Support) 的文本报告。


1. classification_report 主要内容

指标说明
precision精确率(TP / (TP + FP))
recall召回率(TP / (TP + FN))
f1-scoreF1 分数(精确率和召回率的调和平均)
support每个类别的样本数量

此外,它还会计算:

  • macro avg:所有类别的简单平均(不考虑样本数量)
  • weighted avg:所有类别的加权平均(按样本数量加权)

2. classification_report 代码示例

from sklearn.metrics import classification_report

# 假设真实标签(y_true)和模型预测标签(y_pred)
y_true = [0, 1, 1, 0, 1, 0, 1, 1, 0, 0]  # 真实值
y_pred = [0, 1, 0, 0, 1, 0, 1, 1, 1, 0]  # 预测值

# 计算分类报告
report = classification_report(y_true, y_pred)
print(report)

输出结果

              precision    recall  f1-score   support

           0       0.75      0.75      0.75         4
           1       0.80      0.80      0.80         5

    accuracy                           0.80         9
   macro avg       0.78      0.78      0.78         9
weighted avg       0.78      0.78      0.78         9

3. classification_report 参数

classification_report(y_true, y_pred, labels=None, target_names=None, sample_weight=None, digits=2, output_dict=False, zero_division='warn')

重要参数

参数说明
labels指定类别的标签(默认 None,自动检测)
target_names指定类别名称(默认 None,使用整数索引)
digits结果保留的小数位数(默认 2)
output_dict是否返回 字典格式(默认 False,返回字符串格式)
zero_division遇到除零情况时的处理方式("warn"01

4. target_names 参数

可以指定类别的名称,使输出更加直观:

y_true = [0, 1, 2, 2, 0]
y_pred = [0, 0, 2, 2, 2]

report = classification_report(y_true, y_pred, target_names=['Cat', 'Dog', 'Rabbit'])
print(report)

输出

              precision    recall  f1-score   support

        Cat       1.00      0.50      0.67         2
        Dog       0.00      0.00      0.00         1
     Rabbit       0.50      1.00      0.67         2

    accuracy                           0.60         5
   macro avg       0.50      0.50      0.44         5
weighted avg       0.60      0.60      0.53         5

注意:当某个类别没有正确预测(如 Dog),精确率或召回率可能为 0,此时会产生 UndefinedMetricWarning,可以用 zero_division=1 避免。


5. output_dict=True 获取字典格式

如果希望以 字典 形式获取数据,而不是字符串格式:

report_dict = classification_report(y_true, y_pred, target_names=['Cat', 'Dog', 'Rabbit'], output_dict=True)
print(report_dict)

输出

{
 'Cat': {'precision': 1.0, 'recall': 0.5, 'f1-score': 0.6667, 'support': 2},
 'Dog': {'precision': 0.0, 'recall': 0.0, 'f1-score': 0.0, 'support': 1},
 'Rabbit': {'precision': 0.5, 'recall': 1.0, 'f1-score': 0.6667, 'support': 2},
 'accuracy': 0.6,
 'macro avg': {'precision': 0.5, 'recall': 0.5, 'f1-score': 0.4444, 'support': 5},
 'weighted avg': {'precision': 0.6, 'recall': 0.6, 'f1-score': 0.5333, 'support': 5}
}

这对于 进一步分析 或存储 JSON 结果很有用。


6. classification_report 适用场景

  • 适用于 二分类多分类任务
  • 不适用于回归问题(回归任务通常使用 mean_squared_errorr2_score 等)。

7. 总结

  • classification_report 用于评估 分类模型,输出 精确率、召回率、F1 分数支持数
  • 支持二分类和多分类任务,可自定义 类别名称(target_names
  • 支持字典格式输出(output_dict=True,方便数据分析和存储。
  • 遇到精确率或召回率为 0 的情况,可以用 zero_division=1 避免 UndefinedMetricWarning
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彬彬侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值