Accessing Lasso Regression coefficients after fitting

Viewed 14259

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?

1 Answers

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!
Related