Accessing Lasso Regression coefficients after fitting
Asked Answered
R

1

7

I'm trying to Lasso Regression after having optimal value of Lambda and now the problem is , I want to get the coefficients (weight vector) since I want to compare them with weights of Ridge regression.

lasso = Lasso(alpha=optimal_lmbda, fit_intercept=True, random_state=1142, max_iter=5000)
lasso.fit(X_train, y_train)
y_pred_lasso = lasso.predict(X_test)

How to get coefficients(weight vectors) after fitting in Lasso Regression in python in Sklearn?

Reid answered 28/11, 2017 at 0:5 Comment(0)
U
8

Just follow the docs for sklearn.linear_model.Lasso

# Build lasso and fit
lasso = Lasso(...)
lasso.fit(...)

# Read out attributes
coeffs = lasso.coef_         # dense np.array
coeffs = lasso.sparse_coef_  # sparse matrix

coeffs = lasso.intercept_    # probably also relevant

Updated from comment:

  • What is the difference between lasso.coef_ and lasso.sparse_coef_
    • The type, as explained in the in-line comment and the docs. numpy-array or sparse-matrix (from scipy.sparse). The latter being relevant, if you have many variables, and many of those are zero, because of strong regularization (l1 known to effect in many zeros). Then it's useless to store 0's, that's what sparse data-structures are for. There is no dense-vector-type in scipy, therefore the use of sparse matrices. The content is the same!
Unfortunate answered 28/11, 2017 at 0:12 Comment(2)
Is it possible to know which is the feature name assignated to each coefficient?Askwith
@AlvaroMartinez Once you get the coefficients, just do this np.array(df.columns)[coeff==0]. This will give you all the features for which Lasso has shrunk the coeff to 0. Similary just replace ==0 with >0 to get features for which Lasso has not shrunk the coeff to 0.Quondam

© 2022 - 2024 — McMap. All rights reserved.