一、问题描述
计算一个样本这种属性之间的关系热图
以LFWA为例,一共有13143个样本,每个样本有40 个属性(每一个属性取值为0或1)。那么可以获得一个矩阵[13143,40],计算这个矩阵的协方差,并使用seanborn画出热图。
二、代码如下:
import numpy as np
import os
import seaborn as sns
import matplotlib.pyplot as plt
from collections import Counter
from pandas import DataFrame
textpath='/data3/mcs/VideoRecognition/360/LFWA/label.txt'
with open(textpath, 'r') as fd:
imgs = [list(map(int, x.rstrip().split())) for x in fd.readlines()]
imgs=np.array(imgs).astype(float)
print(imgs.shape)
df = DataFrame(imgs)
corr=df.corr()
plt.figure(figsize=(10,10))
mask=np.zeros_like(corr, dtype=np.bool)
mask[np.triu_indices_from(mask)]=True #upper-triangle of mask
cmap=sns.diverging_palette(220,10,as_cmap=True)#色度 饱和度 亮度 生成调色板
sns.heatmap(corr, mask=mask, cmap=cmap, vmax=0.7, square=True, xticklabels=1, yticklabels=1, linewidths=.5, cbar_kws={"shrink":.5})
plt.yticks(rotation=0)
plt.show()
三、效果