[英]How to compute the correlation coefficient between two columns from a data frame?
我想計算來自同一數據幀的兩個不同列之間的相關性。 這是我使用的代碼:
Correlation_unemp_demvote=np.corrcoef(New_table['unemp'],
New_table['demVote'])
Correlation_unemp_demvote
結果如下:
array([[ 1. , 0.34167764],
[ 0.34167764, 1. ]])
我實際上期望得到一個介於 -1 和 1 之間的值,正如實際相關系數定義所解釋的那樣。 你能向我解釋一下我剛剛得到的結果嗎? 我還看到了許多涉及相關性的函數,例如 corr() 或 correlate()。 應該使用哪一種更好?
謝謝,
pd.Series.corr
就是你想要的。
改為這樣做
Correlation_unemp_demvote = New_table['unemp'].corr(New_table['demVote'])
例子
df = pd.DataFrame(np.random.rand(10, 2), columns=list('AB'))
df.A.corr(df.B)
-0.1814956009745472
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.