|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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() # 展示图像
本帖最后由 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[indices] = True
- # 过滤空值
- mask1 = mask1[1:, :-1]
- corr_NotFraud= corr_NotFraud.iloc[1:, :-1].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[indices] = True
- # 过滤空值
- mask2 = mask2[1:, :-1]
- # 将第二个 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]})`。
给个最佳答案
|
|