python短文本数据集的关联性_如何检查两个数据集的匹配列之间的相关性?

这段代码展示了如何利用Scipy库的pearsonr函数计算两个DataFrame中相同列之间的皮尔逊相关系数。通过创建一个新DataFrame并将相关系数存储在对角线上,可以方便地查看各列之间的相关性。结果展示了一个部分计算出的相关系数矩阵。
摘要由CSDN通过智能技术生成

这正是您想要的: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

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

^{pr2}$

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值