自动化特征选择

       自动化特征选择用于判断每个特征的作用,从原始数据特征中选择那些最有用的特征,通常有三种策略:单变量统计、基于模型的选择和迭代选择。下面结合cancer数据集对它们进行分析。

一、单变量统计

       在单变量统计中,我们计算每个特征和目标值之间的关系是否存在统计显著性,然后选择具有最高置信度的特征。(这些测试的一个关键性质就是它们是单变量的,即只单独考虑每个特征,如果一个特征只有在与另一个特征合并时才具有信息量,那么这个特征将被舍弃)。

       在scikit-learn中使用单变量特征选择,对于分类问题,选择f_classif,对于回归问题,选择f_regression,然后基于测试中的p值来确定一种舍弃特征的方法(所有舍弃参数的方法都使用阈值来舍弃p值过大的特征,意味着它们不可能与目标值相关)。计算阈值的方法各有不同,最简单的是SelectKBest和SelectPercentile,前者选择固定数量的k个特征,后者选择固定百分比的特征。

from sklearn.datasets import load_breast_cancer
from sklearn.feature_selection import SelectPercentile
from sklearn.model_selection import train_test_split

cancer = load_breast_cancer()

# get deterministic random numbers
rng = np.random.RandomState(42)
noise = rng.normal(size=(len(cancer.data), 50))
# add noise features to the data
# the first 30 features are from the dataset, the next 50 are noise
X_w_noise = np.hstack([cancer.data, noise])

X_train, X_test, y_train, y_test = train_test_split(
    X_w_noise, cancer.target, random_state=0, test_size=.5)
# use f_classif (the default) and SelectPercentile to select 50% of features
select = SelectPercentile(percentile=50)
select.fit(X_
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值