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

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

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.

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.

Build an AI Chatbot from Scratch using Keras Sequential Model
In this NLP Project, you will learn how to build an AI Chatbot from Scratch using Keras Sequential Model.

Time Series Classification Project for Elevator Failure Prediction
In this Time Series Project, you will predict the failure of elevators using IoT sensor data as a time series classification machine learning problem.

Llama2 Project for MetaData Generation using FAISS and RAGs
In this LLM Llama2 Project, you will automate metadata generation using Llama2, RAGs, and AWS to reduce manual efforts.

MLOps AWS Project on Topic Modeling using Gunicorn Flask
In this project we will see the end-to-end machine learning development process to design, build and manage reproducible, testable, and evolvable machine learning models by using AWS

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

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..

Loan Default Prediction Project using Explainable AI ML Models
Loan Default Prediction Project that employs sophisticated machine learning models, such as XGBoost and Random Forest and delves deep into the realm of Explainable AI, ensuring every prediction is transparent and understandable.