How to do recursive feature elimination in Python?

This recipe helps you do recursive feature elimination in Python

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.

Unleash the Importance of Feature Engineering for Machine Learning Projects

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

Download Materials

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

AWS MLOps Project to Deploy Multiple Linear Regression Model
Build and Deploy a Multiple Linear Regression Model in Python on AWS

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Deploying Machine Learning Models with Flask for Beginners
In this MLOps on GCP project you will learn to deploy a sales forecasting ML Model using Flask.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

Linear Regression Model Project in Python for Beginners Part 1
Machine Learning Linear Regression Project in Python to build a simple linear regression model and master the fundamentals of regression for beginners.

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .