【问题标题】:How to check correlation between matching columns of two data sets?如何检查两个数据集的匹配列之间的相关性?
【发布时间】:2016-12-06 21:09:11
【问题描述】:

如果我们有数据集:

import pandas as pd
a = pd.DataFrame({"A":[34,12,78,84,26], "B":[54,87,35,25,82], "C":[56,78,0,14,13], "D":[0,23,72,56,14], "E":[78,12,31,0,34]})
b = pd.DataFrame({"A":[45,24,65,65,65], "B":[45,87,65,52,12], "C":[98,52,32,32,12], "D":[0,23,1,365,53], "E":[24,12,65,3,65]})

如何创建一个相关矩阵,其中 y 轴代表“a”,x 轴代表“b”?

目的是查看两个数据集的匹配列之间的相关性,如下所示:

【问题讨论】:

  • 您的目标是获得单个系数还是 5 个不同的系数?
  • 我现在意识到我绘制的图片具有误导性。我希望在不同数据集的每个匹配列之间获得一个系数

标签: python pandas numpy correlation


【解决方案1】:

如果您不介意基于 NumPy 的矢量化解决方案,基于 this solution postComputing the correlation coefficient between two multi-dimensional arrays -

corr2_coeff(a.values.T,b.values.T).T # func from linked solution post.

示例运行 -

In [621]: a
Out[621]: 
    A   B   C   D   E
0  34  54  56   0  78
1  12  87  78  23  12
2  78  35   0  72  31
3  84  25  14  56   0
4  26  82  13  14  34

In [622]: b
Out[622]: 
    A   B   C    D   E
0  45  45  98    0  24
1  24  87  52   23  12
2  65  65  32    1  65
3  65  52  32  365   3
4  65  12  12   53  65

In [623]: corr2_coeff(a.values.T,b.values.T).T
Out[623]: 
array([[ 0.71318502, -0.5923714 , -0.9704441 ,  0.48775228, -0.07401011],
       [ 0.0306753 , -0.0705457 ,  0.48801177,  0.34685977, -0.33942737],
       [-0.26626431, -0.01983468,  0.66110713, -0.50872017,  0.68350413],
       [ 0.58095645, -0.55231196, -0.32053858,  0.38416478, -0.62403866],
       [ 0.01652716,  0.14000468, -0.58238879,  0.12936016,  0.28602349]])

【讨论】:

  • 我实际上正在考虑将其全部更改为 numpy。接下来我想实际做 3 个数据集之间的相关性,其中每个列名在每个轴上都有所有三个值。我认为 numpy 会让这更容易。像这样:seaborn.pydata.org/examples/network_correlations.html
  • 您好,我现在一直在使用这个解决方案,谢谢。您是否使用 Spearman 的秩相关而不是 Pearson 的 r 做过类似的事情?
  • @ishido 据我所知,我没有,抱歉。
【解决方案2】:

这正是你想要的:

from scipy.stats import pearsonr

# create a new DataFrame where the values for the indices and columns
# align on the diagonals
c = pd.DataFrame(columns = a.columns, index = a.columns)

# since we know set(a.columns) == set(b.columns), we can just iterate
# through the columns in a (although a more robust way would be to iterate
# through the intersection of the two sets of columns, in the case your actual dataframes' columns don't match up
for col in a.columns:
    correl_signif = pearsonr(a[col], b[col]) # correlation of those two Series
    correl = correl_signif[0] # grab the actual Pearson R value from the tuple from above
    c.loc[col, col] = correl   # locate the diagonal for that column and assign the correlation coefficient   

编辑:嗯,它完全实现了您想要的,直到问题被修改。虽然这很容易改变:

c = pd.DataFrame(columns = a.columns, index = a.columns)

for col in c.columns:
    for idx in c.index:
        correl_signif = pearsonr(a[col], b[idx])
        correl = correl_signif[0]
        c.loc[idx, col] = correl

c 现在是这样的:

Out[16]: 
           A          B         C         D          E
A   0.713185  -0.592371 -0.970444  0.487752 -0.0740101
B  0.0306753 -0.0705457  0.488012   0.34686  -0.339427
C  -0.266264 -0.0198347  0.661107  -0.50872   0.683504
D   0.580956  -0.552312 -0.320539  0.384165  -0.624039
E  0.0165272   0.140005 -0.582389   0.12936   0.286023

【讨论】:

  • 是的!对不起,我编辑了我发布的图像。是否可以包括所有相关系数来做到这一点?你得到的矩阵正是我正在寻找的那种东西。
【解决方案3】:

我使用这个函数用 numpy 分解它

def corr_ab(a, b):

    a_ = a.values
    b_ = b.values
    ab = a_.T.dot(b_)
    n = len(a)

    sums_squared = np.outer(a_.sum(0), b_.sum(0))
    stds_squared = np.outer(a_.std(0), b_.std(0))

    return pd.DataFrame((ab - sums_squared / n) / stds_squared / n,
                        a.columns, b.columns)

演示

corr_ab(a, b)

【讨论】:

    【解决方案4】:

    您必须使用 Pandas 吗?这似乎可以通过 numpy 轻松完成。我是不是理解错了任务?

       import numpy
       X = {"A":[34,12,78,84,26], "B":[54,87,35,25,82], "C":[56,78,0,14,13], "D":[0,23,72,56,14], "E":[78,12,31,0,34]}
       Y = {"A":[45,24,65,65,65], "B":[45,87,65,52,12], "C":[98,52,32,32,12], "D":[0,23,1,365,53], "E":[24,12,65,3,65]}
       for key,value in X.items():
            print "correlation stats for %s is %s" % (key, numpy.corrcoef(value,Y[key]))
    

    【讨论】:

      猜你喜欢
      • 2019-09-21
      • 1970-01-01
      • 2022-11-23
      • 2014-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多