sklearn.metrics.f1_score
f1_score
是 sklearn.metrics
提供的一个 分类模型评估指标,用于计算 F1 分数(F1-score),适用于 二分类、多分类和多标签分类任务。
1. f1_score
计算公式
F1 分数是 精确率(Precision) 和 召回率(Recall) 的 调和平均数:
F
1
=
2
×
Precision
×
Recall
Precision
+
Recall
F1 = 2 \times \frac{\text{Precision} \times \text{Recall}}{\text{Precision} + \text{Recall}}
F1=2×Precision+RecallPrecision×Recall
其中:
- 精确率(Precision) = T P T P + F P \frac{TP}{TP + FP} TP+FPTP
- 召回率(Recall) = T P T P + F N \frac{TP}{TP + FN} TP+FNTP
解释
- F1 分数越高,表示模型在精确率和召回率之间取得了更好的平衡。
- 适用于 精确率和召回率都很重要 的任务,例如 垃圾邮件检测、医学诊断、欺诈检测。
2. f1_score
代码示例
from sklearn.metrics import f1_score
# 真实标签 & 预测标签
y_true = [0, 1, 1, 0, 1, 0, 1, 1, 0, 0] # 真实值
y_pred = [0, 1, 0, 0, 1, 0, 1, 1, 1, 0] # 预测值
# 计算 F1 分数
f1 = f1_score(y_true, y_pred)
print(f"F1 分数: {f1:.2f}")
输出
F1 分数: 0.75
解释
Precision = 3 / (3 + 1) = 0.75
Recall = 3 / (3 + 1) = 0.75
F1 = 2 * (0.75 * 0.75) / (0.75 + 0.75) = 0.75
3. f1_score
在多分类问题中的计算
对于 多分类任务,有多种计算方式:
- macro(宏平均):计算 每个类别的 F1 分数,然后取 平均值(所有类别权重相等)。
- micro(微平均):计算 所有 TP、FP、FN 总数,再计算 F1 分数(适用于类别不均衡数据)。
- weighted(加权平均):根据 每个类别的样本数 计算加权平均 F1 分数(推荐用于数据不均衡)。
- samples(样本平均):适用于 多标签分类,计算每个样本的 F1 分数,再取平均值。
代码示例
y_true = [0, 1, 2, 2, 0]
y_pred = [0, 0, 2, 2, 2]
print("Macro 平均:", f1_score(y_true, y_pred, average='macro'))
print("Micro 平均:", f1_score(y_true, y_pred, average='micro'))
print("Weighted 平均:", f1_score(y_true, y_pred, average='weighted'))
输出
Macro 平均: 0.61
Micro 平均: 0.60
Weighted 平均: 0.73
macro
适用于类别均衡数据,micro
适用于类别不均衡数据,weighted
适用于类别分布不均衡的数据。
4. f1_score
的参数
f1_score(y_true, y_pred, labels=None, pos_label=1, average='binary', sample_weight=None, zero_division='warn')
参数 | 说明 |
---|---|
y_true | 真实标签 |
y_pred | 预测标签 |
labels | 指定计算哪些类别的 F1 分数 |
pos_label | 指定正类(默认为 1 ,仅适用于二分类) |
average | binary (二分类),macro ,micro ,weighted ,samples |
sample_weight | 指定样本权重 |
zero_division | 处理 除零错误(0 、1 或 "warn" ) |
(1) pos_label
指定正类
对于二分类任务,可以使用 pos_label
指定 正类:
y_true = [0, 0, 1, 1]
y_pred = [1, 0, 1, 1]
print("正类 = 1:", f1_score(y_true, y_pred, pos_label=1))
print("正类 = 0:", f1_score(y_true, y_pred, pos_label=0))
输出
正类 = 1: 1.00
正类 = 0: 0.67
(2) 处理 zero_division
当模型 从未预测某个类别 时,可能会遇到 除零错误:
y_true = [0, 0, 0, 0]
y_pred = [1, 1, 1, 1]
# 避免报错
f1 = f1_score(y_true, y_pred, zero_division=1)
print(f"F1 分数(避免除零错误): {f1:.2f}")
5. f1_score
适用场景
适用于 二分类、多分类任务,特别是以下情况:
- 当精确率(Precision)和召回率(Recall)都很重要时
- 垃圾邮件检测(需要避免误报和漏报)
- 欺诈检测(需要减少误报和漏报)
- 医学诊断(需要平衡误诊和漏诊)
对于类别不均衡的数据集,F1 分数比 普通准确率(accuracy_score) 更可靠。
6. f1_score
vs. precision_score
vs. recall_score
指标 | 计算公式 | 适用场景 |
---|---|---|
precision_score | T P / ( T P + F P ) TP / (TP + FP) TP/(TP+FP) | 关注 减少误报(垃圾邮件检测) |
recall_score | T P / ( T P + F N ) TP / (TP + FN) TP/(TP+FN) | 关注 减少漏报(癌症筛查) |
f1_score | 2 × ( P r e c i s i o n × R e c a l l ) / ( P r e c i s i o n + R e c a l l ) 2 \times (Precision \times Recall) / (Precision + Recall) 2×(Precision×Recall)/(Precision+Recall) | 综合平衡 Precision 和 Recall |
示例:
from sklearn.metrics import precision_score, recall_score
print("精确率:", precision_score(y_true, y_pred))
print("召回率:", recall_score(y_true, y_pred))
print("F1 分数:", f1_score(y_true, y_pred))
7. 总结
f1_score
计算 精确率和召回率的调和平均,适用于 二分类和多分类任务。- 适用于 平衡误报(FP)和漏报(FN) 的任务,如 垃圾邮件检测、医学诊断、欺诈检测。
- 支持 macro、micro、weighted 计算方式,适用于不同类别分布的数据集。
- 当数据类别不均衡时,F1 分数比普通准确率更可靠。