sklearn.metrics.classification_report
classification_report
是 sklearn.metrics
提供的一个 综合性分类评估工具,它返回一个包含 精确率(Precision)、召回率(Recall)、F1 分数(F1-score)和样本支持数(Support) 的文本报告。
1. classification_report
主要内容
指标 | 说明 |
---|---|
precision | 精确率(TP / (TP + FP)) |
recall | 召回率(TP / (TP + FN)) |
f1-score | F1 分数(精确率和召回率的调和平均) |
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" 、0 或 1 ) |
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_error
、r2_score
等)。
7. 总结
classification_report
用于评估 分类模型,输出 精确率、召回率、F1 分数 和 支持数。- 支持二分类和多分类任务,可自定义 类别名称(
target_names
)。 - 支持字典格式输出(
output_dict=True
),方便数据分析和存储。 - 遇到精确率或召回率为 0 的情况,可以用
zero_division=1
避免UndefinedMetricWarning
。