Is there any way to get variable importance with Keras?

喜你入骨 提交于 2019-12-02 19:35:13

Since everything will be mixed up along the network, the first layer alone can't tell you about the importante of each var. The following layers can also increase or decrease their importance, and even make one var affect the importance of another var. Every single neuron in the first layer itself will give each var a different importance too, so it's not something that straightforward.

I suggest you do model.predict(inputs) using inputs containing arrays of zeros, making only the variable you want to study be 1 in the input.

That way, you see the result for each var alone. Even though, this will still not help you with the cases where one var increases the importance of another var.

It is not that simple. For example, in later stages the variable could be reduced to 0.

I'd have a look at LIME (Local Interpretable Model-Agnostic Explanations). The basic idea is to set some inputs to zero, pass it through the model and see if the result is similar. If yes, then that variable might not be that important. But there is more about it and if you want to know it, then you should read the paper.

See marcotcr/lime on GitHub.

*Edited to include relevant code to implement permutation importance.

I answered a similar question at Feature Importance Chart in neural network using Keras in Python. It does implement what Teque5 mentioned above, namely shuffling the variable among your sample or permutation importance using the ELI5 package.

from keras.wrappers.scikit_learn import KerasClassifier, KerasRegressor
import eli5
from eli5.sklearn import PermutationImportance

def base_model():
    model = Sequential()        
    ...
    return model

X = ...
y = ...

my_model = KerasRegressor(build_fn=basemodel, **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())
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!