IniziaInizia gratis

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.

Questo esercizio fa parte del corso

Supervised Learning with scikit-learn

Visualizza il corso

Istruzioni dell'esercizio

  • Import Lasso from sklearn.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.

Esercizio pratico interattivo

Prova questo esercizio completando il codice di esempio.

# 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()
Modifica ed esegui il codice