Lasso regression for feature importance
In the video, you saw how lasso regression can be used to identify important features in a dataset.
In this exercise, you will fit a lasso regression model to the sales_df
data and plot the model's coefficients.
The feature and target variable arrays have been pre-loaded as X
and y
, along with sales_columns
, which contains the dataset's feature names.
This exercise is part of the course
Supervised Learning with scikit-learn
Exercise instructions
- Import
Lasso
fromsklearn.linear_model
. - Instantiate a Lasso regressor with an alpha of
0.3
. - Fit the model to the data.
- Compute the model's coefficients, storing as
lasso_coef
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import Lasso
from ____.____ import ____
# Instantiate a lasso regression model
lasso = ____
# Fit the model to the data
____
# Compute and print the coefficients
lasso_coef = ____
print(lasso_coef)
plt.bar(sales_columns, lasso_coef)
plt.xticks(rotation=45)
plt.show()