Search code examples
pythonmachine-learningscikit-learnsvmshap

SHAP values cant compute shap explainer on SVM model


I am trying to compute shape explainer so i can visualize my model. However I keep getting the following error:

Exception: The passed model is not callable and cannot be analyzed directly with 
the given masker! Model: SVC(C=300, probability=True)

my code:

model =  create_model(SVC, C=300, probability=True) #user defined function works right
model.fit(X_train, y_train)

explainer = shap.Explainer(model)
shap_values = explainer.shap_values(X_test)
 
shap.initjs()
shap.force_plot(explainer.expected_value,  X_train)

when I tried KernelExplainer along with X_train I got:

TypeError: 'SVC' object is not callable

Edit:

Here is the create_model function which takes the training variables and other model keywords as parameters:

def create_model(X_train, y_train, model, **kwargs):
    created_model = model(**kwargs)
    created_model.fit(X_train,y_train)
    
    return created_model

Also here is the progress bar after trying recommended answer: progress bar screenshot


Solution

  • First off, you need to pass your model's predict method, not the model on its own.

    Second, (at least on my setup) Explainer cannot automatically determine a suitable explainer for SVC, so you might want to call an appropriate explainer directly, e.g. KernelExplainer:

    explainer = shap.KernelExplainer(model.predict)