赞
踩
- import pandas as pd
- import numpy as np
- import seaborn as sns
- import matplotlib.pyplot as plt
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
- plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
- plt.figure(figsize=(15,5),dpi=300)
- # 生成示例数据
- np.random.seed(0)
- data = np.random.rand(10, 10) # 替换为您的数据
-
- # 创建数据框
- df = pd.DataFrame(data)
-
- # 计算皮尔逊相关系数
- correlation_matrix = df.corr()
-
- # 绘制热力图
- plt.figure(figsize=(10, 8)) # 设置图形大小
- sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt=".2f", linewidths=0.5)
- plt.title('皮尔逊相关系数热力图')
- x_new=np.arange(0.5,10.5)
- x_label_ticks=["数学","英语","物理","语文","化学","生物","地理","科学","政治","历史"]
- plt.xticks(x_new,x_label_ticks,rotation=0)
- plt.yticks(x_new,x_label_ticks,rotation=0)
- plt.show()
x_label_ticks为所需要绘制的所有特征名,x_new需要改成[0.5,所有特征个数+0.5]
上述代码为完整的皮尔逊相关系数热力图:
下三角热力图:
- import pandas as pd
- import numpy as np
- import seaborn as sns
- import matplotlib.pyplot as plt
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
- plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
- plt.figure(figsize=(15,5),dpi=300)
-
- # 生成示例数据
- np.random.seed(0)
- data = np.random.rand(10, 10) # 替换为您的数据
-
- # 创建数据框
- df = pd.DataFrame(data)
-
- # 计算皮尔逊相关系数
- correlation_matrix = df.corr()
-
- # 创建下三角矩阵
- mask = np.triu(np.ones_like(correlation_matrix, dtype=bool))
- np.fill_diagonal(mask, False)
- #mask = np.tri(len(correlation_matrix), k=-1)
-
- # 绘制热力图
- plt.figure(figsize=(10, 8)) # 设置图形大小
- sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt=".2f", linewidths=0.5, mask=mask)
- plt.title('下三角皮尔逊相关系数热力图')
-
- # 设置刻度标签
- x_new = np.arange(0.5, 10.5)
- x_label_ticks = ["数学", "英语", "物理", "语文", "化学", "生物", "地理", "科学", "政治", "历史"]
- plt.xticks(x_new, x_label_ticks, rotation=0)
- plt.yticks(x_new, x_label_ticks, rotation=0)
-
- plt.show()
若无需对角线自相关,则添加np.fill_diagonal(mask, True)即可:
- import pandas as pd
- import numpy as np
- import seaborn as sns
- import matplotlib.pyplot as plt
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
- plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
- plt.figure(figsize=(15,5),dpi=300)
-
- # 生成示例数据
- np.random.seed(0)
- data = np.random.rand(10, 10) # 替换为您的数据
-
- # 创建数据框
- df = pd.DataFrame(data)
-
- # 计算皮尔逊相关系数
- correlation_matrix = df.corr()
-
- # 创建下三角矩阵
- mask = np.triu(np.ones_like(correlation_matrix, dtype=bool))
- np.fill_diagonal(mask, True)
- #mask = np.tri(len(correlation_matrix), k=-1)
-
- # 绘制热力图
- plt.figure(figsize=(10, 8)) # 设置图形大小
- sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt=".2f", linewidths=0.5, mask=mask)
- plt.title('下三角皮尔逊相关系数热力图')
-
- # 设置刻度标签
- x_new = np.arange(0.5, 10.5)
- x_label_ticks = ["数学", "英语", "物理", "语文", "化学", "生物", "地理", "科学", "政治", "历史"]
- plt.xticks(x_new, x_label_ticks, rotation=0)
- plt.yticks(x_new, x_label_ticks, rotation=0)
-
- plt.show()
上三角热力图:
- import pandas as pd
- import numpy as np
- import seaborn as sns
- import matplotlib.pyplot as plt
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
- plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
- plt.figure(figsize=(15,5),dpi=300)
-
- # 生成示例数据
- np.random.seed(0)
- data = np.random.rand(10, 10) # 替换为您的数据
-
- # 创建数据框
- df = pd.DataFrame(data)
-
- # 计算皮尔逊相关系数
- correlation_matrix = df.corr()
-
- # 创建掩码矩阵
- mask = np.tri(len(correlation_matrix), k=0, dtype=bool)
-
- # 绘制热力图
- plt.figure(figsize=(10, 8)) # 设置图形大小
- sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt=".2f", linewidths=0.5, mask=mask)
- plt.title('上三角皮尔逊相关系数热力图')
-
- # 设置刻度标签
- x_new = np.arange(0.5, 10.5)
- x_label_ticks = ["数学", "英语", "物理", "语文", "化学", "生物", "地理", "科学", "政治", "历史"]
- plt.xticks(x_new, x_label_ticks, rotation=0)
- plt.yticks(x_new, x_label_ticks, rotation=0)
-
- plt.show()
若需要对角列,则numpy的tri函数参数k改成-1即可
- import pandas as pd
- import numpy as np
- import seaborn as sns
- import matplotlib.pyplot as plt
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
- plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
- plt.figure(figsize=(15,5),dpi=300)
-
- # 生成示例数据
- np.random.seed(0)
- data = np.random.rand(10, 10) # 替换为您的数据
-
- # 创建数据框
- df = pd.DataFrame(data)
-
- # 计算皮尔逊相关系数
- correlation_matrix = df.corr()
-
- # 创建下三角矩阵
- mask = np.tri(len(correlation_matrix), k=-1)
-
- # 绘制热力图
- plt.figure(figsize=(10, 8)) # 设置图形大小
- sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt=".2f", linewidths=0.5, mask=mask)
- plt.title('上三角皮尔逊相关系数热力图')
-
- # 设置刻度标签
- x_new = np.arange(0.5, 10.5)
- x_label_ticks = ["数学", "英语", "物理", "语文", "化学", "生物", "地理", "科学", "政治", "历史"]
- plt.xticks(x_new, x_label_ticks, rotation=0)
- plt.yticks(x_new, x_label_ticks, rotation=0)
-
- plt.show()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。