热力图怎么画
# 绘制热力图,观察各个特征之间的相关性# 创建图像
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 = True
# 过滤空值
mask1 = mask1
corr_NotFraud = corr_NotFraud.iloc.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 = True
# 过滤空值
mask2 = mask2
corr_Fraud = corr_Fraud.iloc.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() # 展示图像 在你的代码中,你想要生成两个热力图来展示数据中特征间的相关性。这个过程中使用了 seaborn 库的热力图函数。不过,在你的代码中有两处 ??? ,我猜测你可能想要调用 seaborn 的 heatmap 函数。下面是修改后的代码:
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# 创建图像
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)
# 计算上三角mask矩阵
mask1 = np.zeros_like(corr_NotFraud)
indices = np.triu_indices_from(corr_NotFraud)
mask1 = True
# 过滤空值
mask1 = mask1
corr_NotFraud = corr_NotFraud.iloc.copy()
ax1 =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)
ax1.set_xticklabels(ax1.get_xticklabels(), size = 16);
ax1.set_yticklabels(ax1.get_yticklabels(), size = 16);
ax1.set_title('fraud=1的热力图', size = 20)
# 计算上三角mask矩阵
mask2 = np.zeros_like(corr_Fraud)
indices = np.triu_indices_from(corr_Fraud)
mask2 = True
# 过滤空值
mask2 = mask2
corr_Fraud = corr_Fraud.iloc.copy()
ax2 = 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]})
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() # 展示图像
这里需要注意,你需要先定义 corr_NotFraud 和 corr_Fraud 两个数据矩阵。这些矩阵应当由你的数据计算得出,例如可以使用 pandas 库中 DataFrame 的 corr() 函数计算各列之间的相关性。比如: corr_NotFraud = notFraudData.corr() ,其中 notFraudData 是你的非欺诈数据的 DataFrame 。 本帖最后由 sfqxx 于 2023-6-11 11:38 编辑
在第一个空白处填写`heatmap`函数,在第二个空白处也填写`heatmap`函数即可。
对于第一个热力图,可以直接使用:
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)
对于第二个热力图,需要添加colorbar,因此将其赋值给一个变量`ax2`,并在调用`heatmap()`函数时使用其他参数:
ax2 = 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]})
完整代码如下:
# 绘制热力图,观察各个特征之间的相关性
# 创建图像
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 = True
# 过滤空值
mask1 = mask1
corr_NotFraud= corr_NotFraud.iloc.copy()
# 在第一个空白处填写 heatmap 函数
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)
# 画 Fraud 表中的各个特征间相关性热力图
# 计算上三角mask矩阵
mask2 = np.zeros_like(corr_Fraud)
indices = np.triu_indices_from(corr_Fraud)
mask2 = True
# 过滤空值
mask2 = mask2
# 将第二个 heatmap 赋值给 ax2,并添加 colorbar
ax2 = 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]})
# 添加标题
ax1.set_title("Correlation matrix of NotFraud")
ax2.set_title("Correlation matrix of Fraud")
# 调整图像
plt.subplots_adjust(wspace = 0.05)
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]})`。
给个最佳答案{:10_254:}
页:
[1]