python - 在大系数矩阵中找到高相关性
问题描述
我有一个包含 56 个数字特征的数据集。加载它pandas
,我可以很容易地生成一个相关系数矩阵。
但是,由于它的大小,我想找到高于(或低于)某个阈值的系数,例如 >0.8 或 <-0.8,并列出相应的变量对。有没有办法做到这一点?我认为这需要在所有列中按值进行选择,然后返回,不是行,而是值的列名和行索引,但我也不知道该怎么做!
谢谢!
解决方案
我认为你可以这样做where
:stack()
这个:
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
推荐阅读
- java - 在 RollingFile 附加程序中无法识别 log4j2 CronTriggeringPolicy
- python - 如何将条件添加到正则表达式中?
- kubernetes - 从快照问题重新创建 Kubernetes 集群
- sql - 检查一个或多个 id 是否在 json 字符串中
- logic - 如何使用 Hoare 的逻辑证明带有 while 循环的程序的正确性?
- c++ - 为什么输出精度没有正确舍入?
- python - 熊猫计算增量时间
- python - 从 Excel 文件中获取数据并将其打印到控制台?
- android - 运行 ionic cordova 应用程序时出现权限错误
- chart.js - Chart.Js - 高亮背景中的一个区域