1. 程式人生 > 程式設計 >python seaborn heatmap視覺化相關性矩陣例項

python seaborn heatmap視覺化相關性矩陣例項

方法

import pandas as pd
import numpy as np
import seaborn as sns
df = pd.DataFrame(np.random.randn(50).reshape(10,5))
corr = df.corr()
sns.heatmap(corr,cmap='Blues',annot=True)

python seaborn heatmap視覺化相關性矩陣例項

將矩陣型簡化為對角矩陣型:

mask = np.zeros_like(corr)
mask[np.tril_indices_from(mask)] = True
sns.heatmap(corr,annot=True,mask=mask.T)

python seaborn heatmap視覺化相關性矩陣例項

補充知識:Python【相關矩陣】和【協方差矩陣】

相關係數矩陣

pandas.DataFrame(資料).corr()

import pandas as pd
df = pd.DataFrame({
  'a': [11,22,33,44,55,66,77,88,99],'b': [10,24,30,48,50,72,70,96,90],'c': [91,79,58,53,47,34,16,10],'d': [99,10,98,17,89,10]})
df_corr = df.corr()
# 視覺化
import matplotlib.pyplot as mp,seaborn
seaborn.heatmap(df_corr,center=0,cmap='YlGnBu')
mp.show()

python seaborn heatmap視覺化相關性矩陣例項

協方差矩陣

numpy.cov(資料)

import numpy as np
matric = [
  [11,[10,[91,[55,20,19,14]]
covariance_matrix = np.cov(matric)
# 視覺化
print(covariance_matrix)
import matplotlib.pyplot as mp,seaborn
seaborn.heatmap(covariance_matrix,xticklabels=list('abcd'),yticklabels=list('ABCD'))
mp.show()

python seaborn heatmap視覺化相關性矩陣例項

補充

協方差

python seaborn heatmap視覺化相關性矩陣例項

相關係數

python seaborn heatmap視覺化相關性矩陣例項

EXCEL也能做

CORREL函式

python seaborn heatmap視覺化相關性矩陣例項

以上這篇python seaborn heatmap視覺化相關性矩陣例項就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。