当前位置:网站首页>热力图展示相关矩阵
热力图展示相关矩阵
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()
边栏推荐
- Xcode预览(Preview)显示List视图内容的一个Bug及解决
- Apk slimming compression experience
- UE4 WebBrowser chart cannot display problems
- 【微服务|Sentinel】实时监控|RT|吞吐量|并发数|QPS
- Related operations of ansible and Playbook
- JMeter socket connection sends data
- Alternative to log4j
- ros(24):error: invalid initialization of reference of type ‘xx’ from expression of type ‘xx’
- Unmanned driving: Some Thoughts on multi-sensor fusion
- C#和C 的CAN通信实验
猜你喜欢

Usage of ViewModel and livedata in jetpack

Collective example

How to quickly open traffic master for wechat applet

不重要的token可以提前停止计算!英伟达提出自适应token的高效视觉Transformer网络A-ViT,提高模型的吞吐量!...

百公里加速仅5.92秒,威兰达高性能版以高能产品实力领跑

Why are life science enterprises on the cloud in succession?

Go crawler framework -colly actual combat (II) -- Douban top250 crawling

Registration method of native method in JNI

Apk decompiled method (not confused)

Custom animation (simulated win10 loading animation) - Optimization
随机推荐
Custom animation (simulated win10 loading animation) - Optimization
D does not require opapply() as a domain
Collective example
D manual destruction may violate memory security
Wx applet jump page
Custom animation (simulated win10 loading animation)
What is test development? Can you find a job at this stage?
C program design topic 18-19 final exam exercise solutions (Part 2)
redis + lua实现分布式接口限流实现方案
How can I persuade leaders to use DDD to construct the liver project?
Wallpaper applet wechat applet
Uniapp encapsulated incentive advertisement, screen insert advertisement and banner advertisement
The drawableleft of the custom textview in kotlin is displayed in the center together with the text
Is it so difficult to calculate the REM size of the web page according to the design draft?
Use of JMeter
2019 summary and 2020 outlook
Registration method of native method in JNI
Transition from digitalization to intelligent manufacturing
In the process of enterprise development, I found that a colleague used the select * from where condition for update
C程序设计专题 18-19年期末考试习题解答(下)