How to do recursive feature elimination in Python?
FEATURE EXTRACTION DATA CLEANING PYTHON DATA MUNGING MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

# How to do recursive feature elimination in Python?

This recipe helps you do recursive feature elimination in Python

0

## Recipe Objective

To increse the score of the model we need to remove the features which are recursive. Removing recursive feature reduces the computational cost and increase the efficiency.

So this is the recipe on How we can do recursive feature elimination in Python.

## Step 1 - Import the library

``` from sklearn.datasets import make_regression from sklearn.feature_selection import RFECV from sklearn import linear_model ```

We have only imported datasets to import the datasets, RFECV and liner_model.

## Step 2 - Setting up the Data

We have imported inbuilt boston dataset and stored data in X and target in y. We have also used print statement to print rows of the dataset. ``` dataset = datasets.load_boston() X = dataset.data y = dataset.target ```

## Step 3 - Selecting recursive Features

We have used linear Regression as a model and RFECV is used for recursive feature elimination we have used negative mean squared error as a scoring with cross validation as 4. We have fit and transform rfecv. ``` ols = linear_model.LinearRegression() rfecv = RFECV(estimator=ols, step=1, scoring="neg_mean_squared_error", cv=4, verbose=0, n_jobs=4) rfecv.fit(X, y) rfecv.transform(X) print(rfecv) print(rfecv.n_features_) ``` So the output comes as

```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)

6
```

#### Relevant Projects

##### Census Income Data Set Project - Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based on census data.

##### Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

##### Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

##### Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

##### Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

##### German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

##### Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

##### Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

##### Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

##### Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.