赞
踩
相关系数矩阵与热力图heatmap
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# 生成随机矩阵
df = np.random.randint(-10,10,size=(5,8))
df
array([[-10, -4, 1, 8, 8, -9, -2, -2],
[ -4, -2, 7, -10, -6, 4, 5, -1],
[ 6, 4, 1, -7, 0, 3, 3, 4],
[ 8, -2, 9, -4, 0, 1, 2, 0],
[ 3, 5, -3, 1, -4, -3, 8, 2]])
# 计算相关系数矩阵
corr = np.corrcoef(df)
corr
array([[ 1. , -0.45187846, -0.81601955, -0.41843364, -0.29089038],
[-0.45187846, 1. , 0.4896793 , 0.54083859, 0.02788629],
[-0.81601955, 0.4896793 , 1. , 0.48284327, 0.2963696 ],
[-0.41843364, 0.54083859, 0.48284327, 1. , -0.14536089],
[-0.29089038, 0.02788629, 0.2963696 , -0.14536089, 1. ]])
# 生成热力图
f,ax = plt.subplots(figsize=(10,5))
sns.heatmap(corr,annot=True)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。