Pandas中有pandas.DataFrame.corr和pandas.Series.corr兩個(gè)方法進(jìn)行相關(guān)性的計(jì)算砾层,第一個(gè)針對(duì)整個(gè)dataframe數(shù)據(jù)返回一個(gè)矩陣,第二個(gè)針對(duì)不同的column。下面對(duì)第二個(gè)方法的函數(shù)內(nèi)容、使用方法以及重要的顯著性檢驗(yàn)三個(gè)方面進(jìn)行介紹。
第一部分:相關(guān)性系數(shù)計(jì)算方法及相應(yīng)函數(shù)介紹
Pandas函數(shù):
Series.corr
(self, other, method='pearson', min_periods=None)[source]
參數(shù):
otherSeries: 需要用來(lái)計(jì)算相關(guān)性的Series
method{‘pearson’, ‘kendall’, ‘spearman’} :用來(lái)計(jì)算相關(guān)性的方法啡直,可以是這三種,或者其他的可調(diào)動(dòng)函數(shù)苍碟,默認(rèn)是皮爾森相關(guān)系數(shù)。
min_periodsint:可選參數(shù)撮执,為了獲得有效結(jié)果所需要的最小的觀察數(shù)量
返回:float類(lèi)型的相關(guān)系數(shù)
主要參數(shù)methods介紹:
- pearson correlation coefficient(皮爾遜相關(guān)性系數(shù))微峰。
常用的相關(guān)系數(shù)求法,采用協(xié)方差cov(X,Y)/標(biāo)準(zhǔn)差的乘積(σX, σY)抒钱。
數(shù)據(jù)要求: 線性數(shù)據(jù)蜓肆、連續(xù)且符合正態(tài)分布;數(shù)據(jù)間差異不能太大谋币;變量準(zhǔn)差不能為0仗扬,即兩變量中任何一個(gè)值不能都是相同。 - spearman correlation coefficient(斯皮爾曼秩相關(guān)性系數(shù))蕾额。
根據(jù)原始數(shù)據(jù)的排序位置進(jìn)行計(jì)算早芭。
數(shù)據(jù)要求:用于解決稱(chēng)名數(shù)據(jù)和順序數(shù)據(jù)相關(guān)的問(wèn)題,適用于兩列變量诅蝶,而且具有等級(jí)變量性質(zhì)具有線性關(guān)系的數(shù)據(jù)退个,能夠很好處理序列中相同值和異常值。 - kendall correlation coefficient(肯德?tīng)栂嚓P(guān)性系數(shù))调炬。
等級(jí)相關(guān)系數(shù)语盈,適用于兩個(gè)變量均為有序分類(lèi)的情況
數(shù)據(jù)要求:肯德?tīng)栂嚓P(guān)性系數(shù),它也是一種秩相關(guān)系數(shù)缰泡,不過(guò)它所計(jì)算的對(duì)象是分類(lèi)變量刀荒。
所以針對(duì)【連續(xù)、正態(tài)分布、線性】數(shù)據(jù)缠借,采用pearson相關(guān)系數(shù)干毅;針對(duì)【非線性的、非正態(tài)】數(shù)據(jù)烈炭,采用spearman相關(guān)系數(shù)溶锭;針對(duì)【分類(lèi)變量、無(wú)序】數(shù)據(jù)符隙,采用Kendall相關(guān)系數(shù)趴捅。一般來(lái)講,線性數(shù)據(jù)采用pearson霹疫,否則選擇spearman拱绑,如果是分類(lèi)的則用kendall。
該部分內(nèi)容部分參考https://blog.csdn.net/tcy23456/article/details/83910011
第二部分:Pandas包Series.corr()調(diào)用
import pandas as pd
x = [1,2,3,4,5]
y = [6,7,8,9,6]
df = pd.DataFrame({'x':x, 'y':y})
df
Out[138]:
x y
0 1 6
1 2 7
2 3 8
3 4 9
4 5 6
df.x.corr(df.y)
Out[139]: 0.24253562503633297
df.x.corr(df.y, method='spearman')
Out[140]: 0.20519567041703082
df.x.corr(df.y, method='kendall')
Out[141]: 0.31622776601683794
df.corr()
Out[142]:
x y
x 1.000000 0.242536
y 0.242536 1.000000
可以看出丽蝎,構(gòu)造出的數(shù)據(jù)采用順序方法即Kendall相關(guān)系數(shù)最高猎拨。最后調(diào)用DataFrame.corr()可以得到一個(gè)相關(guān)性矩陣。
第三部分:顯著性檢驗(yàn)
顯著性檢驗(yàn)的目的是為了 將從樣本中得到的結(jié)論推廣到總體中屠阻,通過(guò)“小概率事件是不可能事件”這一原理進(jìn)行推斷红省。一般而言是對(duì)總體做出原假設(shè),然后通過(guò)對(duì)隨機(jī)的樣本數(shù)據(jù)對(duì)原假設(shè)進(jìn)行分析国觉,判斷其與原假設(shè)是否存在顯著性的差異吧恃。
調(diào)用Python代碼計(jì)算相關(guān)系數(shù)是非常簡(jiǎn)單的,但是如果沒(méi)有顯著性檢驗(yàn)麻诀,那么所得的結(jié)果是沒(méi)有意義的痕寓。不方便的是pandas內(nèi)嵌的函數(shù)中不提供p值的統(tǒng)計(jì),需要采用其他的統(tǒng)計(jì)軟件包蝇闭。
在對(duì)相關(guān)性檢驗(yàn)前呻率,需要先畫(huà)一個(gè)散點(diǎn)圖,看看兩個(gè)變量是否具有線性關(guān)系呻引。關(guān)于方差齊性的問(wèn)題礼仗,采用 stats.levene() 方法進(jìn)行檢驗(yàn)。如果同方差性結(jié)果不滿足的話苞七,則不能采用pearson 這總參數(shù)性的方法藐守,需要采用separsman或者是Kendall這兩種非參數(shù)性的方法進(jìn)行計(jì)算。
在scipy這一個(gè)統(tǒng)計(jì)包中蹂风,采用scipy.stats.spearmanr()等函數(shù)可以直接獲得相關(guān)系數(shù)以及雙邊檢驗(yàn)的P值卢厂。
correlation, p-value = scipy.stats.pearsonr(x, y)
correlation, p-value = scipy.stats.spearmanr(x, y)
correlation, p-value = scipy.stats.kendalltau(x, y)
采用這種方法,可以直接得到相關(guān)系數(shù)和P值惠啄,如下所示:
import scipy
import pandas as pd
x = [1,2,3,4,5]
y = [6,7,8,9,6]
df = pd.DataFrame({'x':x, 'y':y})
df
Out[138]:
x y
0 1 6
1 2 7
2 3 8
3 4 9
4 5 6
scipy.stats.pearsonr(x, y)
Out[144]: (0.24253562503633297, 0.6942488516293593)
scipy.stats.spearmanr(x, y)
Out[145]: SpearmanrResult(correlation=0.20519567041703082, pvalue=0.7405819415910722)
scipy.stats.kendalltau(x, y)
Out[146]: KendalltauResult(correlation=0.31622776601683794, pvalue=0.44848886103153185)
雖然看著相關(guān)性系數(shù)達(dá)到了0.3慎恒, P-value高達(dá)0.4任内,所以結(jié)果是不顯著的。
附錄
在網(wǎng)上搜到了一些關(guān)于相關(guān)性計(jì)算方法對(duì)數(shù)目要求的描述融柬,對(duì)相關(guān)性的檢驗(yàn)描述非常詳細(xì)死嗦,下面截取部分內(nèi)容:
Pearson correlation assumptions
Pearson correlation test is a parametric test that makes assumption about the data. In order for the results of a Pearson correlation test to be valid, the data must meet these assumptions:
1. The sample is independently and randomly drawn. 【正態(tài)】
2. A linear relationship between the two variables is present【線性】
3. When plotted, the lines form a line and is not curved【成對(duì)】
4. There is homogeneity of variance【方差齊性】
The variables being used in the correlation test should be continuous and measured either on a ratio or interval sale, each variable must have equal number of non-missing observations, and there should be no outliers present.【無(wú)異常值、缺失個(gè)數(shù)一致粒氧、變量連續(xù)】
Spearman Rank correlation assumptions
The Spearman rank correlation is a non-parametric test that does not make any assumptions about the distribution of the data. The assumption for the Spearman rank correlation test is:
There is a monotonic relationship between the variables being tested【單調(diào)性】
A monotonic relationship exists when one variable increases so does the other
For the Spearman rank correlation, the data can be used on ranked data, if the data is not normally distributed, and even if the there is not homogeneity of variance.
Kendall’s Tau correlation assumptions
The Kendall’s Tau correlation is a non-parametric test that does not make any assumptions about the distribution of the data. The only assumption is:
There should be a monotonic relationship between the variables being tested【單調(diào)性】
The data should be measured on either an ordinal, ratio, or interval scale.