Recipe: How to do recursive feature elimination in Python?
FEATURE EXTRACTION

How to do recursive feature elimination in Python?

This recipe helps you do recursive feature elimination in Python
In [1]:
## How to do recursive feature elimination in Python
def Snippet_128():
    print()
    print(format('How to do recursive feature elimination in Python','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    from sklearn.datasets import make_regression
    from sklearn.feature_selection import RFECV
    from sklearn import linear_model

    # Create Data
    # Generate features matrix, target vector, and the true coefficients
    X, y = make_regression(n_samples = 10000, n_features = 100, n_informative = 2)
    print(); print(X.shape)

    # Create Linear Model
    ols = linear_model.LinearRegression()

    # Create recursive feature eliminator that scores features by mean squared errors
    rfecv = RFECV(estimator=ols, step=1, scoring='neg_mean_squared_error', cv=4, verbose=0,
                  n_jobs=4)

    # Fit recursive feature eliminator 
    rfecv.fit(X, y)

    # Recursive feature elimination
    rfecv.transform(X)

    # Number of best features
    print(); print(rfecv)
    print(); print(rfecv.n_features_)

Snippet_128()
****************How to do recursive feature elimination in Python*****************

(10000, 100)

RFECV(cv=4,
   estimator=LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None,
         normalize=False),
   min_features_to_select=1, n_jobs=4, scoring='neg_mean_squared_error',
   step=1, verbose=0)

4


Stuck at work?
Can't find the recipe you are looking for. Let us know and we will find an expert to create the recipe for you. Click here
Companies using this Recipe
1 developer from HvH
1 developer from Wipro
1 developer from Amazon
1 developer from ICU Medical
1 developer from ANAC
1 developer from Porteck
1 developer from Vodafone