9951 explained code solutions for 126 technologies


python-scikit-learnLinear model feature importance calculation


For linear models, trained coefficients can be used as feature importance values:

from sklearn import datasets, linear_model, model_selection, metrics

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.LinearRegression()
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

.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, metrics

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

fi = model.coef_
print(fi)
output
[   7.4644251  -212.29645468  484.41681905  275.862333   -938.22675656
  587.40072837  114.72619725  120.06905393  872.88971664   45.45492861]