['precision', 'recall', 'f1-score', 'support', '0', '0.95', '0.96', '0.95', '10762', '1', '0.91', '0.95', '0.93', '9339', '2', '0.91', '0.58', '0.71', '1628', 'micro', 'avg', '0.93', '0.93', '0.93', '21729', 'macro', 'avg', '0.92', '0.83', '0.86', '21729weighted', 'avg', '0.93', '0.93', '0.92', '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-17 15:05:18 浏览: 128
可以使用以下代码将列表转换为字典形式:
```
report = ['precision', 'recall', 'f1-score', 'support', '0', '0.95', '0.96', '0.95', '10762', '1', '0.91', '0.95', '0.93', '9339', '2', '0.91', '0.58', '0.71', '1628', 'micro', 'avg', '0.93', '0.93', '0.93', '21729', 'macro', 'avg', '0.92', '0.83', '0.86', '21729', 'weighted', 'avg', '0.93', '0.93', '0.92', '21729', 'samples', 'avg', '0.93', '0.93', '0.93', '21729']
report1 = {}
for i in range(4, len(report), 5):
report1[report[i]] = {'precision': float(report[i+1]), 'recall': float(report[i+2]), 'f1-score': float(report[i+3]), 'support': int(report[i+4])}
report1['micro avg'] = {'precision': float(report[21]), 'recall': float(report[22]), 'f1-score': float(report[23]), 'support': int(report[24])}
report1['macro avg'] = {'precision': float(report[26]), 'recall': float(report[27]), 'f1-score': float(report[28]), 'support': int(report[29])}
report1['weighted avg'] = {'precision': float(report[31]), 'recall': float(report[32]), 'f1-score': float(report[33]), 'support': int(report[34])}
report1['samples avg'] = {'precision': float(report[36]), 'recall': float(report[37]), 'f1-score': float(report[38]), 'support': int(report[39])}
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.91, 'recall': 0.58, '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.86, 'support': 21729}, 'weighted avg': {'precision': 0.93, 'recall': 0.93, 'f1-score': 0.92, 'support': 21729}, 'samples avg': {'precision': 0.93, 'recall': 0.93, 'f1-score': 0.93, 'support': 21729}}
```
阅读全文