如何检查两个数据集的匹配列之间的相关性? [英] How to check correlation between matching columns of two data sets?

查看:578
本文介绍了如何检查两个数据集的匹配列之间的相关性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们有数据集:

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"?

How does one create a correlation matrix, in which the y-axis represents "a" and the x-axis represents "b"?

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

The aim is to see correlations between the matching columns of the two datasets like this:

推荐答案

这完全可以实现您想要的:

This achieves exactly what you want:

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   

好吧,它完全实现了您想要的 ,直到问题被修改为止.尽管可以轻松更改:

Well, it achieved exactly what you wanted, until the question was modified. Although this can easily be changed:

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

这篇关于如何检查两个数据集的匹配列之间的相关性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆