Introduction to Recursive Feature Elimination

Welcome! Today's topic is an essential technique in data science and machine learning, called Recursive Feature Elimination (RFE). It's a method used for feature selection, that is, for choosing the relevant input variables in our training data.

In Recursive Feature Elimination, we initially fit the model using all available features. Then we recursively eliminate the least important features and fit the model again. We continue this process until we are left with the specified number of features. What we achieve at the end is a model that's potentially more efficient and performs better.

Sound exciting? Let's go ahead and dive into action!

Understanding the Recursive Feature Elimination

The concept of Recursive Feature Elimination is simple yet powerful. It is based on the idea of recursively removing the least important features from the model. The process involves the following steps:

  1. Fit the model using all available features.
  2. Rank the features based on their importance to the model using a specific criterion (like coefficients, feature importance, etc.).
  3. Remove the least important feature(s) from the model.
  4. Repeat steps 1-3 until the desired number of features is reached.
Data Generation With Scikit-learn

Our exploration of RFE starts with generating some data. We will use a utility from Scikit-learn called make_classification to create a mock (synthetic) dataset. It is extremely useful for trying out different algorithms and understanding their impacts. Here is how we do it.

Python
# Import necessary libraries
from sklearn.datasets import make_classification

# Creating a mock data
X, y = make_classification(n_samples=1000, n_features=10, n_informative=5, n_redundant=5, random_state=1)

The make_classification function generates a random n-class classification problem. In the above example, we've generated data with 1000 samples and 10 features, out of which only 5 features are informative. The rest 5 features are redundant.

After generating the dataset, we can split the data into training and testing sets using the train_test_split function from Scikit-learn, so that we can train our model on the training data and evaluate it on the testing data. We will skip this step in this lesson, and focus on applying RFE directly.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal