在主成份分析、数据探索上,经常要探索特征与特征、特征与因变量之间的相关性。下面记录几种相关性探索的方法。
方法一:单个特征与因变量的相关性直观显视
import matplotlib
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')#设置数据可视化主题
names = col_name.copy() # 列名
names.remove('onset_diabetes') # 移除因变量后得到特征
for name in names:
plt.hist(pima[name][pima['onset_diabetes']==0],10,alpha=0.5,label='non_diabetes')
plt.hist(pima[name][pima['onset_diabetes']==1],10,alpha=0.5,label='diabetes')
plt.xlabel(name)
plt.ylabel("frequency")
plt.title('histogram of {}'.format(name))
plt.show()
![](https://i-blog.csdnimg.cn/blog_migrate/2f58469121c3514e0f40145aba241aa4.png)
这里只放出其中一个特征与因变量的相关性。由图可以看出,因变量结果“diabetes”与“non_diabetes”在特征“plasma_glucose_concentration”的分布明显相差很大,所以因变量“onset_diabetes”与特征“plasma_glucose_concentration”的相关性很强。
方法二:相关矩阵
import seaborn as sns
sns.heatmap(pima.corr())
![](https://i-blog.csdnimg.cn/blog_migrate/044aecbe93419acc72cd56cb2f388103.png)
相关系数越大,相关性越强。如上相关矩阵图,可以看到特征times_pregnant与特征age的相关性最强;如果只看特征与因变量之间关系的话,plasma_glucose_concentration与因变量的相关性最强。
方法三:相关系数
print(pima.corr()["onset_diabetes"])
![](https://i-blog.csdnimg.cn/blog_migrate/95b138e5c18c5d162c892f97b0e604b9.png)
这是直接打印相关系数,更准确地表达特征与因变量之间的相关性。