![](/img/trans.png)
[英]How to get the correlation between two selected columns in a DataFrame using .corr() Pearson Correlation
[英]How to do Pearson correlation of selected columns of a Pandas data frame
我有一个如下所示的 CSV:
gene,stem1,stem2,stem3,b1,b2,b3,special_col
foo,20,10,11,23,22,79,3
bar,17,13,505,12,13,88,1
qui,17,13,5,12,13,88,3
作为数据框,它看起来像这样:
In [17]: import pandas as pd
In [20]: df = pd.read_table("http://dpaste.com/3PQV3FA.txt",sep=",")
In [21]: df
Out[21]:
gene stem1 stem2 stem3 b1 b2 b3 special_col
0 foo 20 10 11 23 22 79 3
1 bar 17 13 505 12 13 88 1
2 qui 17 13 5 12 13 88 3
我想要做的是从最后一列( special_col
)与gene
column 和special column
之间的每一列执行皮尔逊相关,即colnames[1:number_of_column-1]
在一天结束时,我们将得到长度为 6 的数据帧。
Coln PearCorr
stem1 0.5
stem2 -0.5
stem3 -0.9999453506011533
b1 0.5
b2 0.5
b3 -0.5
上面的值是手动计算的:
In [27]: import scipy.stats
In [39]: scipy.stats.pearsonr([3, 1, 3], [11,505,5])
Out[39]: (-0.9999453506011533, 0.0066556395400007278)
我怎样才能做到这一点?
请注意,您的数据有误,特殊列全是 3,因此无法计算相关性。
如果最后删除列选择,您将获得您正在分析的所有其他列的相关矩阵。 最后一个 [:-1] 是去除‘special_col’与其自身的相关性。
In [15]: data[data.columns[1:]].corr()['special_col'][:-1]
Out[15]:
stem1 0.500000
stem2 -0.500000
stem3 -0.999945
b1 0.500000
b2 0.500000
b3 -0.500000
Name: special_col, dtype: float64
如果您对速度感兴趣,这在我的机器上会稍微快一些:
In [33]: np.corrcoef(data[data.columns[1:]].T)[-1][:-1]
Out[33]:
array([ 0.5 , -0.5 , -0.99994535, 0.5 , 0.5 ,
-0.5 ])
In [34]: %timeit np.corrcoef(data[data.columns[1:]].T)[-1][:-1]
1000 loops, best of 3: 437 µs per loop
In [35]: %timeit data[data.columns[1:]].corr()['special_col']
1000 loops, best of 3: 526 µs per loop
但显然,它返回一个数组,而不是 pandas series/DF。
可以使用 pd.DataFrame.corrwith( )代替df.corr() 。
传入我们希望与其余列相关的预期列。
对于上面的具体示例,代码将是: df.corrwith(df['special_col'])
或者简单地df.corr()['special_col']来创建每列与其他列的完整关联,并根据需要创建子集。
您可以apply
调用corr
并传递Series
'special_col'
的lambda
在您的列范围上应用:
In [126]:
df[df.columns[1:-1]].apply(lambda x: x.corr(df['special_col']))
Out[126]:
stem1 0.500000
stem2 -0.500000
stem3 -0.999945
b1 0.500000
b2 0.500000
b3 -0.500000
dtype: float64
时序
实际上另一种方法更快,所以我希望它能更好地扩展:
In [130]:
%timeit df[df.columns[1:-1]].apply(lambda x: x.corr(df['special_col']))
%timeit df[df.columns[1:]].corr()['special_col']
1000 loops, best of 3: 1.75 ms per loop
1000 loops, best of 3: 836 µs per loop
为什么不这样做:
In [34]: df.corr().iloc[:-1,-1]
Out[34]:
stem1 0.500000
stem2 -0.500000
stem3 -0.999945
b1 0.500000
b2 0.500000
b3 -0.500000
Name: special_col, dtype: float64
要么:
In [39]: df.corr().ix['special_col', :-1]
Out[39]:
stem1 0.500000
stem2 -0.500000
stem3 -0.999945
b1 0.500000
b2 0.500000
b3 -0.500000
Name: special_col, dtype: float64
时序
In [35]: %timeit df.corr().iloc[-1,:-1]
1000 loops, best of 3: 576 us per loop
In [40]: %timeit df.corr().ix['special_col', :-1]
1000 loops, best of 3: 634 us per loop
In [36]: %timeit df[df.columns[1:]].corr()['special_col']
1000 loops, best of 3: 968 us per loop
In [37]: %timeit df[df.columns[1:-1]].apply(lambda x: x.corr(df['special_col']))
100 loops, best of 3: 2.12 ms per loop
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.