Accessing Lasso Regression coefficients after fitting

11,732

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!
Share:
11,732
RAM
Author by

RAM

Updated on June 04, 2022

Comments

  • RAM
    RAM almost 2 years

    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?

  • AlSub
    AlSub over 2 years
    Is it possible to know which is the feature name assignated to each coefficient?
  • spectre
    spectre over 2 years
    @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.