Is there any way to get variable importance with Keras?

后端 未结 4 1102
渐次进展
渐次进展 2020-12-23 17:51

I am looking for a proper or best way to get variable importance in a Neural Network created with Keras. The way I currently do it is I just take the weights (not the biases

相关标签:
4条回答
  • 2020-12-23 18:03

    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.

    0 讨论(0)
  • 2020-12-23 18:04

    Since everything will be mixed up along the network, the first layer alone can't tell you about the importance of each variable. The following layers can also increase or decrease their importance, and even make one variable affect the importance of another variable. Every single neuron in the first layer itself will give each variable 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 variable alone. Even though, this will still not help you with the cases where one variable increases the importance of another variable.

    0 讨论(0)
  • 2020-12-23 18:14

    This is a relatively old post with relatively old answers, so I would like to offer another suggestion of using SHAP to determine feature importance for your Keras models. SHAP also allows you to process Keras models using layers requiring 3d input like LSTM and GRU while eli5 cannot.

    To avoid double-posting, I would like to offer my answer to a similar question on Stackoverflow on using SHAP.

    0 讨论(0)
  • 2020-12-23 18:17

    *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())
    
    0 讨论(0)
提交回复
热议问题