Shap: error in v0.30.0

Created on 5 Sep 2019  路  1Comment  路  Source: slundberg/shap

>>> lightgbm.__version__  | >>> shap.__version__
'2.2.3'                   | '0.30.0'
KeyError                                  Traceback (most recent call last)
<ipython-input-9-b1e916a6cf78> in <module>
     10 model_mat_gbm = lgb.Booster(model_file=f'{MAT_OUT}/{INST}_gbm.txt')
     11 expl_mat = shap.TreeExplainer(model_mat_gbm)
---> 12 shap_mat = expl_mat.shap_values(valid_mat)
     13 print('  shap_mat: {:,} x {:,}'.format(*shap_mat.shape))
     14 

~/.local/lib/python3.6/site-packages/shap/explainers/tree.py in shap_values(self, X, y, tree_limit, approximate)
    192                 phi = self.model.original_model.predict(X, num_iteration=tree_limit, pred_contrib=True)
    193                 # Note: the data must be joined on the last axis
--> 194                 if self.model.original_model.params['objective'] == 'binary':
    195                     warnings.warn('LightGBM binary classifier with TreeExplainer shap values output has changed to a list of ndarray')
    196                     phi = np.concatenate((-phi, phi), axis=-1)

KeyError: 'objective'

Most helpful comment

As a workaround you can try adding the 'objective' key to your lgb.Booster.param dict before initailizing your explainer, so if your model is called clf:

clf.params['objective'] = 'binary'
explainer = shap.TreeExplainer(clf)
shap_vals = explainer.shap_values(X)

>All comments

As a workaround you can try adding the 'objective' key to your lgb.Booster.param dict before initailizing your explainer, so if your model is called clf:

clf.params['objective'] = 'binary'
explainer = shap.TreeExplainer(clf)
shap_vals = explainer.shap_values(X)
Was this page helpful?
0 / 5 - 0 ratings