Python 计算所有列之间的成对相关性
我正在处理大型生物数据集 我想计算数据表中所有2列组合的PCC(皮尔逊相关系数),并将结果保存为DataFrame或CSV文件 数据表如下:列是基因的名称,行是数据集的代码。浮点数表示数据集中基因被激活的程度Python 计算所有列之间的成对相关性,python,pandas,correlation,Python,Pandas,Correlation,我正在处理大型生物数据集 我想计算数据表中所有2列组合的PCC(皮尔逊相关系数),并将结果保存为DataFrame或CSV文件 数据表如下:列是基因的名称,行是数据集的代码。浮点数表示数据集中基因被激活的程度 GeneA GeneB GeneC ... DataA 1.5 2.5 3.5 ... DataB 5.5 6.5 7.5 ... DataC 8.5 8.5 8.5 ... ... 作为输出,我希望像下面这样构建表(DataFrame或csv文件),因为scipy.stat
GeneA GeneB GeneC ...
DataA 1.5 2.5 3.5 ...
DataB 5.5 6.5 7.5 ...
DataC 8.5 8.5 8.5 ...
...
作为输出,我希望像下面这样构建表(DataFrame或csv文件),因为scipy.stats.pearsonr函数返回(PCC,p-value)。
在我的示例中,XX和YY表示pearsonr的结果([1.5,5.5,8.5],[2.5,6.5,8.5])。同样,ZZ和AA表示pearsonr的结果([1.5,5.5,8.5],[3.5,7.5,8.5])。在我的测试中,我不需要像GeneB_GeneA或GeneC_GeneB这样的冗余数据
PCC P-value
GeneA_GeneB XX YY
GeneA_GeneC ZZ AA
GeneB_GeneC BB CC
...
由于列和行的数量很多(超过100个),而且它们的名称很复杂,因此使用列名或行名将很困难
对于专家来说,这可能是一个简单的问题,我不知道如何使用python和pandas库处理这种表。尤其是制作新的数据帧和添加结果似乎非常困难
对不起,我解释得不好,但我希望有人能帮助我。要获得配对,这是一个组合问题。您可以
concat
将所有行合并为一个结果dataframe
from pandas import *
from itertools import combinations
df = pandas.read_csv('gene.csv')
# get the column names as list, which are gene names
column_list = df.columns.values.tolist()
result = []
for c in combinations(column_list, 2):
firstGene, secondGene = c
firstGeneData = df[firstGene].tolist()
secondGeneData = df[secondGene].tolist()
# now to get the PCC, P-value using scipy
pcc = ...
p-value = ...
result.append(pandas.DataFrame([{'PCC': pcc, 'P-value': p-value}], index=str(firstGene)+ '_' + str(secondGene), columns=['PCC', 'P-value'])
result_df = pandas.concat(result)
#result_df.to_csv(...)
创建随机样本数据:
df = DataFrame(np.random.random((5, 5)), columns=['gene_' + chr(i + ord('a')) for i in range(5)])
print(df)
gene_a gene_b gene_c gene_d gene_e
0 0.471257 0.854139 0.781204 0.678567 0.697993
1 0.292909 0.046159 0.250902 0.064004 0.307537
2 0.422265 0.646988 0.084983 0.822375 0.713397
3 0.113963 0.016122 0.227566 0.206324 0.792048
4 0.357331 0.980479 0.157124 0.560889 0.973161
correlations = {}
columns = df.columns.tolist()
for col_a, col_b in itertools.combinations(columns, 2):
correlations[col_a + '__' + col_b] = pearsonr(df.loc[:, col_a], df.loc[:, col_b])
result = DataFrame.from_dict(correlations, orient='index')
result.columns = ['PCC', 'p-value']
print(result.sort_index())
PCC p-value
gene_a__gene_b 0.461357 0.434142
gene_a__gene_c 0.177936 0.774646
gene_a__gene_d -0.854884 0.064896
gene_a__gene_e -0.155440 0.802887
gene_b__gene_c -0.575056 0.310455
gene_b__gene_d -0.097054 0.876621
gene_b__gene_e 0.061175 0.922159
gene_c__gene_d -0.633302 0.251381
gene_c__gene_e -0.771120 0.126836
gene_d__gene_e 0.531805 0.356315
- 使用获取
列的唯一组合DataFrame
itertools.组合(iterable,r)
- 迭代这些组合,并使用
scipy.stats.stats.personr
- 将结果(PCC和p值元组)添加到
字典中
- 从
dictionary
然后还可以将
结果保存到\u csv()
。您可能会发现使用多索引(两列包含每列的名称)而不是为成对关联创建的名称很方便。一个简单的解决方案是使用(我创建的)的函数:
这将为您提供一个包含所有列组合的数据框架,并且,对于每一个列,都有r值、p值、样本大小等
还有许多选项用于指定一个或多个列(例如,一对所有行为),以及偏相关的协变量和计算相关系数的不同方法。请参阅以获得更深入的演示。假设您拥有的数据位于数据框中
df.corr('pearson') # 'kendall', and 'spearman' are the other 2 options
将为您提供每列之间的相关矩阵。这里回答:谢谢您的评论。我认为标题不够好。我想知道的不是如何计算PCC,而是计算所有列对的PCC,并将结果保存为新的数据帧。我不知道“组合”,但在进行这种对计算时看起来很不错。此外,我还了解到,通过concat函数可以很容易地从列表中生成数据帧。非常感谢你!非常感谢你!正如您和陈中普所建议的,使用组合函数似乎是解决此类问题的好方法。我还要再次感谢你的友好解释。这非常有用,因为我是python新手。
import pingouin as pg
pg.pairwise_corr(data, method='pearson')
df.corr('pearson') # 'kendall', and 'spearman' are the other 2 options