0

I get the following error when running my code using shap for my CNN model: AssertionError: AssertionError:Instance must have 1 or 2 dimensions!` - I would appreciate any advice

Using 1484 background data samples could cause slower run times. Consider using shap.sample(data, K) or shap.kmeans(data, K) to summarize the background as K samples.

AssertionError Traceback (most recent call last) /tmp/ipykernel_193211/2006523994.py in <cell line: 7>() 5 # Generate SHAP values for a random test image 6 # shape (sample, height, width, channels) ----> 7 shap_values = explainer.shap_values(X_test) 8 shap.initjs() 9 # Plot the SHAP values for the test image

~/anaconda3/envs/tensorflow2_p310/lib/python3.10/site-packages/shap/explainers/_kernel.py in shap_values(self, X, **kwargs) 154 X = X.tolil() 155 assert x_type.endswith(arr_type) or sp.sparse.isspmatrix_lil(X), "Unknown instance type: " + x_type --> 156 assert len(X.shape) == 1 or len(X.shape) == 2, "Instance must have 1 or 2 dimensions!" 157 158 # single instance * AssertionError: Instance must have 1 or 2 dimensions!

My code: import shap # Define the SHAP explainer object explainer = shap.KernelExplainer(best_model, X_test)

# Generate SHAP values for a test image
# shape (nrsamples, height, width, channels)
shap_values = explainer.shap_values(X_test)
shap.initjs()
# Plot the SHAP values for the test image
shap.summary_plot(shap_values, X_test)

Dimensions of X_test: X_test.shape (1484, 74, 62, 119)

  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 19 '23 at 23:52

0 Answers0