From c88a8e31715f6f9a4d092268bd24532cdb5a124c Mon Sep 17 00:00:00 2001 From: vijay-arya Date: Fri, 18 Oct 2019 11:22:20 +0530 Subject: [PATCH] lime and shap --- examples/lime/LIME.ipynb | 269 +++++++++++++------------ examples/shap/SHAP.ipynb | 416 ++++++++++++++------------------------- 2 files changed, 289 insertions(+), 396 deletions(-) diff --git a/examples/lime/LIME.ipynb b/examples/lime/LIME.ipynb index 8495304..f1a97d2 100644 --- a/examples/lime/LIME.ipynb +++ b/examples/lime/LIME.ipynb @@ -4,23 +4,22 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Tutorial to invoke LIME explainers via AIX360\n", + "# Tutorial to invoke LIME explainers via aix360\n", "\n", - "There are two ways to use LIME for explanations after installing aix360:\n", - "- Since LIME is installed along with other libraries in aix360, it can simply be invoked directly.\n", - "- LIME can also be invoked in a manner similar to other algorithms in aix360 via the implemented wrapper classes.\n", + "There are two ways to use [LIME](https://github.com/marcotcr/lime) explainers after installing aix360:\n", + "- [Approach 1 (aix360 style)](#approach1): LIME explainers can be invoked in a manner similar to other explainer algorithms in aix360 via the implemented wrapper classes.\n", + "- [Approach 2 (original style)](#approach2): Since LIME comes pre-installed in aix360, the explainers can simply be invoked directly.\n", "\n", - "This notebook shows both these approaches to invoke LIME in aix360. The notebook is based on the following example from the original LIME tutorial: \n", - "https://github.com/marcotcr/lime/blob/master/doc/notebooks/Lime%20-%20multiclass.ipynb\n" + "This notebook showcases both these approaches to invoke LIME. The notebook is based on the following example from the original LIME tutorial: https://marcotcr.github.io/lime/tutorials/Lime%20-%20multiclass.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Approach 1 (original style)\n", - "### LIME Text Example \n", - "- Example from https://marcotcr.github.io/lime/tutorials/Lime%20-%20multiclass.html" + "## Approach 1 (aix360 style)\n", + "\n", + "- Note the import statement related to LimeTextExplainer" ] }, { @@ -36,7 +35,8 @@ "import sklearn.ensemble\n", "import sklearn.metrics\n", "\n", - "from lime.lime_text import LimeTextExplainer" + "# Importing LimeTextExplainer (aix360 sytle)\n", + "from aix360.algorithms.lime import LimeTextExplainer" ] }, { @@ -44,6 +44,24 @@ "execution_count": 2, "metadata": {}, "outputs": [], + "source": [ + "# Supress jupyter warnings if required for cleaner output\n", + "import warnings\n", + "warnings.simplefilter('ignore')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Fetching data, training a classifier" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], "source": [ "from sklearn.datasets import fetch_20newsgroups\n", "newsgroups_train = fetch_20newsgroups(subset='train')\n", @@ -56,7 +74,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -73,7 +91,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -84,7 +102,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -93,7 +111,7 @@ "MultinomialNB(alpha=0.01, class_prior=None, fit_prior=True)" ] }, - "execution_count": 5, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -106,7 +124,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -115,7 +133,7 @@ "0.8350184193998174" ] }, - "execution_count": 6, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -125,9 +143,16 @@ "sklearn.metrics.f1_score(newsgroups_test.target, pred, average='weighted')" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Explaining predictions using lime" + ] + }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -137,7 +162,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -153,15 +178,6 @@ "print(c.predict_proba([newsgroups_test.data[0]]).round(3))" ] }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [], - "source": [ - "explainer = LimeTextExplainer(class_names=class_names)" - ] - }, { "cell_type": "code", "execution_count": 10, @@ -171,12 +187,13 @@ "name": "stdout", "output_type": "stream", "text": [ - "\n" + "\n" ] } ], "source": [ - "print(type(explainer))" + "limeexplainer = LimeTextExplainer(class_names=class_names)\n", + "print(type(limeexplainer))" ] }, { @@ -184,14 +201,6 @@ "execution_count": 11, "metadata": {}, "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/anaconda3/envs/aix360/lib/python3.6/site-packages/lime/lime_text.py:116: FutureWarning: split() requires a non-empty pattern match.\n", - " self.as_list = [s for s in splitter.split(self.raw) if s]\n" - ] - }, { "name": "stdout", "output_type": "stream", @@ -204,7 +213,8 @@ ], "source": [ "idx = 1340\n", - "exp = explainer.explain_instance(newsgroups_test.data[idx], c.predict_proba, num_features=6, labels=[0, 17])\n", + "# aix360 style for explaining input instances\n", + "exp = limeexplainer.explain_instance(newsgroups_test.data[idx], c.predict_proba, num_features=6, labels=[0, 17])\n", "print('Document id: %d' % idx)\n", "print('Predicted class =', class_names[nb.predict(test_vectors[idx]).reshape(1,-1)[0,0]])\n", "print('True class: %s' % class_names[newsgroups_test.target[idx]])" @@ -220,20 +230,20 @@ "output_type": "stream", "text": [ "Explanation for class atheism\n", - "('Caused', 0.2638134141747723)\n", - "('Rice', 0.138346697297952)\n", - "('Genocide', 0.12506034401934968)\n", - "('owlnet', -0.08968526910905454)\n", - "('scri', -0.08426182410065453)\n", - "('Semitic', -0.07968947032986141)\n", + "('Caused', 0.2598306611703779)\n", + "('Rice', 0.1476407287363688)\n", + "('Genocide', 0.13182300286384235)\n", + "('scri', -0.09419412002335747)\n", + "('certainty', -0.09272741554383297)\n", + "('owlnet', -0.08993298975187172)\n", "\n", "Explanation for class mideast\n", - "('fsu', -0.06012358222232414)\n", - "('Theism', -0.05266356722531139)\n", - "('Luther', -0.049997961626805004)\n", - "('jews', 0.035879721280743154)\n", - "('Caused', -0.03457976903151407)\n", - "('PBS', 0.034166969785271686)\n" + "('fsu', -0.05535199329931831)\n", + "('Theism', -0.05150493402341905)\n", + "('Luther', -0.04742295494991691)\n", + "('jews', 0.03810985477960863)\n", + "('Caused', -0.037706845450166455)\n", + "('PBS', 0.031228586744514376)\n" ] } ], @@ -250,14 +260,6 @@ "execution_count": 13, "metadata": {}, "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/anaconda3/envs/aix360/lib/python3.6/site-packages/lime/lime_text.py:116: FutureWarning: split() requires a non-empty pattern match.\n", - " self.as_list = [s for s in splitter.split(self.raw) if s]\n" - ] - }, { "name": "stdout", "output_type": "stream", @@ -267,7 +269,8 @@ } ], "source": [ - "exp = explainer.explain_instance(newsgroups_test.data[idx], c.predict_proba, num_features=6, top_labels=2)\n", + "# aix360 style for explaining input instances\n", + "exp = limeexplainer.explain_instance(newsgroups_test.data[idx], c.predict_proba, num_features=6, top_labels=2)\n", "print(exp.available_labels())" ] }, @@ -37369,10 +37372,10 @@ "/***/ })\n", "/******/ ]);\n", "//# sourceMappingURL=bundle.js.map \n", - "
\n", + "
\n", " \n", " \n", @@ -74503,10 +74506,10 @@ "/***/ })\n", "/******/ ]);\n", "//# sourceMappingURL=bundle.js.map \n", - "
\n", + "
\n", " \n", " \n", " " @@ -74542,9 +74545,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Approach 2 (aix360 style)\n", - "### LIME Text Example \n", - "- Example from https://marcotcr.github.io/lime/tutorials/Lime%20-%20multiclass.html" + "## Approach 2 (original style)\n", + "\n", + "- Note the import statement related to LimeTextExplainer" ] }, { @@ -74560,7 +74563,8 @@ "import sklearn.ensemble\n", "import sklearn.metrics\n", "\n", - "from aix360.algorithms.lime import LimeTextExplainer" + "# Importing LimeTextExplainer (original style)\n", + "from lime.lime_text import LimeTextExplainer" ] }, { @@ -74568,6 +74572,24 @@ "execution_count": 17, "metadata": {}, "outputs": [], + "source": [ + "# Supress jupyter warnings if required for cleaner output\n", + "import warnings\n", + "warnings.simplefilter('ignore')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Fetching data, training a classifier" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], "source": [ "from sklearn.datasets import fetch_20newsgroups\n", "newsgroups_train = fetch_20newsgroups(subset='train')\n", @@ -74580,7 +74602,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 19, "metadata": {}, "outputs": [ { @@ -74597,7 +74619,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 20, "metadata": {}, "outputs": [], "source": [ @@ -74608,7 +74630,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 21, "metadata": {}, "outputs": [ { @@ -74617,7 +74639,7 @@ "MultinomialNB(alpha=0.01, class_prior=None, fit_prior=True)" ] }, - "execution_count": 20, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } @@ -74630,8 +74652,10 @@ }, { "cell_type": "code", - "execution_count": 21, - "metadata": {}, + "execution_count": 22, + "metadata": { + "scrolled": true + }, "outputs": [ { "data": { @@ -74639,7 +74663,7 @@ "0.8350184193998174" ] }, - "execution_count": 21, + "execution_count": 22, "metadata": {}, "output_type": "execute_result" } @@ -74649,9 +74673,16 @@ "sklearn.metrics.f1_score(newsgroups_test.target, pred, average='weighted')" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Explaining predictions using lime" + ] + }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 23, "metadata": {}, "outputs": [], "source": [ @@ -74661,8 +74692,10 @@ }, { "cell_type": "code", - "execution_count": 23, - "metadata": {}, + "execution_count": 24, + "metadata": { + "scrolled": true + }, "outputs": [ { "name": "stdout", @@ -74677,15 +74710,6 @@ "print(c.predict_proba([newsgroups_test.data[0]]).round(3))" ] }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": {}, - "outputs": [], - "source": [ - "explainer = LimeTextExplainer(class_names=class_names)" - ] - }, { "cell_type": "code", "execution_count": 25, @@ -74695,11 +74719,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "\n" + "\n" ] } ], "source": [ + "explainer = LimeTextExplainer(class_names=class_names)\n", "print(type(explainer))" ] }, @@ -74708,14 +74733,6 @@ "execution_count": 26, "metadata": {}, "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/anaconda3/envs/aix360/lib/python3.6/site-packages/lime/lime_text.py:116: FutureWarning: split() requires a non-empty pattern match.\n", - " self.as_list = [s for s in splitter.split(self.raw) if s]\n" - ] - }, { "name": "stdout", "output_type": "stream", @@ -74728,6 +74745,7 @@ ], "source": [ "idx = 1340\n", + "# LIME original style for explaining input instances\n", "exp = explainer.explain_instance(newsgroups_test.data[idx], c.predict_proba, num_features=6, labels=[0, 17])\n", "print('Document id: %d' % idx)\n", "print('Predicted class =', class_names[nb.predict(test_vectors[idx]).reshape(1,-1)[0,0]])\n", @@ -74744,20 +74762,20 @@ "output_type": "stream", "text": [ "Explanation for class atheism\n", - "('Caused', 0.2597060457934411)\n", - "('Rice', 0.13750116781221147)\n", - "('Genocide', 0.12709403856842216)\n", - "('owlnet', -0.09213085168650197)\n", - "('certainty', -0.08533532387206953)\n", - "('scri', -0.0815491273299416)\n", + "('Caused', 0.2601154192014532)\n", + "('Rice', 0.14380270774013457)\n", + "('Genocide', 0.12202776283712338)\n", + "('owlnet', -0.0894833391913411)\n", + "('scri', -0.08839334155116563)\n", + "('certainty', -0.08588797496264497)\n", "\n", "Explanation for class mideast\n", - "('fsu', -0.05853835334882245)\n", - "('Theism', -0.05311524141087401)\n", - "('Luther', -0.05238291745855021)\n", - "('jews', 0.03575948416506709)\n", - "('Rice', -0.0345554947784977)\n", - "('PBS', 0.02977066446182471)\n" + "('fsu', -0.053283740516256506)\n", + "('Theism', -0.05103765776729127)\n", + "('Luther', -0.04556208403350433)\n", + "('jews', 0.03472601688974457)\n", + "('Caused', -0.03463184676401465)\n", + "('PBS', 0.02914905322207845)\n" ] } ], @@ -74774,14 +74792,6 @@ "execution_count": 28, "metadata": {}, "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/anaconda3/envs/aix360/lib/python3.6/site-packages/lime/lime_text.py:116: FutureWarning: split() requires a non-empty pattern match.\n", - " self.as_list = [s for s in splitter.split(self.raw) if s]\n" - ] - }, { "name": "stdout", "output_type": "stream", @@ -74791,6 +74801,7 @@ } ], "source": [ + "# LIME original style for explaining input instances\n", "exp = explainer.explain_instance(newsgroups_test.data[idx], c.predict_proba, num_features=6, top_labels=2)\n", "print(exp.available_labels())" ] @@ -111893,10 +111904,10 @@ "/***/ })\n", "/******/ ]);\n", "//# sourceMappingURL=bundle.js.map \n", - "
\n", + "
\n", " \n", " \n", @@ -111932,7 +111943,9 @@ { "cell_type": "code", "execution_count": 30, - "metadata": {}, + "metadata": { + "scrolled": false + }, "outputs": [ { "data": { @@ -149027,10 +149040,10 @@ "/***/ })\n", "/******/ ]);\n", "//# sourceMappingURL=bundle.js.map \n", - "
\n", + "
\n", " \n", " \n", " " diff --git a/examples/shap/SHAP.ipynb b/examples/shap/SHAP.ipynb index 2fe2079..06155a3 100644 --- a/examples/shap/SHAP.ipynb +++ b/examples/shap/SHAP.ipynb @@ -4,13 +4,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Tutorial to invoke SHAP explainers via AIX360\n", + "# Tutorial to invoke SHAP explainers via aix360\n", "\n", - "There are two ways to use SHAP for explanations after installing aix360:\n", - "- Since SHAP is installed along with other libraries in aix360, it can simply be invoked directly.\n", - "- SHAP explainers can also be invoked in a manner similar to other explainer algorithms in aix360 via the implemented wrapper classes.\n", + "There are two ways to use [SHAP](https://github.com/slundberg/shap) explainers after installing aix360:\n", + "- [Approach 1 (aix360 style)](#approach1): SHAP explainers can be invoked in a manner similar to other explainer algorithms in aix360 via the implemented wrapper classes.\n", + "- [Approach 2 (original style)](#approach2): Since SHAP comes pre-installed in aix360, the explainers can simply be invoked directly.\n", "\n", - "This notebook showcases both these approaches to invoke SHAP via aix360. The notebook is based on the following example from the original SHAP tutorial: \n", + "This notebook showcases both these approaches to invoke SHAP. The notebook is based on the following example from the original SHAP tutorial: \n", "https://slundberg.github.io/shap/notebooks/Iris%20classification%20with%20scikit-learn.html\n" ] }, @@ -18,7 +18,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Approach 1 (original style)" + "## Approach 1 (aix360 style)\n", + "\n", + "- Note the import statement related to KernelExplainer" ] }, { @@ -33,17 +35,38 @@ "import sklearn.datasets\n", "import sklearn.ensemble\n", "import numpy as np\n", - "import shap\n", "import time\n", "np.random.seed(1)\n", "\n", - "from shap import KernelExplainer" + "# Importing shap KernelExplainer (aix360 style)\n", + "from aix360.algorithms.shap import KernelExplainer\n", + "\n", + "# the following import is required for access to shap plotting functions and datasets\n", + "import shap" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, + "outputs": [], + "source": [ + "# Supress jupyter warnings if required for cleaner output\n", + "import warnings\n", + "warnings.simplefilter('ignore')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### K-nearest neighbors" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, "outputs": [ { "data": { @@ -75,6 +98,13 @@ }, "metadata": {}, "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Accuracy = 96.66666666666667%\n" + ] } ], "source": [ @@ -89,62 +119,66 @@ " print(\"Accuracy = {0}%\".format(100*np.sum(f(X_test) == Y_test)/len(Y_test)))\n", " time.sleep(0.5) # to let the print get out before any progress bars\n", "\n", - "shap.initjs()" + "shap.initjs()\n", + "\n", + "knn = sklearn.neighbors.KNeighborsClassifier()\n", + "knn.fit(X_train, Y_train)\n", + "\n", + "print_accuracy(knn.predict)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### K-nearest neighbors" + "### Explain a single prediction from the test set" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": {}, "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Using 120 background data samples could cause slower run times. Consider using shap.kmeans(data, K) to summarize the background as K weighted samples.\n" + ] + }, { "name": "stdout", "output_type": "stream", "text": [ - "Accuracy = 96.66666666666667%\n" + "\n" ] } ], "source": [ - "knn = sklearn.neighbors.KNeighborsClassifier()\n", - "knn.fit(X_train, Y_train)\n", - "\n", - "print_accuracy(knn.predict)" + "shapexplainer = KernelExplainer(knn.predict_proba, X_train)\n", + "print(type(shapexplainer))" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 5, "metadata": {}, + "outputs": [], "source": [ - "### Explain a single prediction from the test set" + "# aix360 style for explaining input instances\n", + "shap_values = shapexplainer.explain_instance(X_test.iloc[0,:])" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 6, "metadata": {}, "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Using 120 background data samples could cause slower run times. Consider using shap.kmeans(data, K) to summarize the background as K weighted samples.\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n" - ] - }, { "data": { "text/html": [ "\n", - "
\n", + "
\n", "
\n", " Visualization omitted, Javascript library not loaded!
\n", " Have you run `initjs()` in this notebook? If this notebook was from another\n", @@ -155,7 +189,7 @@ " " ], @@ -163,32 +197,13 @@ "" ] }, - "execution_count": 4, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "explainer = KernelExplainer(knn.predict_proba, X_train)\n", - "shap_values = explainer.shap_values(X_test.iloc[0,:])\n", - "shap.force_plot(explainer.expected_value[0], shap_values[0], X_test.iloc[0,:])" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], - "source": [ - "print(type(explainer))" + "shap.force_plot(shapexplainer.explainer.expected_value[0], shap_values[0], X_test.iloc[0,:])" ] }, { @@ -200,13 +215,13 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "d6c4f926b165474997fe5493223434e1", + "model_id": "53079d7d468a4a568d975ae624d1d419", "version_major": 2, "version_minor": 0 }, @@ -217,76 +232,6 @@ "metadata": {}, "output_type": "display_data" }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n" - ] - }, { "name": "stdout", "output_type": "stream", @@ -294,19 +239,11 @@ "\n" ] }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n" - ] - }, { "data": { "text/html": [ "\n", - "
\n", + "
\n", "
\n", " Visualization omitted, Javascript library not loaded!
\n", " Have you run `initjs()` in this notebook? If this notebook was from another\n", @@ -317,7 +254,7 @@ " " ], @@ -325,26 +262,29 @@ "" ] }, - "execution_count": 6, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "shap_values = explainer.shap_values(X_test)\n", - "shap.force_plot(explainer.expected_value[0], shap_values[0], X_test)" + "# aix360 style for explaining input instances\n", + "shap_values = shapexplainer.explain_instance(X_test)\n", + "shap.force_plot(shapexplainer.explainer.expected_value[0], shap_values[0], X_test)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Approach 2 (AIX360 style)" + "## Approach 2 (original style)\n", + "\n", + "- Note the last import statement related to KernelExplainer" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -354,16 +294,35 @@ "import sklearn.datasets\n", "import sklearn.ensemble\n", "import numpy as np\n", - "import shap\n", "import time\n", "np.random.seed(1)\n", "\n", - "from aix360.algorithms.shap import KernelExplainer" + "# Importing shap KernelExplainer (original style)\n", + "import shap\n", + "from shap import KernelExplainer" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "# Supress jupyter warnings if required for cleaner output\n", + "import warnings\n", + "warnings.simplefilter('ignore')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### K-nearest neighbors" + ] + }, + { + "cell_type": "code", + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -396,6 +355,13 @@ }, "metadata": {}, "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Accuracy = 96.66666666666667%\n" + ] } ], "source": [ @@ -410,48 +376,56 @@ " print(\"Accuracy = {0}%\".format(100*np.sum(f(X_test) == Y_test)/len(Y_test)))\n", " time.sleep(0.5) # to let the print get out before any progress bars\n", "\n", - "shap.initjs()" + "shap.initjs()\n", + "\n", + "knn = sklearn.neighbors.KNeighborsClassifier()\n", + "knn.fit(X_train, Y_train)\n", + "\n", + "print_accuracy(knn.predict)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Explain a single prediction from the test set" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 11, "metadata": {}, "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Using 120 background data samples could cause slower run times. Consider using shap.kmeans(data, K) to summarize the background as K weighted samples.\n" + ] + }, { "name": "stdout", "output_type": "stream", "text": [ - "Accuracy = 96.66666666666667%\n" + "\n" ] } ], "source": [ - "knn = sklearn.neighbors.KNeighborsClassifier()\n", - "knn.fit(X_train, Y_train)\n", - "\n", - "print_accuracy(knn.predict)" + "explainer = KernelExplainer(knn.predict_proba, X_train)\n", + "print(type(explainer))" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 12, "metadata": {}, "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Using 120 background data samples could cause slower run times. Consider using shap.kmeans(data, K) to summarize the background as K weighted samples.\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n" - ] - }, { "data": { "text/html": [ "\n", - "
\n", + "
\n", "
\n", " Visualization omitted, Javascript library not loaded!
\n", " Have you run `initjs()` in this notebook? If this notebook was from another\n", @@ -462,7 +436,7 @@ " " ], @@ -470,43 +444,33 @@ "" ] }, - "execution_count": 10, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "shapexplainer = KernelExplainer(knn.predict_proba, X_train)\n", - "shap_values = shapexplainer.explain_instance(X_test.iloc[0,:])\n", - "shap.force_plot(shapexplainer.explainer.expected_value[0], shap_values[0], X_test.iloc[0,:])" + "# Shap original style for explaining input instances\n", + "shap_values = explainer.shap_values(X_test.iloc[0,:])\n", + "shap.force_plot(explainer.expected_value[0], shap_values[0], X_test.iloc[0,:])" ] }, { - "cell_type": "code", - "execution_count": 11, + "cell_type": "markdown", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], "source": [ - "print(type(shapexplainer))" + "### Explain all the predictions in the test set" ] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "5bddb6f994cf4555a65eb30649cd650c", + "model_id": "236c4a11af794e15941943bac6ec73bf", "version_major": 2, "version_minor": 0 }, @@ -517,74 +481,6 @@ "metadata": {}, "output_type": "display_data" }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n" - ] - }, { "name": "stdout", "output_type": "stream", @@ -592,21 +488,11 @@ "\n" ] }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n", - "/anaconda3/envs/aix360/lib/python3.6/site-packages/shap/explainers/kernel.py:555: UserWarning: l1_reg=\"auto\" is deprecated and in the next version (v0.29) the behavior will change from a conditional use of AIC to simply \"num_features(10)\"!\n", - " \"l1_reg=\\\"auto\\\" is deprecated and in the next version (v0.29) the behavior will change from a \" \\\n" - ] - }, { "data": { "text/html": [ "\n", - "
\n", + "
\n", "
\n", " Visualization omitted, Javascript library not loaded!
\n", " Have you run `initjs()` in this notebook? If this notebook was from another\n", @@ -617,7 +503,7 @@ " " ], @@ -625,22 +511,16 @@ "" ] }, - "execution_count": 12, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "shap_values = shapexplainer.explain_instance(X_test)\n", - "shap.force_plot(shapexplainer.explainer.expected_value[0], shap_values[0], X_test)" + "# Shap original style for explaining input instances\n", + "shap_values = explainer.shap_values(X_test)\n", + "shap.force_plot(explainer.expected_value[0], shap_values[0], X_test)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": {