首页 > 解决方案 > 在大系数矩阵中找到高相关性

问题描述

我有一个包含 56 个数字特征的数据集。加载它pandas,我可以很容易地生成一个相关系数矩阵

但是,由于它的大小,我想找到高于(或低于)某个阈值的系数,例如 >0.8 或 <-0.8,并列出相应的变量对。有没有办法做到这一点?我认为这需要在所有列中按值进行选择,然后返回,不是行,而是值的列名和行索引,但我也不知道该怎么做!

谢谢!

标签: pythonpandascorrelationpearson-correlation

解决方案


我认为你可以这样做wherestack()这个:

np.random.seed(1)
df = pd.DataFrame(np.random.rand(10,3))

coeff = df.corr()

# 0.3 is used for illustration 
# replace with your actual value
thresh = 0.3

mask = coeff.abs().lt(thresh)
# or mask = coeff < thresh

coeff.where(mask).stack()

输出:

0  2   -0.089326
2  0   -0.089326
dtype: float64

输出:

0  1    0.319612
   2   -0.089326
1  0    0.319612
   2   -0.687399
2  0   -0.089326
   1   -0.687399
dtype: float64

推荐阅读