一个数据帧与另一个数据帧的相关矩阵 [英] correlation matrix of one dataframe with another

查看:72
本文介绍了一个数据帧与另一个数据帧的相关矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读这个问题的答案.接下来的问题是如何计算一个数据帧中所有列与另一数据帧中所有列之间的相关性.既然似乎这个问题无法解决,所以我想问这个问题,因为我需要像这样的东西.

I was reading through the answers to this question. Then question came up on how to calculate the correlations of all columns from one dataframe with all columns from the other dataframe. Since it seemed this question wasn't going to get answered, I wanted to ask it as I need something just like that.

所以说我有数据框AB:

import pandas as pd
import numpy as np

A = pd.DataFrame(np.random.rand(24, 5), columns=list('abcde'))
B = pd.DataFrame(np.random.rand(24, 5), columns=list('ABCDE'))

如何获取如下所示的数据框:

how do I get a dataframe that looks like this:

pd.DataFrame([], A.columns, B.columns)

     A    B    C    D    E
a  NaN  NaN  NaN  NaN  NaN
b  NaN  NaN  NaN  NaN  NaN
c  NaN  NaN  NaN  NaN  NaN
d  NaN  NaN  NaN  NaN  NaN
e  NaN  NaN  NaN  NaN  NaN

但是充满适当的相关性吗?

But filled with the appropriate correlations?

推荐答案

一种方法是:

pd.concat([A, B], axis=1).corr().filter(B.columns).filter(A.columns, axis=0)

一种更有效的方法是:

Az = (A - A.mean())
Bz = (B - B.mean())

Az.T.dot(Bz).div(len(A)).div(Bz.std(ddof=0)).div(Az.std(ddof=0), axis=0)

您将获得与以上相同的信息.

And you'd get the same as above.

这篇关于一个数据帧与另一个数据帧的相关矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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