在主成份分析、數據探索上,經常要探索特徵與特徵、特徵與因變量之間的相關性。下面記錄幾種相關性探索的方法。
方法一:單個特徵與因變量的相關性直觀顯視
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()
這裏只放出其中一個特徵與因變量的相關性。由圖可以看出,因變量結果“diabetes”與“non_diabetes”在特徵“plasma_glucose_concentration”的分佈明顯相差很大,所以因變量“onset_diabetes”與特徵“plasma_glucose_concentration”的相關性很強。
方法二:相關矩陣
import seaborn as sns
sns.heatmap(pima.corr())
相關係數越大,相關性越強。如上相關矩陣圖,可以看到特徵times_pregnant與特徵age的相關性最強;如果只看特徵與因變量之間關係的話,plasma_glucose_concentration與因變量的相關性最強。
方法三:相關係數
print(pima.corr()["onset_diabetes"])
這是直接打印相關係數,更準確地表達特徵與因變量之間的相關性。