Search code examples
pythonmatplotlibkerasdeep-learningshap

How to save shap summary_plot as image file


I have the following script that is working

import numpy as np
import shap
from tensorflow import keras
from tensorflow.keras import layers
X = np.array([[(1,2,3,3,1),(3,2,1,3,2),(3,2,2,3,3),(2,2,1,1,2),(2,1,1,1,1)],
              [(4,5,6,4,4),(5,6,4,3,2),(5,5,6,1,3),(3,3,3,2,2),(2,3,3,2,1)],
              [(7,8,9,4,7),(7,7,6,7,8),(5,8,7,8,8),(6,7,6,7,8),(5,7,6,6,6)],
              [(7,8,9,8,6),(6,6,7,8,6),(8,7,8,8,8),(8,6,7,8,7),(8,6,7,8,8)],
              [(4,5,6,5,5),(5,5,5,6,4),(6,5,5,5,6),(4,4,3,3,3),(5,5,4,4,5)],
              [(4,5,6,5,5),(5,5,5,6,4),(6,5,5,5,6),(4,4,3,3,3),(5,5,4,4,5)],
              [(1,2,3,3,1),(3,2,1,3,2),(3,2,2,3,3),(2,2,1,1,2),(2,1,1,1,1)]])
y = np.array([0, 1, 2, 2, 1, 1, 0])

# Updated model with correct input shape
model = keras.Sequential([
    layers.Conv1D(128, kernel_size=3, activation='relu',input_shape=(5,5)),
    layers.MaxPooling1D(pool_size=2),
    layers.LSTM(128, return_sequences=True),
    layers.Flatten(),
    layers.Dense(128, activation='relu'),
    layers.Dense(5, activation='softmax')  # Adjust the number of output units based on your problem (3 for 3 classes)
])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Train the model
model.fit(X, y, epochs=10)

explainer = shap.GradientExplainer(model, X)
shap_values = explainer.shap_values(X)
#print(shap_values)

cls = 0
idx = 0
shap.summary_plot(shap_values[cls][:,idx,:], X[:,idx,:])

I want to save shap.summary_plot as an image file in my folder. How can I do that?

I am trying the following code but it is saving an empty figure.

# Save the plot using matplotlib
import matplotlib.pyplot as plt

save_path = 'shap_summary_plot.png'
plt.savefig(save_path)
plt.close()

Anyone know how to plot this?


Solution

  • Initialize a matplotlib figure object first and plot the summary plot. Then interact with this figure object to save, close etc. In other words, try the following code:

    import matplotlib.pyplot as plt
    fig = plt.figure()             # <---- initialize figure `fig`
    shap.summary_plot(shap_values[cls][:,idx,:], X[:,idx,:])
    save_path = 'shap_summary_plot.png'
    fig.savefig(save_path)         # <---- save `fig` (not current figure)
    plt.close(fig)                 # <---- close `fig`