混淆矩阵绘制

import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix

# 示例的真实标签和预测标签
true_labels = ['cat', 'dog', 'bird', 'cat', 'dog', 'bird', 'cat', 'bird', 'bird']
predicted_labels = ['cat', 'bird', 'dog', 'cat', 'bird', 'dog', 'cat', 'cat', 'bird']

# 确定所有类别的唯一值
unique_labels = np.unique(true_labels)

# 计算混淆矩阵
cm = confusion_matrix(true_labels, predicted_labels)
print(cm)
# 绘制混淆矩阵的可视化
plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
# 在每个格子上标注数量
rows, cols = cm.shape
for i in range(rows):
    for j in range(cols):
        plt.text(j, i, str(cm[i, j]), ha='center', va='center', color='black')
plt.title('Confusion Matrix')
plt.colorbar()
tick_marks = np.arange(len(unique_labels))
plt.xticks(tick_marks, unique_labels)
plt.yticks(tick_marks, unique_labels)
plt.xlabel('Predicted Label')
plt.ylabel('True Label')
plt.show()

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值