怎么将跑出的实际的precision、recall、f1-score、support带入
时间: 2024-01-25 08:05:17 浏览: 102
你可以将实际的precision、recall、f1-score和support值以列表或数组的形式传递给代码中的对应变量,然后运行代码生成柱状图。以下是修改后的示例代码:
```python
import matplotlib.pyplot as plt
import numpy as np
# 实际的precision、recall、f1-score和support值
models = ['RandomForest', 'LogisticRegression', 'NaiveBayes', 'DecisionTree', 'KNN']
precision = [0.8, 0.7, 0.85, 0.75, 0.9]
recall = [0.85, 0.75, 0.8, 0.9, 0.85]
f1_score = [0.82, 0.72, 0.83, 0.78, 0.88]
support = [30, 35, 40, 45, 50]
# 设置柱状图的颜色
colors = ['red', 'green', 'blue', 'yellow', 'orange']
# 绘制柱状图
fig, ax = plt.subplots()
bar_width = 0.2
index = np.arange(len(models))
rects1 = ax.bar(index, precision, bar_width, color=colors[0], label='Precision')
rects2 = ax.bar(index + bar_width, recall, bar_width, color=colors[1], label='Recall')
rects3 = ax.bar(index + 2 * bar_width, f1_score, bar_width, color=colors[2], label='F1-score')
rects4 = ax.bar(index + 3 * bar_width, support, bar_width, color=colors[3], label='Support')
# 设置x轴标签倾斜
ax.set_xticks(index + 2 * bar_width)
ax.set_xticklabels(models, rotation=45, ha='right')
# 设置图例
ax.legend()
# 显示网格线
ax.grid(True)
# 添加标题和轴标签
plt.title('Comparison of Performance Metrics')
plt.xlabel('Models')
plt.ylabel('Scores')
# 显示图形
plt.tight_layout()
plt.show()
```
在这个示例代码中,我将实际的precision、recall、f1-score和support值替换了之前的随机示例数据。你可以根据你的实际数据进行相应的替换,然后运行代码生成对应的柱状图。
阅读全文