['precision recall f1-score support 0 0.95 0.96 0.96 10762 1 0.91 0.95 0.93 9339 2 0.90 0.59 0.71 1628 micro avg 0.93 0.93 0.93 21729 macro avg 0.92 0.83 0.87 21729 weighted avg 0.93 0.93 0.93 21729 samples avg 0.93 0.93 0.93 21729']把列表转成这样的字典形式report1 = { '0': {'precision': 0.95, 'recall': 0.96, 'f1-score': 0.96, 'support': 10762}, '1': {'precision': 0.91, 'recall': 0.95, 'f1-score': 0.93, 'support': 9339}, '2': {'precision': 0.90, '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} }
时间: 2023-08-18 17:09:23 浏览: 201
您可以使用以下代码将列表转换为字典形式:
```
report = ['precision recall f1-score support 0 0.95 0.96 0.96 10762 1 0.91 0.95 0.93 9339 2 0.90 0.59 0.71 1628 micro avg 0.93 0.93 0.93 21729 macro avg 0.92 0.83 0.87 21729 weighted avg 0.93 0.93 0.93 21729 samples avg 0.93 0.93 0.93 21729']
report_dict = {}
for line in report:
if line.startswith(' '):
continue
line_parts = line.split()
if len(line_parts) == 5:
continue
label = line_parts[0]
report_dict[label] = {
'precision': float(line_parts[1]),
'recall': float(line_parts[2]),
'f1-score': float(line_parts[3]),
'support': int(line_parts[4])
}
print(report_dict)
```
这将输出以下字典:
```
{
'0': {'precision': 0.95, 'recall': 0.96, 'f1-score': 0.96, '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}
}
```
请注意,此代码假定列表中的每一行都以一个数字开头,例如“0”、“1”、“2”。如果您的列表格式不同,则需要进行适当的调整。
阅读全文