AttributeError: 'Kernel' object has no attribute 'masker' during training of SVC with rbf kernel

1.6k views Asked by At

I am training my SVC model and I am getting this error with the Shap Library. Have tried some methods including shap.maskers.Independent but the same error keeps appearing. Am I missing out something?

Codes

model = SVC (C = 10.0, gamma = 0.01, kernel = 'rbf', probability=True)
model.fit(X_resampled,y_resampled)
y_predict= model.predict_proba(X_resampled_test)[:,1] # for AUC calculation, ploting ROC-AUC and Precision_

from sklearn.metrics import log_loss
print("Log-loss on logit: {:6.4f}".format(log_loss(y_resampled_test, y_predict)))

plt.figure()
# # #Get shap values

# feature_names = X_train.columns
explainer = shap.KernelExplainer(model.predict_proba, X_resampled, link="logit")
shap_values = explainer(X_resampled, X_resampled_test) 

Error

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_22576/2256358008.py in <module>
      4 # feature_names = X_train.columns
      5 explainer = shap.KernelExplainer(model.predict_proba, X_resampled, link="logit")
----> 6 shap_values = explainer(X_resampled, X_resampled_test)

c:\Users\danie\anaconda3\lib\site-packages\shap\explainers\_explainer.py in __call__(self, max_evals, main_effects, error_bounds, batch_size, outputs, silent, *args, **kwargs)
    205         start_time = time.time()
    206 
--> 207         if issubclass(type(self.masker), maskers.OutputComposite) and len(args)==2:
    208             self.masker.model = models.TextGeneration(target_sentences=args[1])
    209             args = args[:1]

AttributeError: 'Kernel' object has no attribute 'masker
1

There are 1 answers

0
Sergey Bushmanov On

You may wish to try the following:

from sklearn.datasets import make_classification
from sklearn.svm import SVC
import shap
shap.initjs()

X, y = make_classification(1000)

model = SVC(C = 10.0, gamma = 0.01, kernel = 'rbf', probability=True)
model.fit(X , y)

masker = shap.maskers.Independent(X, 10)
explainer = shap.KernelExplainer(model.predict_proba, masker.data)

shap_values = explainer.shap_values(X)
idx = 0
shap.force_plot(explainer.expected_value[0], shap_values[0][idx], masker.data[idx])

enter image description here