我们如何在热图中仅显示超过某个阈值的相关特征?

2024-09-27 09:30:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个数据框中有太多的功能。我试图只画出在某个阈值上相关的特征,比如说超过80%,并在热图中显示这些特征。我把一些代码放在一起运行,但我仍然看到一些白线,它们没有数据,因此没有相关性。此外,我看到的东西,是远远低于80%的相关性。这是我试过的代码

import seaborn
c = newdf.corr()
plt.figure(figsize=(10,10))
seaborn.heatmap(c, cmap='RdYlGn_r', mask = (np.abs(c) >= 0.8))
plt.show()

当我运行它时,我看到了这个

enter image description here

这里怎么了

我正在做一个小的更新,有一些新的发现

这只会得到corr>;。8.

corr = newdf.corr()
kot = corr[corr>=.8]
plt.figure(figsize=(12,8))
sns.heatmap(kot, cmap="Reds")

enter image description here

这似乎有效,但它仍然给我很多白色!我认为应该有一种方法,只包括在一定数量上具有相关性的项目。也许您必须使用>;复制这些项目;。将8项添加到新的数据框中,并建立该对象的相关性。我不知道这是怎么回事


Tags: 数据项目代码gt功能plt特征seaborn
1条回答
网友
1楼 · 发布于 2024-09-27 09:30:31

以下代码将强相关特征(相关度大于0.8)分组为组件,并分别绘制每组组件的相关性。如果与您想要的不同,请告诉我

components = list()
visited = set()
print(newdf.columns)
for col in newdf.columns:
    if col in visited:
        continue

    component = set([col, ])
    just_visited = [col, ]
    visited.add(col)
    while just_visited:
        c = just_visited.pop(0)
        for idx, val in corr[c].items():
            if abs(val) > 0.999 and idx not in visited:
                just_visited.append(idx)
                visited.add(idx)
                component.add(idx)
    components.append(component)

for component in components:
    plt.figure(figsize=(12,8))
    sns.heatmap(corr.loc[component, component], cmap="Reds")

相关问题 更多 >

    热门问题