9951 explained code solutions for 126 technologies


python-scikit-learnHow to get Elasticnet feature importance


For ElasticNext model we can use trained coefficients to evaluate feature importance:

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.ElasticNet()
model.fit(X_train, y_train)

fi = model.coef_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

.ElasticNet(

creates ElasticNet training model

.fit(

train model with a given features and target variable dataset

.coef_

returns list of coefficients of a trained model


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.ElasticNet()
model.fit(X_train, y_train)

fi = model.coef_
print(fi)
output
[ 0.15675432  0.          3.33520417  2.10046188  0.44089256  0.27115722
 -2.23505284  2.37683059  3.24743807  1.85651586]