Introduction

Welcome back! In this lesson, we will delve into another powerful technique for feature selection — SelectFromModel. This technique is particularly useful when you have a trained model and want to select the most important features based on the model's importance criterion.

SelectFromModel is a meta-transformer that can be used along with any estimator that assigns importance to each feature through a specific attribute (like coef_ or feature_importances_). Later, you can set a threshold, and SelectFromModel will consider those features whose importance is more than this threshold.

So, in essence, SelectFromModel does the heavy lifting of identifying and choosing the right features based on the model's importance criterion — a significant advantage for any machine learning practitioner!

Exploring the California Housing Dataset

While discussing dimensionality reduction and feature selection remains crucial, these theories gain relevance and become more comprehensible when we apply them to real-life datasets. For the purpose of our lesson, we shall work with the California Housing dataset available in scikit-learn's set of datasets.

Let's begin by loading and briefly exploring the dataset:

Python
# Import necessary libraries
from sklearn.datasets import fetch_california_housing

# Load California housing dataset
housing = fetch_california_housing()
X = housing.data
Y = housing.target

The California housing dataset encapsulates data on multiple variables like the average number of rooms, average income, population, etc., in various housing blocks in California. Each of these variables might exhibit a distinct influence on the median housing prices, our target variable in this dataset. Managing such a dataset makes feature selection quite necessary!

After loading 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 only on applying SelectFromModel for feature selection.

Creating and Training a Linear Regression Model

Before moving on to feature selection, let's understand why we need a model to perform feature selection. The model helps us determine the importance of each feature in predicting the target variable. This importance is then used by SelectFromModel to select the most important features. Hence, the model acts as a guiding light in our feature selection journey.

For demo purposes, we'll use a simple Linear Regression model where it will learn the relationship between the independent variables (features) and the dependent variable (housing prices).

Let's fit our model:

from sklearn.linear_model import LinearRegression

# Fitting linear regression model on the data
lr = LinearRegression()
lr.fit(X_train, Y_train)

Great! Now that we have a trained Linear Regression model, let's use it with SelectFromModel to perform feature selection.

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