I am currently using SHAP Package to determine the feature contributions. I have used the approach for XGBoost and RandomForest and it worked really well. Since the data I am working on is a sequential data I tried using LSTM and CNN to train the model and then get the feature importance using the SHAP's DeepExplainer
; but it is continuously throwing error. The error I am getting is:
AssertionError: <class 'keras.callbacks.History'> is not currently a supported model type!.
I am attaching the sample code as well (LSTM). It would be helpful if someone could help me with it.
shap.initjs()
model = Sequential()
model.add(LSTM(n_neurons, input_shape=(X.shape[1],X.shape[2]), return_sequences=True))
model.add(LSTM(n_neurons, return_sequences=False))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
h=model.fit(X, y, epochs=nb_epochs, batch_size=n_batch, verbose=1, shuffle=True)
background = X[np.random.choice(X.shape[0],100, replace=False)]
explainer = shap.DeepExplainer(h,background)
SHAP values (SHapley Additive exPlanations) is an awesome tool to understand your complex Neural network models and other machine learning models such as Decision trees, Random forests. Basically, it visually shows you which feature is important for making predictions.
Explain Image Classification by SHAP Deep ExplainerImage classification tasks can be explained by the scores on each pixel on a predicted image, which indicates how much it contributes to classifying that image into a particular class.
An example in Python with neural networksLet's first install shap library. Then, let's import it and other useful libraries. Now we can load our dataset and the feature names, that will be useful later. We can now split our dataset into training and test.
The returned value of model.fit
is not the model instance; rather, it's the history of training (i.e. stats like loss and metric values) as an instance of keras.callbacks.History
class. That's why you get the mentioned error when you pass the returned History
object to shap.DeepExplainer
. Instead, you should pass the model instance itself:
explainer = shap.DeepExplainer(model, background)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With