【问题标题】:how to find and drop correlated columns from pandas df [duplicate]如何从熊猫df中查找和删除相关列[重复]
【发布时间】:2021-06-18 19:10:57
【问题描述】:

我有一个 df,我使用热图来查找列之间的相关性,我想删除那些相关的列,是否有任何库或函数不知道对这个问题进行排序?

import matplotlib.pyplot as plt
import seaborn as sns
fig, ax = plt.subplots(figsize=(30,30)) 
sns.heatmap(dfnew.corr(),annot = True, vmin=-1, vmax=1, center= 0, cmap= 'coolwarm',ax=ax)

【问题讨论】:

  • 问题与machine-learning 或random-forest 无关 - 请不要向无关标签发送垃圾邮件(已删除)。

标签: python pandas correlation


【解决方案1】:
# Create correlation matrix
corr_matrix =dfnew.corr().abs()

# Select upper triangle of correlation matrix
upper = corr_matrix.where(np.triu(np.ones(corr_matrix.shape), k=1).astype(np.bool))

# Find index of feature columns with correlation greater than 0.95
to_drop = [column for column in upper.columns if any(upper[column] > 0.90)]

【讨论】:

    猜你喜欢
    • 2018-07-01
    • 2021-07-22
    • 2019-05-06
    • 1970-01-01
    • 2019-11-13
    • 2020-06-20
    • 2021-08-31
    • 2022-07-27
    • 2019-11-05
    相关资源
    最近更新 更多