python - 相关矩阵: Extract Variables with High R Values

标签 python matrix correlation

如何获得仅列出绝对值相关性大于 0.7 的变量的输出?

我想要类似这样的输出:

four: one, three
one: three

感谢您的宝贵时间!

代码

import pandas as pd

x={'one':[1,2,3,4],'two':[3,5,7,5],'three':[2,3,4,9],'four':[4,3,1,0],}
y=pd.DataFrame(x)
print(y.corr())

输出

           four       one     three       two
four   1.000000 -0.989949 -0.880830 -0.670820
one   -0.989949  1.000000  0.913500  0.632456
three -0.880830  0.913500  1.000000  0.262613
two   -0.670820  0.632456  0.262613  1.000000

最佳答案

如果您只想打印出来,这将起作用:

col_names = y.corr().columns.values

for col, row in (y.corr().abs() > 0.7).iteritems():
    print(col, col_names[row.values])

请注意,这可以工作,但可能会很慢,因为 iteritems 方法将每一行转换为一个系列。

关于python - 相关矩阵: Extract Variables with High R Values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37715050/

相关文章:

python - Sqlite3与python插入同一行

python - Altair mark_line 绘图比 matplotlib 更嘈杂?

java - 我怎样才能得到一个方阵并将其放入另一个矩阵?

matlab - 乔列斯基分解

arrays - 如何获取矩阵列中最后一个非零值的数组?

r - 相关性的错误视觉图表示

python - Django:带有 mixins 和调度方法的基于类的 View

R:计算数据集子集的相关性?

signal-processing - 两个信号之间的相位差?

python - 按列表顺序打印字典