鱼C论坛

 找回密码
 立即注册
查看: 2693|回复: 2

[已解决]热力图怎么画

[复制链接]
发表于 2023-6-11 11:10:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
# 绘制热力图,观察各个特征之间的相关性

# 创建图像
grid_kws = {"width_ratios": (.9, .9, .05), "wspace": 0.2}
f, (ax1, ax2, cbar_ax) = plt.subplots(1, 3, gridspec_kw=grid_kws, figsize = (18, 9))
# 定义调色板
cmap = sns.diverging_palette(0, 230, 90, 60, as_cmap=True)

# 画 NotFraud 表中的各个特征间相关性热力图
# 计算上三角mask矩阵
mask1 = np.zeros_like(corr_NotFraud)
indices = np.triu_indices_from(corr_NotFraud)
mask1[indices] = True

# 过滤空值
mask1 = mask1[1:, :-1]
corr_NotFraud = corr_NotFraud.iloc[1:, :-1].copy()
ax1 =sns.???(corr_NotFraud, ax = ax1, vmin = -1, vmax = 1, cmap = cmap, square = False, \
                  annot=True, fmt=".2f", linewidths = 0.5, mask = mask1, cbar = False)
ax1.set_xticklabels(ax1.get_xticklabels(), size = 16);
ax1.set_yticklabels(ax1.get_yticklabels(), size = 16);
ax1.set_title('fraud=1的热力图', size = 20)

# 画 Fraud 表中的各个特征间相关性热力图

# 计算上三角mask矩阵
mask2 = np.zeros_like(corr_Fraud)
indices = np.triu_indices_from(corr_Fraud)
mask2[indices] = True

# 过滤空值
mask2 = mask2[1:, :-1]
corr_Fraud = corr_Fraud.iloc[1:, :-1].copy()

ax2 = sns.???(corr_Fraud, vmin = -1, vmax = 1, cmap = cmap, ax = ax2, square = False, \
                  linewidths = 0.5, mask = mask2, yticklabels = False, \
                  annot=True, fmt=".2f", cbar_ax = cbar_ax, cbar_kws={'orientation': 'vertical',  'ticks': [-1, -0.5, 0, 0.5, 1]})

ax2.set_xticklabels(ax2.get_xticklabels(), size = 16);
ax2.set_title('fraud=0的热力图', size = 20);

cbar_ax.set_yticklabels(cbar_ax.get_yticklabels(), size = 14);

plt.show()    # 展示图像
最佳答案
2023-6-11 11:17:15
本帖最后由 sfqxx 于 2023-6-11 11:38 编辑

在第一个空白处填写`heatmap`函数,在第二个空白处也填写`heatmap`函数即可。

对于第一个热力图,可以直接使用:
  1. sns.heatmap(corr_NotFraud, ax = ax1, vmin = -1, vmax = 1, cmap = cmap, square = False, \
  2.             annot=True, fmt=".2f", linewidths = 0.5, mask = mask1, cbar = False)
复制代码


对于第二个热力图,需要添加colorbar,因此将其赋值给一个变量`ax2`,并在调用`heatmap()`函数时使用其他参数:


  1. ax2 = sns.heatmap(corr_Fraud, vmin = -1, vmax = 1, cmap = cmap, ax = ax2, square = False, \
  2.                   linewidths = 0.5, mask = mask2, yticklabels = False, \
  3.                   annot=True, fmt=".2f", cbar_ax = cbar_ax, cbar_kws={'orientation': 'vertical',  'ticks': [-1, -0.5, 0, 0.5, 1]})
复制代码


完整代码如下:

  1. # 绘制热力图,观察各个特征之间的相关性

  2. # 创建图像
  3. grid_kws = {"width_ratios": (.9, .9, .05), "wspace": 0.2}
  4. f, (ax1, ax2, cbar_ax) = plt.subplots(1, 3, gridspec_kw=grid_kws, figsize = (18, 9))
  5. # 定义调色板
  6. cmap = sns.diverging_palette(0, 230, 90, 60, as_cmap=True)

  7. # 画 NotFraud 表中的各个特征间相关性热力图
  8. # 计算上三角mask矩阵
  9. mask1 = np.zeros_like(corr_NotFraud)
  10. indices = np.triu_indices_from(corr_NotFraud)
  11. mask1[indices] = True

  12. # 过滤空值
  13. mask1 = mask1[1:, :-1]
  14. corr_NotFraud= corr_NotFraud.iloc[1:, :-1].copy()
  15. # 在第一个空白处填写 heatmap 函数
  16. sns.heatmap(corr_NotFraud, ax = ax1, vmin = -1, vmax = 1, cmap = cmap, square = False, \
  17.             annot=True, fmt=".2f", linewidths = 0.5, mask = mask1, cbar = False)

  18. # 画 Fraud 表中的各个特征间相关性热力图
  19. # 计算上三角mask矩阵
  20. mask2 = np.zeros_like(corr_Fraud)
  21. indices = np.triu_indices_from(corr_Fraud)
  22. mask2[indices] = True

  23. # 过滤空值
  24. mask2 = mask2[1:, :-1]

  25. # 将第二个 heatmap 赋值给 ax2,并添加 colorbar
  26. ax2 = sns.heatmap(corr_Fraud, vmin = -1, vmax = 1, cmap = cmap, ax = ax2, square = False, \
  27.                   linewidths = 0.5, mask = mask2, yticklabels = False, \
  28.                   annot=True, fmt=".2f", cbar_ax = cbar_ax, cbar_kws={'orientation': 'vertical',  'ticks': [-1, -0.5, 0, 0.5, 1]})

  29. # 添加标题
  30. ax1.set_title("Correlation matrix of NotFraud")
  31. ax2.set_title("Correlation matrix of Fraud")

  32. # 调整图像
  33. plt.subplots_adjust(wspace = 0.05)
  34. plt.show()
复制代码


其中,第一个空白处填写的是 `sns.heatmap(corr_NotFraud, ax = ax1, vmin = -1, vmax = 1, cmap = cmap, square = False,             annot=True, fmt=".2f", linewidths = 0.5, mask = mask1, cbar = False)`,

第二个空白处填写的是 `sns.heatmap(corr_Fraud, vmin = -1, vmax = 1, cmap = cmap, ax = ax2, square = False,                   linewidths = 0.5, mask = mask2, yticklabels = False,                   annot=True, fmt=".2f", cbar_ax = cbar_ax, cbar_kws={'`orientation': 'vertical',  'ticks': [-1, -0.5, 0, 0.5, 1]})`。

给个最佳答案
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-6-11 11:13:20 | 显示全部楼层
在你的代码中,你想要生成两个热力图来展示数据中特征间的相关性。这个过程中使用了 seaborn 库的热力图函数。不过,在你的代码中有两处 ??? ,我猜测你可能想要调用 seaborn 的 heatmap 函数。下面是修改后的代码:

  1. import numpy as np
  2. import seaborn as sns
  3. import matplotlib.pyplot as plt

  4. # 创建图像
  5. grid_kws = {"width_ratios": (.9, .9, .05), "wspace": 0.2}
  6. f, (ax1, ax2, cbar_ax) = plt.subplots(1, 3, gridspec_kw=grid_kws, figsize = (18, 9))

  7. # 定义调色板
  8. cmap = sns.diverging_palette(0, 230, 90, 60, as_cmap=True)

  9. # 计算上三角mask矩阵
  10. mask1 = np.zeros_like(corr_NotFraud)
  11. indices = np.triu_indices_from(corr_NotFraud)
  12. mask1[indices] = True

  13. # 过滤空值
  14. mask1 = mask1[1:, :-1]
  15. corr_NotFraud = corr_NotFraud.iloc[1:, :-1].copy()
  16. ax1 =sns.heatmap(corr_NotFraud, ax = ax1, vmin = -1, vmax = 1, cmap = cmap, square = False, \
  17.                   annot=True, fmt=".2f", linewidths = 0.5, mask = mask1, cbar = False)
  18. ax1.set_xticklabels(ax1.get_xticklabels(), size = 16);
  19. ax1.set_yticklabels(ax1.get_yticklabels(), size = 16);
  20. ax1.set_title('fraud=1的热力图', size = 20)

  21. # 计算上三角mask矩阵
  22. mask2 = np.zeros_like(corr_Fraud)
  23. indices = np.triu_indices_from(corr_Fraud)
  24. mask2[indices] = True

  25. # 过滤空值
  26. mask2 = mask2[1:, :-1]
  27. corr_Fraud = corr_Fraud.iloc[1:, :-1].copy()

  28. ax2 = sns.heatmap(corr_Fraud, vmin = -1, vmax = 1, cmap = cmap, ax = ax2, square = False, \
  29.                   linewidths = 0.5, mask = mask2, yticklabels = False, \
  30.                   annot=True, fmt=".2f", cbar_ax = cbar_ax, cbar_kws={'orientation': 'vertical',  'ticks': [-1, -0.5, 0, 0.5, 1]})

  31. ax2.set_xticklabels(ax2.get_xticklabels(), size = 16);
  32. ax2.set_title('fraud=0的热力图', size = 20);

  33. cbar_ax.set_yticklabels(cbar_ax.get_yticklabels(), size = 14);

  34. plt.show()    # 展示图像
复制代码


这里需要注意,你需要先定义 corr_NotFraud 和 corr_Fraud 两个数据矩阵。这些矩阵应当由你的数据计算得出,例如可以使用 pandas 库中 DataFrame 的 corr() 函数计算各列之间的相关性。比如: corr_NotFraud = notFraudData.corr() ,其中 notFraudData 是你的非欺诈数据的 DataFrame 。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-11 11:17:15 | 显示全部楼层    本楼为最佳答案   
本帖最后由 sfqxx 于 2023-6-11 11:38 编辑

在第一个空白处填写`heatmap`函数,在第二个空白处也填写`heatmap`函数即可。

对于第一个热力图,可以直接使用:
  1. sns.heatmap(corr_NotFraud, ax = ax1, vmin = -1, vmax = 1, cmap = cmap, square = False, \
  2.             annot=True, fmt=".2f", linewidths = 0.5, mask = mask1, cbar = False)
复制代码


对于第二个热力图,需要添加colorbar,因此将其赋值给一个变量`ax2`,并在调用`heatmap()`函数时使用其他参数:


  1. ax2 = sns.heatmap(corr_Fraud, vmin = -1, vmax = 1, cmap = cmap, ax = ax2, square = False, \
  2.                   linewidths = 0.5, mask = mask2, yticklabels = False, \
  3.                   annot=True, fmt=".2f", cbar_ax = cbar_ax, cbar_kws={'orientation': 'vertical',  'ticks': [-1, -0.5, 0, 0.5, 1]})
复制代码


完整代码如下:

  1. # 绘制热力图,观察各个特征之间的相关性

  2. # 创建图像
  3. grid_kws = {"width_ratios": (.9, .9, .05), "wspace": 0.2}
  4. f, (ax1, ax2, cbar_ax) = plt.subplots(1, 3, gridspec_kw=grid_kws, figsize = (18, 9))
  5. # 定义调色板
  6. cmap = sns.diverging_palette(0, 230, 90, 60, as_cmap=True)

  7. # 画 NotFraud 表中的各个特征间相关性热力图
  8. # 计算上三角mask矩阵
  9. mask1 = np.zeros_like(corr_NotFraud)
  10. indices = np.triu_indices_from(corr_NotFraud)
  11. mask1[indices] = True

  12. # 过滤空值
  13. mask1 = mask1[1:, :-1]
  14. corr_NotFraud= corr_NotFraud.iloc[1:, :-1].copy()
  15. # 在第一个空白处填写 heatmap 函数
  16. sns.heatmap(corr_NotFraud, ax = ax1, vmin = -1, vmax = 1, cmap = cmap, square = False, \
  17.             annot=True, fmt=".2f", linewidths = 0.5, mask = mask1, cbar = False)

  18. # 画 Fraud 表中的各个特征间相关性热力图
  19. # 计算上三角mask矩阵
  20. mask2 = np.zeros_like(corr_Fraud)
  21. indices = np.triu_indices_from(corr_Fraud)
  22. mask2[indices] = True

  23. # 过滤空值
  24. mask2 = mask2[1:, :-1]

  25. # 将第二个 heatmap 赋值给 ax2,并添加 colorbar
  26. ax2 = sns.heatmap(corr_Fraud, vmin = -1, vmax = 1, cmap = cmap, ax = ax2, square = False, \
  27.                   linewidths = 0.5, mask = mask2, yticklabels = False, \
  28.                   annot=True, fmt=".2f", cbar_ax = cbar_ax, cbar_kws={'orientation': 'vertical',  'ticks': [-1, -0.5, 0, 0.5, 1]})

  29. # 添加标题
  30. ax1.set_title("Correlation matrix of NotFraud")
  31. ax2.set_title("Correlation matrix of Fraud")

  32. # 调整图像
  33. plt.subplots_adjust(wspace = 0.05)
  34. plt.show()
复制代码


其中,第一个空白处填写的是 `sns.heatmap(corr_NotFraud, ax = ax1, vmin = -1, vmax = 1, cmap = cmap, square = False,             annot=True, fmt=".2f", linewidths = 0.5, mask = mask1, cbar = False)`,

第二个空白处填写的是 `sns.heatmap(corr_Fraud, vmin = -1, vmax = 1, cmap = cmap, ax = ax2, square = False,                   linewidths = 0.5, mask = mask2, yticklabels = False,                   annot=True, fmt=".2f", cbar_ax = cbar_ax, cbar_kws={'`orientation': 'vertical',  'ticks': [-1, -0.5, 0, 0.5, 1]})`。

给个最佳答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-23 23:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表