修正下列代码merged_report = {} a=0 b=0 c=0 d=0 for i in range(len(report)): for key in report[i].keys(): if key in ['micro avg', 'macro avg', 'weighted avg', 'samples avg']: merged_report[key] = report[i][key] else: a+=report[i][key]['precision'] b+=report[i][key]['recall'] c+=report[i][key]['f1-score'] d+=report[i][key]['support'] merged_report[key] = { 'precision': a / n, 'recall': b / n, 'f1-score': c / n, 'support': d } print(merged_report)
时间: 2024-03-04 14:48:01 浏览: 91
修正后的代码如下:
```
merged_report = {}
a = 0
b = 0
c = 0
d = 0
for i in range(len(report)):
for key in report[i].keys():
if key in ['micro avg', 'macro avg', 'weighted avg', 'samples avg']:
merged_report[key] = report[i][key]
else:
a += report[i][key]['precision']
b += report[i][key]['recall']
c += report[i][key]['f1-score']
d += report[i][key]['support']
n = len(report) - len(['micro avg', 'macro avg', 'weighted avg', 'samples avg'])
if n > 0:
merged_report[key] = {
'precision': a / n,
'recall': b / n,
'f1-score': c / n,
'support': d / n
}
print(merged_report)
```
主要是在计算平均值时,需要先计算类别数,然后再除以类别数,以得到正确的平均值。此外,还需要在计算平均值之前将 a、b、c、d 归零。
阅读全文