get feature improtance from a lasso regressino in python

In order to get feature importance from a Lasso Regression in Python using Scikit-Learn, we need to follow these steps:

  1. Train the Lasso Regression model
  2. Retrieve the coefficients of the trained Lasso model
  3. Rank the coefficients based on their absolute value magnitude

Here is an example code snippet that performs the above three steps:

main.py
from sklearn.linear_model import Lasso
import numpy as np

# Define your input features and target variable
X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
y = np.array([6, 12, -3, 4])

# Train the Lasso Regression model 
lasso = Lasso(alpha=0.1) # Choose your regularization strength
lasso.fit(X, y)

# Retrieve the coefficients of the trained Lasso model
lasso_coeffs = lasso.coef_

# Rank the coefficients based on their absolute value magnitude
sorted_idx = np.argsort(np.abs(lasso_coeffs))[::-1] # Examine sorted indices of coefficients
for idx in sorted_idx:
    print(f"Feature {idx} importance score: {lasso_coeffs[idx]}")
645 chars
19 lines

The code first generates some toy input/output data. Then, a Lasso regression model is created with a chosen regularization parameter (alpha=0.1). The coefficients of the trained model are then retrieved using the lasso.coef_ command. Finally, the code sorts the coefficients based on their absolute value magnitude (greatest absolute value first) and prints out the feature index and its corresponding coefficient value.

gistlibby LogSnag