如何测量分类变量和连续变量之间的相关性

数据挖掘 机器学习 Python scikit-学习 相关性 scipy
2022-02-16 16:18:53

我的数据集中有以下分类变量的名称列表:

categorical_columns = ['MSSubClass', 'MSZoning', 'LotShape', 'LandContour', 'LotConfig', 'Neighborhood', 'Condition1',
                       'Condition2', 'BldgType', 'HouseStyle', 'RoofStyle', 'RoofMatl', 'Exterior1st', 'Exterior2nd',
                       'Foundation', 'Heating', 'Electrical', 'Functional', 'GarageType', 'PavedDrive', 'Fence',
                       'MiscFeature', 'SaleType', 'SaleCondition', 'Street', 'CentralAir']

在这个函数定义中,我在 for 循环的帮助下对每一列进行一次性编码:

def feature_encoding(df, categorical_list):

    # One Hot Encoding the columns gathered in categorical_columns
    for col in categorical_list:

        # take one-hot encoding
        OHE_sdf = pd.get_dummies(df[categorical_list])

        # drop the old categorical column from original df
        df.drop(col, axis = 1, inplace = True)

        # attach one-hot encoded columns to original dataframe
        df = pd.concat([df, OHE_sdf], axis = 1, ignore_index = True)

我不想在训练中使用所有这些列,所以现在我正处于工作的降维阶段。我想测量这些列中的每一个与我的SalePrice变量(数值)之间的相关性,并剔除相关性低的列。

我读过卡方检验通常用于测量分类变量的相关性,但我还没有看到它是分类变量列表与连续变量的实现。

1个回答

卡方检验测量两个分类变量之间的关系。

要测量分类特征和连续特征之间的关系,可以使用 ANOVA 检验。

顺便说一句,您不需要使用 for 循环进行编码 -get_dummies具有columns允许用户指定对哪些列进行编码的参数。