首页 > 解决方案 > 使用统计模型进行特征选择

问题描述

问题陈述

我正在解决一个问题,我必须预测客户是否会选择贷款。我已将所有可用的数据类型(对象,int)转换为整数,现在我的数据如下所示。

数据视图

突出显示的列是我的目标列,其中

0表示

1表示

该数据集中有47 个独立的列。

我想 针对我的目标列对这些列进行特征选择!!

我从Z 测试开始

import numpy as np
import scipy.stats as st
import scipy.special as sp


def feature_selection_pvalue(df,col_name,samp_size=1000):
    relation_columns=[]
    no_relation_columns=[]
    H0='There is no relation between target column and independent column'
    H1='There is a relation between target column and independent column'
    sample_data[col_name]=df[col_name].sample(samp_size)
    samp_mean=sample_data[col_name].mean()
    pop_mean=df[col_name].mean()
    pop_std=df[col_name].std()
    print (pop_mean)
    print (pop_std)
    print (samp_mean)
    n=samp_size
    q=.5
    #lets calculate z
    #z = (samp_mean - pop_mean) / np.sqrt(pop_std*pop_std/n)
    z = (samp_mean - pop_mean) / np.sqrt(pop_std*pop_std / n)
    print (z)
    pval = 2 * (1 - st.norm.cdf(z))
    print ('p values is==='+str(pval))
    if pval< .05 :
        print ('Null hypothesis is Accepted for col ---- >'+H0+col_name)

        no_relation_columns.append(col_name)
    else:
        print ('Alternate Hypothesis is accepted -->'+H1)
        relation_columns.append(col_name)
        print ('length of list ==='+str(len(relation_columns)))


    return relation_columns,no_relation_columns

当我运行这个函数时,我总是得到不同的结果

for items in df.columns:
    relation,no_relation=feature_selection_pvalue(df,items,5000)

我的问题是

  1. 当结果每次都不同时,高于 z-Test 是进行特征选择的可靠方法
  2. 在这种情况下,做特征选择的更好方法是什么,如果可能的话,提供一个例子

标签: machine-learningstatisticsfeature-extractionfeature-selection

解决方案


在这种情况下,做特征选择的更好方法是什么,如果可能的话,提供一个例子

你能用scikit吗?他们提供了很多示例和可能性来选择您的功能: https ://scikit-learn.org/stable/modules/feature_selection.html

如果我们看第一个(方差阈值):

from sklearn.feature_selection import VarianceThreshold
X = df[['age', 'balance',...]] #select your columns
sel = VarianceThreshold(threshold=(.8 * (1 - .8)))
X_red = sel.fit_transform(X)

例如,这只会保留有一些差异的列,而不是只有相同的值。


推荐阅读