200722-Seaborn绘制热力混淆矩阵自定义刻度

本文介绍如何使用Python的sklearn、matplotlib、seaborn等库绘制混淆矩阵和数据热力图,通过实例展示了混淆矩阵和热力图的生成过程,包括数据准备、图表配置及展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

sns.set()

y_test = np.random.randint(1,8,100)
y_pred = np.random.randint(1,8,100)


fig = plt.figure()
ax = fig.add_subplot(111)
conf_mat = confusion_matrix(y_test, y_pred)
sns.heatmap(conf_mat,annot=True,xticklabels=['0','1','2','3','4','5','8'],yticklabels=['0','1','2','3','4','5','8']) #画热力图
ax.set_title('confusion matrix') #标题
ax.set_xlabel('predict') #x轴
ax.set_ylabel('true') #y轴
plt.show()

在这里插入图片描述

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.pyplot as plt



data = [[89.65, 90.47, 87.99, 89.51, 89.13, 88.85],
        [89.41, 88.70, 86.06, 89.81, 89.79, 89.73],
        [89.38, 90.72, 87.40, 89.91, 91.30, 89.68],
        [89.04, 89.51, 89.46, 88.81, 89.63, 89.39],
        [90.35, 88.36, 89.66, 88.67, 90.46, 89.21],
        [89.90, 89.94, 90.94, 83.64, 89.27, 90.79]]
data = np.array(data)
xticks = [1,4,16,64,26,1024]
yticks = [1,4,16,64,26,1024]


fig, ax = plt.subplots(figsize=(6,4)) 
ax = sns.heatmap(data, 
                cmap="YlGnBu", 
                annot=True, 
                fmt=".2f", 
                linewidths=2, 
                square=True,
                xticklabels=xticks,
                yticklabels=yticks,)

ax.set_xlabel('Target number $\ell_T$', family='Arial')
ax.set_ylabel('Source number $\ell_S$', family='Arial')
ax.set_title('Max:91.30, Min:83.64, Mean:89.29, Std:1.37.', family='Arial')
plt.tight_layout()
plt.savefig('Heatmap.png', dpi=300)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GuokLiu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值