import matplotlib
matplotlib.use('TkAgg') # 使用Agg后端
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
import seaborn as sns
# 假设的实际标签和预测标签
actual_labels = ['Cat', 'Dog', 'Car', 'Person', 'Cat', 'Dog', 'Car', 'Person', 'Cat', 'Dog']
predicted_labels = ['Dog', 'Dog', 'Car', 'Person', 'Cat', 'Car', 'Car', 'Person', 'Cat', 'Cat']
# 标签列表
labels = ['Cat', 'Dog', 'Car', 'Person']
# 计算混淆矩阵
cm = confusion_matrix(actual_labels, predicted_labels, labels=labels)
# 绘制混淆矩阵
plt.figure(figsize=(8, 6))
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues", xticklabels=labels, yticklabels=labels)
plt.xlabel('Predicted Label')
plt.ylabel('True Label')
plt.title('Confusion Matrix')
plt.show()
多分类混淆矩阵绘制
最新推荐文章于 2024-09-14 22:39:45 发布