当前位置:网站首页>热力图展示相关矩阵
热力图展示相关矩阵
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()
边栏推荐
- Dry and wet contacts
- Registration method of native method in JNI
- Tiktok wallpaper applet v1.0.2 function, new arrival function
- Svg line animation background JS effect
- 【面试题】instancof和getClass()的区别
- The third generation of power electronics semiconductors: SiC MOSFET learning notes (V) research on driving power supply
- C program design topic 18-19 final exam exercise solutions (Part 2)
- Design scheme of authority management of fusion model
- ros(25):rqt_image_view报错Unable to load plugin for transport ‘compressed‘, error string
- C程序设计专题 15-16年期末考试习题解答(上)
猜你喜欢

Svg line animation background JS effect

Unmanned driving: Some Thoughts on multi-sensor fusion

【微服务|Sentinel】实时监控|RT|吞吐量|并发数|QPS

In the process of enterprise development, I found that a colleague used the select * from where condition for update
![[interview question] what is a transaction? What are dirty reads, unrepeatable reads, phantom reads, and how to deal with several transaction isolation levels of MySQL](/img/95/02a58c9dc97bd8347b43247e38357d.png)
[interview question] what is a transaction? What are dirty reads, unrepeatable reads, phantom reads, and how to deal with several transaction isolation levels of MySQL

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

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

ServerSocket and socket connection

2021-11-07

ros(24):error: invalid initialization of reference of type ‘xx’ from expression of type ‘xx’
随机推荐
Adding, deleting, modifying and checking in low build code
Wallpaper applet wechat applet
2021-11-05
Jar package merging using Apache ant
Wx applet jump page
Helm chart仓库操作
On the difficulty of developing large im instant messaging system
Use of navigation and navigationui
Human body transformation vs digital Avatar
Applet opening traffic master
Go crawler framework -colly actual combat (II) -- Douban top250 crawling
Use of JMeter easynmon
After 5 years of software testing in didi and ByteDance, it's too real
@mysql
C program design topic 15-16 final exam exercise solutions (Part 1)
D omit parameter name
无人驾驶: 对多传感器融合的一些思考
[interview question] the difference between instancof and getclass()
Design scheme of authority management of fusion model
The drawableleft of the custom textview in kotlin is displayed in the center together with the text