Skip to content

Commit

Permalink
Make shap an extra dependency (#376)
Browse files Browse the repository at this point in the history
* Make shap an extra dependency

* Add note in method notebooks

* Install shap in CI

* Add shap to licence check

* Handle absence of shap

* Simplify installation command with shap

* Test on latest dependencies on CI
  • Loading branch information
jklaise authored Mar 31, 2021
1 parent 20c641f commit e358950
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 13 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ jobs:
- name: Install dependencies
run: |
pip install --upgrade pip setuptools
pip install -r requirements/dev.txt
pip install --upgrade --upgrade-strategy eager -r requirements/dev.txt
python -m spacy download en_core_web_md
pip install -e .
pip install --upgrade --upgrade-strategy eager -e .
pip install --upgrade --upgrade-strategy eager -e .[shap]
pip freeze
- name: Lint with flake8
run: |
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ To take advantage of distributed computation of explanations, install `alibi` wi
pip install alibi[ray]
```

For SHAP support, install `alibi` as follows:
```bash
pip install alibi && pip install alibi[shap]
```

The alibi explanation API takes inspiration from `scikit-learn`, consisting of distinct initialize,
fit and explain steps. We will use the [AnchorTabular](https://docs.seldon.io/projects/alibi/en/latest/methods/Anchors.html)
explainer to illustrate the API:
Expand Down
9 changes: 6 additions & 3 deletions alibi/explainers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from .cem import CEM
from .cfproto import CounterFactualProto
from .counterfactual import CounterFactual
from .shap_wrappers import KernelShap, TreeShap
from .integrated_gradients import IntegratedGradients

__all__ = ["ALE",
Expand All @@ -20,8 +19,12 @@
"CEM",
"CounterFactual",
"CounterFactualProto",
"KernelShap",
"TreeShap",
"plot_ale",
"IntegratedGradients"
]

try:
from .shap_wrappers import KernelShap, TreeShap
__all__ += ["KernelShap", "TreeShap"]
except ImportError:
pass
14 changes: 14 additions & 0 deletions doc/source/methods/KernelSHAP.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@
"# Kernel SHAP"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-info\">\n",
"To enable SHAP support, you may need to run\n",
" \n",
"```bash\n",
"pip install alibi[shap]\n",
"```\n",
"\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
14 changes: 14 additions & 0 deletions doc/source/methods/TreeSHAP.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@
"# Tree SHAP"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-info\">\n",
"To enable SHAP support, you may need to run\n",
" \n",
"```bash\n",
"pip install alibi[shap]\n",
"```\n",
"\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
16 changes: 15 additions & 1 deletion examples/interventional_tree_shap_adult_xgb.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
"# Explaining Tree Models with Interventional Feature Perturbation Tree SHAP"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-info\">\n",
"To enable SHAP support, you may need to run\n",
" \n",
"```bash\n",
"pip install alibi[shap]\n",
"```\n",
"\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -1369,7 +1383,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
"version": "3.8.5"
}
},
"nbformat": 4,
Expand Down
16 changes: 15 additions & 1 deletion examples/kernel_shap_adult_categorical_preproc.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
"# KernelSHAP: combining preprocessor and predictor"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-info\">\n",
"To enable SHAP support, you may need to run\n",
" \n",
"```bash\n",
"pip install alibi[shap]\n",
"```\n",
"\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -968,7 +982,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
"version": "3.8.5"
}
},
"nbformat": 4,
Expand Down
16 changes: 15 additions & 1 deletion examples/kernel_shap_adult_lr.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
"# Handling categorical variables with KernelSHAP "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-info\">\n",
"To enable SHAP support, you may need to run\n",
" \n",
"```bash\n",
"pip install alibi[shap]\n",
"```\n",
"\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -2462,7 +2476,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
"version": "3.8.5"
}
},
"nbformat": 4,
Expand Down
16 changes: 15 additions & 1 deletion examples/kernel_shap_wine_intro.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
"# Kernel SHAP explanation for SVM models "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-info\">\n",
"To enable SHAP support, you may need to run\n",
" \n",
"```bash\n",
"pip install alibi[shap]\n",
"```\n",
"\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -1197,7 +1211,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
"version": "3.8.5"
}
},
"nbformat": 4,
Expand Down
16 changes: 15 additions & 1 deletion examples/kernel_shap_wine_lr.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
"# Kernel SHAP explanation for multinomial logistic regression models "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-info\">\n",
"To enable SHAP support, you may need to run\n",
" \n",
"```bash\n",
"pip install alibi[shap]\n",
"```\n",
"\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -937,7 +951,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
"version": "3.8.5"
}
},
"nbformat": 4,
Expand Down
16 changes: 15 additions & 1 deletion examples/path_dependent_tree_shap_adult_xgb.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
"# Explaining Tree Models with Path-Dependent Feature Perturbation Tree SHAP"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-info\">\n",
"To enable SHAP support, you may need to run\n",
" \n",
"```bash\n",
"pip install alibi[shap]\n",
"```\n",
"\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -1454,7 +1468,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
"version": "3.8.5"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ deps =
dataclasses # required with py36
contextvars # required with py36
immutables # required with py36
extras = all
extras = shap
commands =
pip-licenses \
--from=mixed \
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def readme():
extras_require = {
'examples': ['seaborn>=0.9.0', 'xgboost>=0.90'],
'ray': ['ray>=0.8.7, <2.0.0'], # from requirements/dev.txt
# shap is separated due to build issues, see https://github.com/slundberg/shap/pull/1802
'shap': ['shap>=0.36.0, !=0.38.1, <0.40.0'], # versioning: https://github.com/SeldonIO/alibi/issues/333
}

setup(name='alibi',
Expand All @@ -37,7 +39,6 @@ def readme():
'Pillow>=5.4.1, <9.0',
'tensorflow>=2.0.0, <2.5.0',
'attrs>=19.2.0, <21.0.0',
'shap>=0.36.0, !=0.38.1, <0.40.0', # https://github.com/SeldonIO/alibi/issues/333
'scipy>=1.1.0, <2.0.0',
'matplotlib>=3.0.0, <4.0.0',
'typing-extensions>=3.7.2; python_version < "3.8"', # https://github.com/SeldonIO/alibi/pull/248
Expand Down

0 comments on commit e358950

Please sign in to comment.