f1-score 的计算公式
时间: 2025-03-20 12:07:54 AIGC 浏览: 149
### F1-Score 的计算公式
F1-Score 是一种综合评价分类器性能的指标,特别适用于处理类别不平衡的数据集。其定义为精确率(Precision)和召回率(Recall)的调和平均数。
具体来说,F1-Score 的计算公式如下:
\[
F1 = 2 \cdot \frac{Precision \cdot Recall}{Precision + Recall}
\]
其中,
- **Precision** 表示预测为正类的样本中有多少比例实际上是正类[^1]。
\[
Precision = \frac{\text{True Positives (TP)}}{\text{True Positives (TP)} + \text{False Positives (FP)}}
\]
- **Recall** 表示实际为正类的样本中有多少比例被成功预测为正类[^2]。
\[
Recall = \frac{\text{True Positives (TP)}}{\text{True Positives (TP)} + \text{False Negatives (FN)}}
\]
因此,F1-Score 将 Precision 和 Recall 结合起来,提供了一个平衡两者表现的单一数值度量标准。该值范围在 0 到 1 之间,越接近于 1 越好,表示模型在这两方面的表现更优[^3]。
以下是 Python 中实现 F1-Score 计算的一个简单代码示例:
```python
def calculate_f1_score(tp, fp, fn):
precision = tp / (tp + fp) if (tp + fp) != 0 else 0
recall = tp / (tp + fn) if (tp + fn) != 0 else 0
f1_score = 2 * ((precision * recall) / (precision + recall)) if (precision + recall) != 0 else 0
return f1_score
# 示例数据
true_positives = 90
false_positives = 30
false_negatives = 20
f1_result = calculate_f1_score(true_positives, false_positives, false_negatives)
print(f"F1 Score: {f1_result}")
```
阅读全文
相关推荐




















