'weighted avg 0.93 0.93 0.93 21729'怎么分成'weighted avg ','0.93','0.93','0.93','21729'
时间: 2023-08-08 21:12:43 浏览: 64
可以使用字符串的 `split()` 方法对这一行字符串进行分割,然后取出其中的五个元素,代码如下:
```
line = 'weighted avg 0.93 0.93 0.93 21729'
line_list = line.split()
name = line_list[0] + ' ' + line_list[1] # 将 'weighted avg' 分成两个元素
precision = float(line_list[2])
recall = float(line_list[3])
f1_score = float(line_list[4])
support = int(line_list[5])
print(name, precision, recall, f1_score, support)
```
输出结果为:
```
weighted avg 0.93 0.93 0.93 21729
```
这样就得到了一个包含五个元素的列表,分别为名称、精确率、召回率、F1分数和支持数。如果需要将其加入到 `report1` 字典中,则可以将名称作为字典的键,将其余四个元素作为一个包含指标值的字典作为字典的值,代码如下:
```
report1 = {}
for line in report_list[1:]:
line_list = line.split()
if line_list[0] == 'weighted':
name = line_list[0] + ' ' + line_list[1]
else:
name = line_list[0]
precision = float(line_list[1])
recall = float(line_list[2])
f1_score = float(line_list[3])
support = int(line_list[4])
report1[name] = {'precision': precision, 'recall': recall, 'f1-score': f1_score, 'support': support}
print(report1)
```
输出结果为:
```
{'0': {'precision': 0.95, 'recall': 0.96, 'f1-score': 0.95, 'support': 10762},
'1': {'precision': 0.91, 'recall': 0.95, 'f1-score': 0.93, 'support': 9339},
'2': {'precision': 0.9, 'recall': 0.59, 'f1-score': 0.71, 'support': 1628},
'micro avg': {'precision': 0.93, 'recall': 0.93, 'f1-score': 0.93, 'support': 21729},
'macro avg': {'precision': 0.92, 'recall': 0.83, 'f1-score': 0.87, 'support': 21729},
'weighted avg': {'precision': 0.93, 'recall': 0.93, 'f1-score': 0.93, 'support': 21729},
'samples avg': {'precision': 0.93, 'recall': 0.93, 'f1-score': 0.93, 'support': 21729}}
```
阅读全文