【问题标题】:Selecting specific features based on correlation values根据相关值选择特定特征
【发布时间】:2020-09-25 19:11:02
【问题描述】:

我正在使用来自 Kaggle 的 Housing train.csv 数据进行预测。
https://www.kaggle.com/c/house-prices-advanced-regression-techniques/data?select=train.csv

我正在尝试生成相关性,并且仅将与 SalePrice 相关的特征保持在 0.5 到 0.9 之间。我试图使用这个函数来过滤其中的一些,但我只删除了高于 0.9 的相关值。 如何更新此函数以仅保留生成相关热图所需的那些特定特征?

data = train
corr = data.corr()
columns = np.full((corr.shape[0],), True, dtype=bool)
for i in range(corr.shape[0]):
    for j in range(i+1, corr.shape[0]):
        if corr.iloc[i,j] >= 0.9:
            if columns[j]:
                columns[j] = False
selected_columns = data.columns[columns]
data = data[selected_columns]

【问题讨论】:

  • 所以你想要一个只有这些列的数据框,而所有其他列都被删除了?
  • 是的,如果可以的话,请。
  • 我在答案中做了同样的事情:)

标签: python


【解决方案1】:
import pandas as pd

data = pd.read_csv('train.csv')
col = data.columns

c  = [i for i in col if data[i].dtypes=='int64' or data[i].dtypes=='float64']   # dropping columns as dtype == object
main_col = ['SalePrice']        # column with which we have to compare correlation

corr_saleprice = data.corr().filter(main_col).drop(main_col)    

c1 =(corr_saleprice['SalePrice']>=0.5) & (corr_saleprice['SalePrice']<=0.9)
c2 =(corr_saleprice['SalePrice']>=-0.9) & (corr_saleprice['SalePrice']<=-0.5)

req_index= list(corr_saleprice[c1 | c2].index)   # selecting column with given criteria

#req_index.append('SalePrice')      #if you want SalePrice column in your final dataframe too , uncomment this line

data = data[req_index]  

data

同样使用 for 循环也不是那么有效,直接实现是有利的。我希望这就是你想要的!

要生成热图,您可以使用以下代码:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

a  =data.corr()
mask = np.triu(np.ones_like(a, dtype=np.bool))
plt.figure(figsize=(10,10))
_ = sns.heatmap(a,cmap=sns.diverging_palette(250, 20, n=250),square=True,mask=mask,annot=True,center=0.5)

【讨论】:

  • 我试图用热图绘制相关性,但看起来不太好。您想尝试生成结果的热图并告诉我它是否适合您吗?
  • correlations = train [req_index]#为上三角形掩码生成掩码= np.zeros_like(correletations)掩码[np.triu_indices_from(mask)] = 1 sns.set_style('白色')plt .figure(Figsize =(20,18))#绘制相关性SNS.HeatMap(相关* 100,CMAP ='RDBU_R',ANNOT = TRUE,FMT ='。0F',MASK =掩码,CBAR = FALSE)PLT .show() span>
  • 你能分享你得到的图像,以及你想要的不同的图像吗?
  • 是的,你需要 -0.9 到 -0.5 和 0.5 到 0.9 !
  • 是的,正是……我认为那会更好。难道你呢? span>
猜你喜欢
  • 2014-09-21
  • 2017-08-14
  • 1970-01-01
  • 2021-03-04
  • 1970-01-01
  • 1970-01-01
  • 2021-04-06
  • 2013-02-21
相关资源
最近更新 更多