9951 explained code solutions for 126 technologies


python-scikit-learnGet Lasso model coefficients


from sklearn import datasets, linear_model, model_selection

X, y = datasets.load_diabetes(return_X_y=True)
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y)

model = linear_model.Lasso()
model.fit(X_train, y_train)

coeffs = model.coef_
sparse = model.sparse_coef_
intercept = model.intercept_ctrl + c
from sklearn import

import module from scikit-learn

datasets.load_diabetes

loads sample diabetes database

model_selection.train_test_split

splits given X and y datasets to test (25% of values by default) and train (75% of values by default) subsets

.Lasso(

create Lasso model object

.fit(

train model with a given features and target variable dataset

.coef_

returns list of coefficients of a trained model

.intercept_

value of linear model intercept after training

sparse_coef_

sparse representation of the fitted coefficients


Usage example

from sklearn import datasets, linear_model, model_selection

X, y = datasets.load_diabetes(return_X_y=True)
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y)

model = linear_model.Lasso()
model.fit(X_train, y_train)

coeffs = model.coef_
sparse = model.sparse_coef_
intercept = model.intercept_  

print(coeffs, sparse, intercept)
output
[  0.          -0.         378.42448976  22.84904289   0.
   0.          -0.           0.         302.34461535   0.        ]   (0, 2) 378.4244897581937
  (0, 3)    22.8490428882718
  (0, 8)    302.3446153480934 151.67182691818425