I am using python(3.6) anaconda (64 bit) spyder (3.1.2). I already set a neural network model using keras (2.0.6) for a regression problem(one response, 10 variables). I was wondering how can I generate feature importance chart like so:
def base_model():model = Sequential()model.add(Dense(200, input_dim=10, kernel_initializer='normal', activation='relu'))model.add(Dense(1, kernel_initializer='normal'))model.compile(loss='mean_squared_error', optimizer = 'adam')return modelclf = KerasRegressor(build_fn=base_model, epochs=100, batch_size=5,verbose=0)
clf.fit(X_train,Y_train)
I was recently looking for the answer to this question and found something that was useful for what I was doing and thought it would be helpful to share. I ended up using a permutation importance module from the eli5 package. It most easily works with a scikit-learn model. Luckily, Keras provides a wrapper for sequential models. As shown in the code below, using it is very straightforward.
from keras.wrappers.scikit_learn import KerasClassifier, KerasRegressor
import eli5
from eli5.sklearn import PermutationImportancedef base_model():model = Sequential() ...return modelX = ...
y = ...my_model = KerasRegressor(build_fn=base_model, **sk_params)
my_model.fit(X,y)perm = PermutationImportance(my_model, random_state=1).fit(X,y)
eli5.show_weights(perm, feature_names = X.columns.tolist())