baby-shap


Namebaby-shap JSON
Version 0.0.6 PyPI version JSON
download
home_page
SummaryA stripped and opiniated version of Scott Lundberg's SHAP (SHapley Additive exPlanations)
upload_time2023-01-30 08:00:51
maintainer
docs_urlNone
authorThom Hopmans
requires_python>=3.10
licenseMIT
keywords shap
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            

<p align="center">
  <img src="https://raw.githubusercontent.com/slundberg/shap/master/docs/artwork/shap_header.svg" width="800" />
</p>

---
![example workflow](https://github.com/thomhopmans/baby-shap/actions/workflows/run_tests.yml/badge.svg)

Baby Shap is a stripped and opiniated version of **SHAP (SHapley Additive exPlanations)**, a game theoretic approach to explain the output of any machine learning model by Scott Lundberg. It connects optimal credit allocation with local explanations using the classic Shapley values from game theory and their related extensions (see [papers](#citations) for details and citations). 

**Baby Shap solely implements and maintains the Linear and Kernel Explainer and a limited range of plots, while limiting the number of dependencies, conflicts and raised warnings and errors.**

## Install

Baby SHAP can be installed from either [PyPI](https://pypi.org/project/baby-shap):

<pre>
pip install baby-shap
</pre>

## Model agnostic example with KernelExplainer (explains any function)

Kernel SHAP uses a specially-weighted local linear regression to estimate SHAP values for any model. Below is a simple example for explaining a multi-class SVM on the classic iris dataset.

```python
import baby_shap
from sklearn import datasets, svm, model_selection

# print the JS visualization code to the notebook
baby_shap.initjs()

# train a SVM classifier
d = datasets.load_iris()
X = pd.DataFrame(data=d.data, columns=d.feature_names)
y = d.target

X_train, X_test, Y_train, Y_test = model_selection.train_test_split(X, y, test_size=0.2, random_state=0)
clf = svm.SVC(kernel='rbf', probability=True)
clf.fit(X_train.to_numpy(), Y_train)

# use Kernel SHAP to explain test set predictions
explainer = baby_shap.KernelExplainer(svm.predict_proba, X_train, link="logit")
shap_values = explainer.shap_values(X_test, nsamples=100)

# plot the SHAP values for the Setosa output of the first instance
baby_shap.force_plot(explainer.expected_value[0], shap_values[0][0,:], X_test.iloc[0,:], link="logit")
```
<p align="center">
  <img width="810" src="https://raw.githubusercontent.com/slundberg/shap/master/docs/artwork/iris_instance.png" />
</p>

The above explanation shows four features each contributing to push the model output from the base value (the average model output over the training dataset we passed) towards zero. If there were any features pushing the class label higher they would be shown in red.

If we take many explanations such as the one shown above, rotate them 90 degrees, and then stack them horizontally, we can see explanations for an entire dataset. This is exactly what we do below for all the examples in the iris test set:

```python
# plot the SHAP values for the Setosa output of all instances
baby_shap.force_plot(explainer.expected_value[0], shap_values[0], X_test, link="logit")
```
<p align="center">
  <img width="813" src="https://raw.githubusercontent.com/slundberg/shap/master/docs/artwork/iris_dataset.png" />
</p>

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "baby-shap",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": "",
    "keywords": "shap",
    "author": "Thom Hopmans",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/f7/6b/8499710ecf5543ed7e6a0c662d22fca05aba579bf1da5838d8bedad4289e/baby_shap-0.0.6.tar.gz",
    "platform": null,
    "description": "\n\n<p align=\"center\">\n  <img src=\"https://raw.githubusercontent.com/slundberg/shap/master/docs/artwork/shap_header.svg\" width=\"800\" />\n</p>\n\n---\n![example workflow](https://github.com/thomhopmans/baby-shap/actions/workflows/run_tests.yml/badge.svg)\n\nBaby Shap is a stripped and opiniated version of **SHAP (SHapley Additive exPlanations)**, a game theoretic approach to explain the output of any machine learning model by Scott Lundberg. It connects optimal credit allocation with local explanations using the classic Shapley values from game theory and their related extensions (see [papers](#citations) for details and citations). \n\n**Baby Shap solely implements and maintains the Linear and Kernel Explainer and a limited range of plots, while limiting the number of dependencies, conflicts and raised warnings and errors.**\n\n## Install\n\nBaby SHAP can be installed from either [PyPI](https://pypi.org/project/baby-shap):\n\n<pre>\npip install baby-shap\n</pre>\n\n## Model agnostic example with KernelExplainer (explains any function)\n\nKernel SHAP uses a specially-weighted local linear regression to estimate SHAP values for any model. Below is a simple example for explaining a multi-class SVM on the classic iris dataset.\n\n```python\nimport baby_shap\nfrom sklearn import datasets, svm, model_selection\n\n# print the JS visualization code to the notebook\nbaby_shap.initjs()\n\n# train a SVM classifier\nd = datasets.load_iris()\nX = pd.DataFrame(data=d.data, columns=d.feature_names)\ny = d.target\n\nX_train, X_test, Y_train, Y_test = model_selection.train_test_split(X, y, test_size=0.2, random_state=0)\nclf = svm.SVC(kernel='rbf', probability=True)\nclf.fit(X_train.to_numpy(), Y_train)\n\n# use Kernel SHAP to explain test set predictions\nexplainer = baby_shap.KernelExplainer(svm.predict_proba, X_train, link=\"logit\")\nshap_values = explainer.shap_values(X_test, nsamples=100)\n\n# plot the SHAP values for the Setosa output of the first instance\nbaby_shap.force_plot(explainer.expected_value[0], shap_values[0][0,:], X_test.iloc[0,:], link=\"logit\")\n```\n<p align=\"center\">\n  <img width=\"810\" src=\"https://raw.githubusercontent.com/slundberg/shap/master/docs/artwork/iris_instance.png\" />\n</p>\n\nThe above explanation shows four features each contributing to push the model output from the base value (the average model output over the training dataset we passed) towards zero. If there were any features pushing the class label higher they would be shown in red.\n\nIf we take many explanations such as the one shown above, rotate them 90 degrees, and then stack them horizontally, we can see explanations for an entire dataset. This is exactly what we do below for all the examples in the iris test set:\n\n```python\n# plot the SHAP values for the Setosa output of all instances\nbaby_shap.force_plot(explainer.expected_value[0], shap_values[0], X_test, link=\"logit\")\n```\n<p align=\"center\">\n  <img width=\"813\" src=\"https://raw.githubusercontent.com/slundberg/shap/master/docs/artwork/iris_dataset.png\" />\n</p>\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A stripped and opiniated version of Scott Lundberg's SHAP (SHapley Additive exPlanations)",
    "version": "0.0.6",
    "split_keywords": [
        "shap"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "80b7ab881676900cf15d4f543a7f63a460e597b3efd9c984a19dfc9bcadbf4b7",
                "md5": "6077ba4d80e872418500b770701410c2",
                "sha256": "a4e4c58d5ccae5117dbbb057ab01741f14604916988fd4b0c4e8fbbf9f46aa96"
            },
            "downloads": -1,
            "filename": "baby_shap-0.0.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6077ba4d80e872418500b770701410c2",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 190641,
            "upload_time": "2023-01-30T08:00:50",
            "upload_time_iso_8601": "2023-01-30T08:00:50.067709Z",
            "url": "https://files.pythonhosted.org/packages/80/b7/ab881676900cf15d4f543a7f63a460e597b3efd9c984a19dfc9bcadbf4b7/baby_shap-0.0.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f76b8499710ecf5543ed7e6a0c662d22fca05aba579bf1da5838d8bedad4289e",
                "md5": "b0851c555e9cb8c070c0f63353bc96aa",
                "sha256": "76f7b24d5807ed88e34dd5d63dd6445d25b5b1a44541b07c3c80381a6acdf8a8"
            },
            "downloads": -1,
            "filename": "baby_shap-0.0.6.tar.gz",
            "has_sig": false,
            "md5_digest": "b0851c555e9cb8c070c0f63353bc96aa",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 182897,
            "upload_time": "2023-01-30T08:00:51",
            "upload_time_iso_8601": "2023-01-30T08:00:51.452485Z",
            "url": "https://files.pythonhosted.org/packages/f7/6b/8499710ecf5543ed7e6a0c662d22fca05aba579bf1da5838d8bedad4289e/baby_shap-0.0.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-01-30 08:00:51",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "baby-shap"
}
        
Elapsed time: 0.03373s