Read#Feature selection using Wrapper methods in Python

Feature selection using Wrapper methods in Python

To perform any machine learning task or to get insights from such high dimensional data

Feature selection:

  • Training faster
  • Reduce complexity
  • Better prediction
  • Reduce overfitting

Feature selection methods:

  • Filter method
  • Wrapper method
  • Embedded method
    Difference

Wrapper method

(based on specific machine learning algorithm)
It follows a greedy search approach by evaluating all the possible combination of feature against evaluation criterion. Finally, it selects the combination of features that gives the optimal results for the specified ML algorithm.
在这里插入图片描述
Most common method in wrapper :
1.Forward selection
2.Backward elimination
3.BI-directional elimination (Stepwise Selection)

in sklearn

1.Forward selection
start with a null model and then fit the model with each individual one at a time and select the feature with the minimum p-value.

In short, the steps for forward selection technique are as follows :
1.Choose a significance level.(显著性水平)
2.Fit model one feature at a time. Select the feature with the lowest p-value.
3.Fit all possible models with one extra feature.
4.Again,select the feature with minimum p-value.if p-value < significance level then go to step 3 , otherwise terminate the process.

from sklearn.datasets import load_bosto
import pandas as pd
import statsmodels.api as sm
boston = load_boston()
bos = pd.DataFrame(boston.data, columns=boston.feature_names)
bos['Price'] = boston.target
X = bos.drop("Price", 1)
y = bos['Price']
bos.head()
def forward_selection(data, target, significance_level=0.05):
    initial_features = data.columns.tolist()
    best_features = []
    while (len(list(initial_features))>0):
        remaining_features = list(set(initial_features)-set(best_features))
        new_pval = pd.Series(index=remaining_features)
        for new_column in remaining_features:
            model = sm.OLS(target, sm.add_constant(data[best_features+[new_column]])).fit()
            new_pval[new_column] = model.pvalues[new_column]
        min_p_value = new_pval.min()
        if(min_p_value<significance_level):
            best_features.append(new_pval.idxmin())
        else:
            break
    return best_features

print(forward_selection(X,y))

2. Backward elimination
In short, the steps for backward elimination technique are as follows :
1.Choose a significance level (e.g. SL = 0.05 with a 95% confidence).
2.Fit a full model including all the features.
3.Consider the feature with highest p-value. If the p-value > significance level then go to Step 4, otherwise terminate the process.
4.Remove the feature which is under consideration.
5.Fit a model without this feature. Repeat the entire process from Step 3.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值