%matplotlib inline
import lightgbm as lgb
lgb.__version__
'3.1.1'
import plotly
plotly.__version__
'4.14.3'
import shap
shap.initjs()
shap.__version__
'0.38.1'

logit[source]

logit(p)

Compute log-odds of p

expit[source]

expit(a)

The reverse of logit

class Shap[source]

Shap(X, y, model, n_samples=1000)

Fit a model for X, y, then explain it using SHAP plots

X, y = shap.datasets.adult()
print(y)
X
[False False False ... False False  True]
Age Workclass Education-Num Marital Status Occupation Relationship Race Sex Capital Gain Capital Loss Hours per week Country
0 39.0 7 13.0 4 1 0 4 1 2174.0 0.0 40.0 39
1 50.0 6 13.0 2 4 4 4 1 0.0 0.0 13.0 39
2 38.0 4 9.0 0 6 0 4 1 0.0 0.0 40.0 39
3 53.0 4 7.0 2 6 4 2 1 0.0 0.0 40.0 39
4 28.0 4 13.0 2 10 5 2 0 0.0 0.0 40.0 5
... ... ... ... ... ... ... ... ... ... ... ... ...
32556 27.0 4 12.0 2 13 5 4 0 0.0 0.0 38.0 39
32557 40.0 4 9.0 2 7 4 4 1 0.0 0.0 40.0 39
32558 58.0 4 9.0 6 1 1 4 0 0.0 0.0 40.0 39
32559 22.0 4 9.0 4 1 3 4 1 0.0 0.0 20.0 39
32560 52.0 5 9.0 2 4 5 4 0 15024.0 0.0 40.0 39

32561 rows × 12 columns

from lightgbm.sklearn import LGBMClassifier
model = LGBMClassifier()
sh = Shap(X, y, model)

Shap.waterfall_plot[source]

Shap.waterfall_plot(id:int)

Display SHAP waterfall plot

sh.waterfall_plot(id=0)
import plotly.io as pio
print(pio.renderers.default)
plotly_mimetype+notebook

Shap.wf_plot[source]

Shap.wf_plot(id:int, title='', n_feature=0, n_pos=0, n_neg=0)

Display shap values in a waterfall plot.

Params:

- n_feature: Number of features with larger SHAP values to show in the plot.
- n_pos: Number of features with positive SHAP values to show in the plot.
- n_neg: Number of features with negative SHAP values to show in the plot.
sh.wf_plot(3)
sh.wf_plot(3, n_pos=3, n_neg=3, title="Features' impact")
sh.wf_plot(3, n_feature=8, n_pos=3, n_neg=3)

Shap.bar_plot[source]

Shap.bar_plot(id:int, figsize=None)

Display shap values in a bar plot

sh.bar_plot(id=0, figsize=(5,3))
<AxesSubplot:>

Shap.force_plot[source]

Shap.force_plot(n_plots=1)

Display a SHAP force plot

sh.force_plot()
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must also trust this notebook (File -> Trust notebook). If you are viewing this notebook on github the Javascript has been stripped for security. If you are using JupyterLab this error is because a JupyterLab extension has not yet been written.
sh.force_plot(1000)
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must also trust this notebook (File -> Trust notebook). If you are viewing this notebook on github the Javascript has been stripped for security. If you are using JupyterLab this error is because a JupyterLab extension has not yet been written.