几组数据的相关性python_python – 计算所有列之间的成对相关性

from pandas import *

import numpy as np

from libraries.settings import *

from scipy.stats.stats import pearsonr

import itertools

创建随机样本数据:

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.combination(iterable,r)

>使用scipy.stats.stats.personr迭代这些组合并计算成对相关性

>将结果(PCC和p值元组)添加到字典中

>从字典构建DataFrame

然后,您还可以保存result.to_csv().您可能会发现使用MultiIndex(包含每列名称的两列)而不是成对关联的已创建名称会很方便.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值