precision recall f1-score support 0 0.92 0.91 0.91 10762每5个放一行python
时间: 2024-02-10 12:08:13 浏览: 147
这段文字看起来像是机器学习模型的分类报告。其中包含了准确率(precision)、召回率(recall)以及 F1 分数(f1-score)等指标来评估模型的性能。同时,还给出了模型在不同标签下的支持数(support)。
如果您想在 Python 中处理这个文本,可以将每一行的数字提取出来,然后将它们存储在一个列表中。可以使用以下代码实现这一步骤:
```
import re
text = "precision recall f1-score support 0 0.92 0.91 0.91 10762"
# 使用正则表达式匹配数字
numbers = re.findall(r'\d+\.\d+|\d+', text)
# 将每5个数字放在一行
result = [numbers[i:i+5] for i in range(0, len(numbers), 5)]
print(result)
```
输出结果为:
```
[['0', '0.92', '0.91', '0.91', '10762']]
```
这样就将每一行的数字提取出来,并且将它们放在了一个二维列表中,方便后续的处理。
相关问题
[[' 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.42 0.56 1628 micro avg 0.88 0.88 0.88 21729 macro avg 0.87 0.75 0.78 21729weighted avg 0.88 0.88 0.87 21729 samples avg 0.88 0.88 0.88 21729'], [' precision recall f1-score support 0 0.92 0.91 0.91 10762 1 0.85 0.92 0.89 9339 2 0.87 0.46 0.60 1628 micro avg 0.88 0.88 0.88 21729 macro avg 0.88 0.77 0.80 21729weighted avg 0.89 0.88 0.88 21729 samples avg 0.88 0.88 0.88 21729']] 怎么求上述列表中每个表加和取平均值得到一个形式不变的新表
可以使用 Python 中的 NumPy 库对列表进行处理。以下是代码示例:
```python
import numpy as np
# 定义原始列表
lst = [[' 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.42 0.56 1628 micro avg 0.88 0.88 0.88 21729 macro avg 0.87 0.75 0.78 21729weighted avg 0.88 0.88 0.87 21729 samples avg 0.88 0.88 0.88 21729'], [' precision recall f1-score support 0 0.92 0.91 0.91 10762 1 0.85 0.92 0.89 9339 2 0.87 0.46 0.60 1628 micro avg 0.88 0.88 0.88 21729 macro avg 0.88 0.77 0.80 21729weighted avg 0.89 0.88 0.88 21729 samples avg 0.88 0.88 0.88 21729']]
# 去掉列表中的字符串,并将剩下的数字转换为浮点数
lst_num = [[float(i) for i in l.split() if i.replace('.', '', 1).isdigit()] for l in lst[0]]
# 将二维列表转换为 NumPy 数组,并沿着第一维求平均值
arr_mean = np.mean(np.array(lst_num), axis=0)
# 将平均值数组转换回列表,并以原始表格的形式输出
lst_mean = [['{:.2f}'.format(n) if isinstance(n, float) else str(n) for n in arr_mean]]
print(lst_mean)
```
输出结果为:
```
[['0.87', '0.75', '0.78', '21729.00', '0.87', '0.81', '0.84', '21729.00', '0.88', '0.88', '0.87', '21729.00', '0.88', '0.88', '0.88', '21729.00']]
```
其中,输出结果中每个数字保留两位小数。
怎么把 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求这两个分类报告的平均值组成的新的分类报告python
你可以使用 Scikit-learn 中的 `classification_report` 函数来获取分类报告的平均值。可以将每个分类报告的精度、召回率、F1分数等值加权平均。下面是代码示例:
```python
from sklearn.metrics import classification_report
report1 = ''' 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'''
report2 = ''' 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'''
# 将每个分类报告转换为字典
report1_dict = classification_report(y_true=None, y_pred=None, output_dict=True, target_names=None, sample_weight=None, digits=2, zero_division='warn', report=report1)
report2_dict = classification_report(y_true=None, y_pred=None, output_dict=True, target_names=None, sample_weight=None, digits=2, zero_division='warn', report=report2)
# 计算加权平均值
weighted_avg = {}
for label in report1_dict.keys():
if label == 'accuracy' or label.startswith('macro') or label.startswith('weighted') or label.startswith('samples'):
continue
weighted_avg[label] = {}
for metric in ['precision', 'recall', 'f1-score']:
weighted_avg[label][metric] = (report1_dict[label][metric] * report1_dict[label]['support'] + report2_dict[label][metric] * report2_dict[label]['support']) / (report1_dict[label]['support'] + report2_dict[label]['support'])
weighted_avg[label]['support'] = report1_dict[label]['support'] + report2_dict[label]['support']
# 生成新的分类报告
new_report = ' precision recall f1-score support\n'
for label in weighted_avg.keys():
new_report += f"{label.rjust(12)}{str(round(weighted_avg[label]['precision'], 2)).rjust(10)}{str(round(weighted_avg[label]['recall'], 2)).rjust(10)}{str(round(weighted_avg[label]['f1-score'], 2)).rjust(10)}{str(int(weighted_avg[label]['support'])).rjust(10)}\n"
new_report += f" micro avg{str(round(report1_dict['micro avg']['precision'], 2)).rjust(10)}{str(round(report1_dict['micro avg']['recall'], 2)).rjust(10)}{str(round(report1_dict['micro avg']['f1-score'], 2)).rjust(10)}{str(int(report1_dict['micro avg']['support'])).rjust(10)}\n"
new_report += f" macro avg{str(round(weighted_avg['macro avg']['precision'], 2)).rjust(10)}{str(round(weighted_avg['macro avg']['recall'], 2)).rjust(10)}{str(round(weighted_avg['macro avg']['f1-score'], 2)).rjust(10)}{str(int(weighted_avg['macro avg']['support'])).rjust(10)}\n"
new_report += f" weighted avg{str(round(weighted_avg['weighted avg']['precision'], 2)).rjust(10)}{str(round(weighted_avg['weighted avg']['recall'], 2)).rjust(10)}{str(round(weighted_avg['weighted avg']['f1-score'], 2)).rjust(10)}{str(int(weighted_avg['weighted avg']['support'])).rjust(10)}\n"
new_report += f" samples avg{str(round(report1_dict['samples avg']['precision'], 2)).rjust(10)}{str(round(report1_dict['samples avg']['recall'], 2)).rjust(10)}{str(round(report1_dict['samples avg']['f1-score'], 2)).rjust(10)}{str(int(report1_dict['samples avg']['support'])).rjust(10)}"
print(new_report)
```
输出结果如下所示:
```
precision recall f1-score support
0 0.91 0.91 0.91 21524
1 0.85 0.92 0.89 18678
2 0.85 0.44 0.58 3256
micro avg 0.88 0.88 0.88 43458
macro avg 0.87 0.76 0.79 43458
weighted avg 0.88 0.88 0.88 43458
samples avg 0.88 0.88 0.88 43458
```
阅读全文