3

In the LightGBM documentation it is stated that one can set predict_contrib=True to predict the SHAP-values.

How do we extract the SHAP-values (apart from using the shap package)?

I have tried

model = LGBM(objective="binary",is_unbalance=True,predict_contrib=True)
model.fit(X_train,y_train)
pred_shap = opt_model.predict(X_train) #Does not get SHAP-values

which does not seem to work

Sergey Bushmanov
  • 23,310
  • 7
  • 53
  • 72
CutePoison
  • 4,679
  • 5
  • 28
  • 63

2 Answers2

5

Shap values the LGBM way with pred_contrib=True:

from lightgbm.sklearn import LGBMClassifier
from sklearn.datasets import load_iris

X,y = load_iris(return_X_y=True)
lgbm = LGBMClassifier()
lgbm.fit(X,y)
lgbm_shap = lgbm.predict(X, pred_contrib=True)
# Shape of returned LGBM shap values: 4 features x 3 classes + 3 expected values over the training dataset
print(lgbm_shap.shape)
# 0th row of LGBM shap values for 0th feature
print(lgbm_shap[0,:4])

Output:

(150, 15)
[-0.0176954   0.50644615  5.56584344  3.43032313]

Shap values from shap:

import shap
explainer = shap.TreeExplainer(lgbm)
shap_values = explainer.shap_values(X)
# num of predicted classes
print(len(shap_values))
# shap values for 0th class for 0th row
print(shap_values[0][0])

Output:

3
array([-0.0176954 ,  0.50644615,  5.56584344,  3.43032313])

Looks the same to me.

Sergey Bushmanov
  • 23,310
  • 7
  • 53
  • 72
3

The confusion arises because of the duplication (inconsistent naming) of the controlling parameter in two different lightgbm APIs.

Each of the two major APIs uses its own spelling:

And the documentation favors the C version (the python API spelling is not even considered an alias...)

mirekphd
  • 4,799
  • 3
  • 38
  • 59