計算DataFrame列之間的相關係數
a = np.arange(1,10).reshape(3,3)
data = DataFrame(a,index=["a","b","c"],columns=["one","two","three"])
print(data)
'''
one two three
a 1 2 3
b 4 5 6
c 7 8 9
'''
#計算第一列和第二列的相關係數
print(data.one.corr(data.two))
#1.0
#返回一個相關係數矩陣
print(data.corr())
'''
one two three
one 1.0 1.0 1.0
two 1.0 1.0 1.0
three 1.0 1.0 1.0
'''
#計算第一列和第二列的協方差
print(data.one.cov(data.two))
#9.0
#返回一個協方差矩陣
print(data.cov())
'''
one two three
one 9.0 9.0 9.0
two 9.0 9.0 9.0
three 9.0 9.0 9.0
'''
相關性係數(pearson、spearman、kendall,pointbiserialr)
series.corr(other[, method, min_periods])
用途:
檢查兩個變量之間變化趨勢的方向以及程度,值範圍-1到+1,0表示兩個變量不相關,正值表示正相關,負值表示負相關,值越大相關性越強。
計算積距pearson相關係數,連續性變量纔可採用;計算Spearman秩相關係數,適合於定序變量或不滿足正態分佈假設的等間隔數據; 計算Kendall秩相關係數,適合於定序變量或不滿足正態分佈假設的等間隔數據。
1pearson correlation coefficient(皮爾森相關性係數)
公式可以化簡爲:
連續數據,正態分佈,線性關係,用pearson相關係數是最恰當
spearman相關係數
不滿足連續數據,正態分佈,線性關係,用spearman相關係數是最恰,當兩個定序測量數據之間也用spearman相關係數。