Python 返回数据帧中相关列的组
我在数据帧上运行了一个相关矩阵:Python 返回数据帧中相关列的组,python,numpy,pandas,Python,Numpy,Pandas,我在数据帧上运行了一个相关矩阵: df=pd.DataFrame( {'one':[0.1, .32, .2, 0.4, 0.8], 'two':[.23, .18, .56, .61, .12], 'three':[.9, .3, .6, .5, .3], 'four':[.34, .75, .91, .19, .21], 'zive': [0.1, .32, .2, 0.4, 0.8], 'six':[.9, .3, .6, .5, .3], 'drive':[.9, .3, .6, .5,
df=pd.DataFrame( {'one':[0.1, .32, .2, 0.4, 0.8], 'two':[.23, .18, .56, .61, .12], 'three':[.9, .3, .6, .5, .3], 'four':[.34, .75, .91, .19, .21], 'zive': [0.1, .32, .2, 0.4, 0.8], 'six':[.9, .3, .6, .5, .3], 'drive':[.9, .3, .6, .5, .3]})
corrMatrix=df.corr()
corrMatrix
drive four one six three two zive
drive 1.00 -0.04 -0.75 1.00 1.00 0.24 -0.75
four -0.04 1.00 -0.49 -0.04 -0.04 0.16 -0.49
one -0.75 -0.49 1.00 -0.75 -0.75 -0.35 1.00
six 1.00 -0.04 -0.75 1.00 1.00 0.24 -0.75
three 1.00 -0.04 -0.75 1.00 1.00 0.24 -0.75
two 0.24 0.16 -0.35 0.24 0.24 1.00 -0.35
zive -0.75 -0.49 1.00 -0.75 -0.75 -0.35 1.00
现在,我想编写一些代码来返回组中完全相关的列(即correlation==1)
最理想的情况是,我希望:
我已经编写了下面的代码,这给了我['drive'、'one'、'six'、'three'、'zive']
,但正如您所看到的,它们只是一袋列,与其他列有某种完美的相关性——这并没有将它们与完全相关的同类列放在一个独特的分组中
correlatedCols=[]
for col in corrMatrix:
data=corrMatrix[col][corrMatrix[col]==1]
if len(data)>1:
correlatedCols.append(data.name)
correlatedCols
['drive','one', 'six', 'three', 'zive']
编辑:根据@Karl D给出的建议,我得到了以下信息:
cor = df.corr()
cor.loc[:,:] = np.tril(cor.values, k=-1)
cor = cor.stack()
cor[cor ==1]
six drive 1.00
three drive 1.00
six 1.00
zive one 1.00
…这不是我想要的--因为[six,drive]
不是一个分组--它缺少'three'
您可以执行以下操作:
>>> cor = df.corr()
>>> cor.loc[:,:] = np.tril(cor, k=-1)
>>> cor = cor.stack()
>>> cor[cor > 0.9999]
three six 1
zive one 1
>>> cor[cor > 0.9999].to_dict().keys()
[('zive', 'one'), ('three', 'six')]
要更紧密地匹配预期输出,您可以执行以下操作:
>>> cor = df.corr()
>>> cor.loc[:,:] = np.tril(cor, k=-1)
>>> cor = cor.stack()
>>> cor[cor > 0.9999]
three six 1
zive one 1
>>> cor[cor > 0.9999].to_dict().keys()
[('zive', 'one'), ('three', 'six')]
解释。首先,我创建了一个不包括对角线的协方差矩阵的下三角版本(使用numpy的tril
):
然后我堆叠数据帧:
>>> cor = cor.stack()
four four 0.000000
one -0.000000
six -0.000000
three -0.000000
two 0.000000
zive -0.000000
one four -0.489177
one 0.000000
six -0.000000
three -0.000000
two -0.000000
zive 0.000000
six four -0.039607
one -0.747365
six 0.000000
three 0.000000
two 0.000000
zive -0.000000
three four -0.039607
one -0.747365
six 1.000000
three 0.000000
two 0.000000
zive -0.000000
two four 0.159583
one -0.351531
six 0.238102
three 0.238102
two 0.000000
zive -0.000000
zive four -0.489177
one 1.000000
six -0.747365
three -0.747365
two -0.351531
zive 0.000000
然后我就可以抓取等于1的行
编辑:我想这会得到你想要的形式,但它并不优雅:
>>> from itertools import chain
>>> cor.loc[:,:] = np.tril(cor, k=-1)
>>> cor = cor.stack()
>>> ones = cor[cor > 0.999].reset_index().loc[:,['level_0','level_1']]
>>> ones = ones.query('level_0 not in level_1')
>>> ones.groupby('level_0').agg(lambda x: set(chain(x.level_0,x.level_1))).values
[[set(['six', 'drive', 'three'])]
[set(['zive', 'one'])]]
以下是一种天真的方法:
df=pd.DataFrame( {'one':[0.1, .32, .2, 0.4, 0.8], 'two':[.23, .18, .56, .61, .12], 'three':[.9, .3, .6, .5, .3], 'four':[.34, .75, .91, .19, .21], 'zive': [0.1, .32, .2, 0.4, 0.8], 'six':[.9, .3, .6, .5, .3], 'drive':[.9, .3, .6, .5, .3]})
corrMatrix=df.corr()
corrMatrix.loc[:,:] = np.tril(corrMatrix, k=-1) # borrowed from Karl D's answer
already_in = set()
result = []
for col in corrMatrix:
perfect_corr = corrMatrix[col][corrMatrix[col] == 1].index.tolist()
if perfect_corr and col not in already_in:
already_in.update(set(perfect_corr))
perfect_corr.append(col)
result.append(perfect_corr)
结果:
>>> result
[['six', 'three', 'drive'], ['zive', 'one']]
隐马尔可夫模型。。。只是尝试了一组三个相关变量,但是输出有点混乱。我将修改我的问题来创建这个场景。我不认为输出会让人困惑,它只是提供了所有完全相关的列对:基于您最初的问题,这正是我认为您想要的。但是,即使它提供了同等的信息,它也没有根据您的编辑以您想要的形式提供给您。让我看看能不能把它做成你想要的形状。效果很好。谢谢。还有一个问题——我现在在一个datafame上实现这个函数,这个datafame有3856列宽,100000行长。由于该函数不使用多处理,因此速度非常慢是可以理解的。查看pandas
文档,我看不到让DataFrame.corr()
函数使用多处理的方法。您知道有哪些函数会在本机使用多处理的数据帧上创建相关矩阵吗?如果不是的话,我们是否需要再问一个问题来解决这个问题,或者我们是否应该继续重复这个问题?@Bryan,我不知道有任何这样的功能或选项,对不起。因此,或许最好问一个不同的问题。