machine-learning - 使用统计模型进行特征选择
问题描述
问题陈述 :
我正在解决一个问题,我必须预测客户是否会选择贷款。我已将所有可用的数据类型(对象,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)
我的问题是
- 当结果每次都不同时,高于 z-Test 是进行特征选择的可靠方法
- 在这种情况下,做特征选择的更好方法是什么,如果可能的话,提供一个例子
解决方案
在这种情况下,做特征选择的更好方法是什么,如果可能的话,提供一个例子
你能用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)
例如,这只会保留有一些差异的列,而不是只有相同的值。
推荐阅读
- docker - docker中的模拟时间(-compose)
- r - 在 R Shiny App 中延迟后更改输出
- machine-learning - 在现实世界中没有恒定平衡时如何平衡您的数据集
- php - 无论如何,SQlite3 是否知道已添加的条目数量?
- java - Java FX 与 Firebase 存储的连接,用于上传图片
- parsing - 使用 Wordpress 提要获取解析错误
- julia - “错误:语法:不能并列字符串文字”用引号结束三引号字符串文字时
- c# - 如何为 GPIO 启用 LightningProviders?
- c# - 当按下按钮并显示在另一个页面上时,试图从两个输入中更改标签?| Xamarin | C#
- r - 将 JSON 对象拆分为多个数据帧的更快方法?