当前位置:网站首页>热力图展示相关矩阵
热力图展示相关矩阵
2022-06-24 20:02:00 【梦想画家】
量化两个变量之间的关系通常使用皮尔逊相关系数,即衡量变量间的线性相关程度。
相关系数取值范围:[-1,1]:
- -1 表示完全负线性相关
- 0 表示没有线性关系
- 1 表示完全正线性相关
越远离0,相关性越强。当变量数量超过2个时,通常使用相关矩阵表示,即用方阵形式表格表示每对变量之间的相关性。
下面通过Python示例说明如何计算相关矩阵,并使用热力图进行表示。
创建相关矩阵
首先创建示例数据集:
import pandas as pd
data = {'assists': [4, 5, 5, 6, 7, 8, 8, 10],
'rebounds': [12, 14, 13, 7, 8, 8, 9, 13],
'points': [22, 24, 26, 26, 29, 32, 20, 14]
}
df = pd.DataFrame(data, columns=['assists','rebounds','points'])
df
# assist rebounds points
# 0 4 12 22
# 1 5 14 24
# 2 5 13 26
# 3 6 7 26
# 4 7 8 29
# 5 8 8 32
# 6 8 9 20
# 7 10 13 14
下面计算相关矩阵:
# 创建相关矩阵
df.corr()
# assists rebounds points
# assists 1.000000 -0.244861 -0.329573
# rebounds -0.244861 1.000000 -0.522092
# points -0.329573 -0.522092 1.000000
# 创建相关矩阵,保留三位小数
df.corr().round(3)
# assists rebounds points
# assists 1.000 -0.245 -0.330
# rebounds -0.245 1.000 -0.522
# points -0.330 -0.522 1.000
表格对角线的相关系数都为1,表示每个变量都完全自相关。其他数值表示每对变量的相关系数:
assists 和 rebounds 相关系数为 -0.245.
assists 和 points 相关系数为 -0.330.
rebounds 和 points 相关系数为 -0.522.
相关矩阵可视化
下面同构pandas包中的样式选型可视化相关矩阵,使用不同的样式风格:
corr = df.corr()
sns.heatmap(corr, xticklabels=corr.columns, yticklabels=corr.columns, cmap="Blues")
# sns.heatmap(corr, xticklabels=corr.columns, yticklabels=corr.columns, cmap="RdYlGn")
# sns.heatmap(corr, xticklabels=corr.columns, yticklabels=corr.columns, cmap="coolwarm")
# sns.heatmap(corr, xticklabels=corr.columns, yticklabels=corr.columns, cmap="bwr")
# sns.heatmap(corr, xticklabels=corr.columns, yticklabels=corr.columns, cmap="PuOr")
plt.title('相关性热力图')
plt.show()
| 

完整代码
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams['axes.unicode_minus'] = False
data = {'assists': [4, 5, 5, 6, 7, 8, 8, 10],
'rebounds': [12, 14, 13, 7, 8, 8, 9, 13],
'points': [22, 24, 26, 26, 29, 32, 20, 14]
}
df = pd.DataFrame(data, columns=['assists', 'rebounds', 'points'])
corr = df.corr()
sns.heatmap(corr, xticklabels=corr.columns, yticklabels=corr.columns, cmap="Blues")
# sns.heatmap(corr, xticklabels=corr.columns, yticklabels=corr.columns, cmap="RdYlGn")
# sns.heatmap(corr, xticklabels=corr.columns, yticklabels=corr.columns, cmap="coolwarm")
# sns.heatmap(corr, xticklabels=corr.columns, yticklabels=corr.columns, cmap="bwr")
# sns.heatmap(corr, xticklabels=corr.columns, yticklabels=corr.columns, cmap="PuOr")
plt.title('相关性热力图')
plt.show()
边栏推荐
- Related operations of ansible and Playbook
- Svg+js keyboard control path
- Human body transformation vs digital Avatar
- 2021-11-07
- How can I persuade leaders to use DDD to construct the liver project?
- Scrollview height cannot fill full screen
- Custom animation (simulated win10 loading animation) - Optimization
- 腾讯云国际云服务器网络访问丢包问题解决办法
- 打卡smart精灵#1,品牌不缺吸引力,产品本身实力如何?
- C WinForm maximizes occlusion of the taskbar and full screen display
猜你喜欢

Unimportant tokens can be stopped in advance! NVIDIA proposes an efficient visual transformer network a-vit with adaptive token to improve the throughput of the model

VNC viewer remote connection raspberry pie without display

打卡smart精灵#1,品牌不缺吸引力,产品本身实力如何?

Custom animation (simulated win10 loading animation)

2019 summary and 2020 outlook

Do280openshift access control -- encryption and configmap

Meta&伯克利基于池化自注意力机制提出通用多尺度视觉Transformer,在ImageNet分类准确率达88.8%!开源...

Working principle analysis of kubernetes architecture core components
Hyperledger Fabric 2. X dynamic update smart contract

Decoupling pages and components using lifecycle
随机推荐
C程序设计专题 18-19年期末考试习题解答(下)
Registration method of native method in JNI
实现mnist手写数字识别
December 6, 2019 what happens after the browser enters a URL
在企业级开发过程中我发现有位同事用select * from where 条件 for update
Modstart: embrace new technologies and take the lead in supporting laravel 9.0
Microsoft won the title of "leader" in the magic quadrant of Gartner industrial Internet of things platform again!
Virtual machine - network configuration
I suddenly find that the request dependent package in NPM has been discarded. What should I do?
Helm chart warehouse operation
Unimportant tokens can be stopped in advance! NVIDIA proposes an efficient visual transformer network a-vit with adaptive token to improve the throughput of the model
How to quickly open traffic master for wechat applet
Go crawler framework -colly actual combat (II) -- Douban top250 crawling
ros(25):rqt_image_view报错Unable to load plugin for transport ‘compressed‘, error string
Use and click of multitypeadapter in recycleview
Signal integrity (SI) power integrity (PI) learning notes (I) introduction to signal integrity analysis
Difficult and miscellaneous problems: A Study on the phenomenon of text fuzziness caused by transform
百公里加速仅5.92秒,威兰达高性能版以高能产品实力领跑
vim使用命令
Dry and wet contacts