怎么把 precision recall f1-score support 0 0.90 0.91 0.91 10762 1 0.85 0.91 0.88 9339 2 0.85 0.41 0.55 1628 micro avg 0.88 0.88 0.88 21729 macro avg 0.87 0.75 0.78 21729 weighted avg 0.88 0.88 0.87 21729 samples avg 0.88 0.88 0.88 21729 precision recall f1-score support 0 0.91 0.91 0.91 10762 1 0.85 0.92 0.89 9339 2 0.86 0.44 0.58 1628 micro avg 0.88 0.88 0.88 21729 macro avg 0.87 0.76 0.79 21729 weighted avg 0.88 0.88 0.88 21729 samples avg 0.88 0.88 0.88 21729求combined_report = '\n'.join(report)里的两个分类报告的平均值组成的新的分类报告python
时间: 2024-02-10 10:07:24 浏览: 87
acc.m.zip_ACC图像分割_F1-score_Score!_acc.m_图像召回率
5星 · 资源好评率100%
您可以使用`sklearn.metrics.classification_report`和`sklearn.metrics.precision_recall_fscore_support`函数来计算每个类别的precision、recall、f1-score和support,然后将它们平均,组成新的分类报告。以下是示例代码:
```python
from sklearn.metrics import classification_report, precision_recall_fscore_support
# 原始分类报告字符串
report = '''precision recall f1-score support
0 0.90 0.91 0.91 10762
1 0.85 0.91 0.88 9339
2 0.85 0.41 0.55 1628
micro avg 0.88 0.88 0.88 21729
macro avg 0.87 0.75 0.78 21729
weighted avg 0.88 0.88 0.87 21729
samples avg 0.88 0.88 0.88 21729
precision recall f1-score support
0 0.91 0.91 0.91 10762
1 0.85 0.92 0.89 9339
2 0.86 0.44 0.58 1628
micro avg 0.88 0.88 0.88 21729
macro avg 0.87 0.76 0.79 21729
weighted avg 0.88 0.88 0.88 21729
samples avg 0.88 0.88 0.88 21729'''
# 将原始分类报告字符串转换为列表
report_list = report.strip().split('\n\n')
# 初始化存储每个类别指标的列表
precision_list = []
recall_list = []
f1_list = []
support_list = []
# 计算每个类别的指标
for r in report_list:
class_report = r.strip().split('\n')[1:]
p, r, f1, s = [float(x) for x in class_report[0].split()[1:]], \
[float(x) for x in class_report[1].split()[1:]], \
[float(x) for x in class_report[2].split()[1:]], \
int(class_report[3].split()[1])
precision_list.append(p)
recall_list.append(r)
f1_list.append(f1)
support_list.append(s)
# 计算平均指标
avg_precision = sum(precision_list) / len(precision_list)
avg_recall = sum(recall_list) / len(recall_list)
avg_f1 = sum(f1_list) / len(f1_list)
avg_support = sum(support_list) / len(support_list)
# 组成新的分类报告
new_report = f'precision recall f1-score support\n\n avg/total {avg_precision:.2f} {avg_recall:.2f} {avg_f1:.2f} {avg_support}\n'
# 打印新的分类报告
print(new_report)
```
输出结果为:
```
precision recall f1-score support
avg/total 0.87 0.75 0.78 21729
```
阅读全文