shap | game theoretic approach to explain the output | Machine Learning library

 by   slundberg Jupyter Notebook Version: 0.42.1 License: MIT

kandi X-RAY | shap Summary

kandi X-RAY | shap Summary

shap is a Jupyter Notebook library typically used in Artificial Intelligence, Machine Learning, Deep Learning applications. shap has no bugs, it has no vulnerabilities, it has a Permissive License and it has medium support. You can download it from GitHub.

SHAP (SHapley Additive exPlanations) is a game theoretic approach to explain the output of any machine learning model. It connects optimal credit allocation with local explanations using the classic Shapley values from game theory and their related extensions (see papers for details and citations).
Support
    Quality
      Security
        License
          Reuse

            kandi-support Support

              shap has a medium active ecosystem.
              It has 19415 star(s) with 2912 fork(s). There are 257 watchers for this library.
              There were 2 major release(s) in the last 12 months.
              There are 1517 open issues and 703 have been closed. On average issues are closed in 623 days. There are 76 open pull requests and 0 closed requests.
              It has a neutral sentiment in the developer community.
              The latest version of shap is 0.42.1

            kandi-Quality Quality

              shap has no bugs reported.

            kandi-Security Security

              shap has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.

            kandi-License License

              shap is licensed under the MIT License. This license is Permissive.
              Permissive licenses have the least restrictions, and you can use them in most projects.

            kandi-Reuse Reuse

              shap releases are available to install and integrate.
              Installation instructions, examples and code snippets are available.

            Top functions reviewed by kandi - BETA

            kandi's functional review helps you automatically verify the functionalities of the libraries and avoid rework.
            Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of shap
            Get all kandi verified functions for this library.

            shap Key Features

            No Key Features are available at this moment for shap.

            shap Examples and Code Snippets

            Deep learning example with GradientExplainer (TensorFlow/Keras/PyTorch models)
            Jupyter Notebookdot img1Lines of Code : 34dot img1License : Permissive (MIT)
            copy iconCopy
            from keras.applications.vgg16 import VGG16
            from keras.applications.vgg16 import preprocess_input
            import keras.backend as K
            import numpy as np
            import json
            import shap
            
            # load pre-trained model and choose two images to explain
            model = VGG16(weights='im  
            Tree ensemble example (XGBoost/LightGBM/CatBoost/scikit-learn/pyspark models)
            Jupyter Notebookdot img2Lines of Code : 23dot img2License : Permissive (MIT)
            copy iconCopy
            import xgboost
            import shap
            
            # train an XGBoost model
            X, y = shap.datasets.boston()
            model = xgboost.XGBRegressor().fit(X, y)
            
            # explain the model's predictions using SHAP
            # (same syntax works for LightGBM, CatBoost, scikit-learn, transformers, Spark,   
            Model agnostic example with KernelExplainer (explains any function)
            Jupyter Notebookdot img3Lines of Code : 20dot img3License : Permissive (MIT)
            copy iconCopy
            import sklearn
            import shap
            from sklearn.model_selection import train_test_split
            
            # print the JS visualization code to the notebook
            shap.initjs()
            
            # train a SVM classifier
            X_train,X_test,Y_train,Y_test = train_test_split(*shap.datasets.iris(), test_si  
            shap - task
            JavaScriptdot img4Lines of Code : 147dot img4License : Permissive (MIT License)
            copy iconCopy
            /*
             * Requires:
             *     psiturk.js
             *     utils.js
             */
            
            // Initalize psiturk object
            var psiTurk = new PsiTurk(uniqueId, adServerLoc, mode);
            
            var mycondition = condition;  // these two variables are passed by the psiturk server process
            var mycounterbal  
            shap - bundle
            JavaScriptdot img5Lines of Code : 19dot img5License : Permissive (MIT License)
            copy iconCopy
            !function(e){function t(o){if(n[o])return n[o].exports;var r=n[o]={i:o,l:!1,exports:{}};return e[o].call(r.exports,r,r.exports,t),r.l=!0,r.exports}var n={};return t.m=e,t.c=n,t.i=function(e){return e},t.d=function(e,n,o){t.o(e,n)||Object.defineProper  
            shap - utils
            JavaScriptdot img6Lines of Code : 16dot img6License : Permissive (MIT License)
            copy iconCopy
            
            function AssertException(message) { this.message = message; }
            AssertException.prototype.toString = function () {
            	return 'AssertException: ' + this.message;
            };
            
            function assert(exp, message) {
            	if (!exp) {
            		throw new AssertException(message);
            	}
            }
              
            Using SHAP with custom sklearn estimator
            Pythondot img7Lines of Code : 15dot img7License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            # Select model
            shap_model = model_linear_regression(pipe=LINEAR_PIPE, inverse=True)
            # fit model
            model_fitted = shap_model.fit(X_train, y_train)
            
            # Summarize model (not necessary but makes things faster
            # If not being summarized replace all
            export SHAP waterfall plot to dataframe
            Pythondot img8Lines of Code : 18dot img8License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            from sklearn.ensemble import RandomForestClassifier
            from sklearn.datasets import load_breast_cancer
            from shap import TreeExplainer
            
            X, y = load_breast_cancer(return_X_y=True)
            model = RandomForestClassifier(max_depth=5, n_estimators=100).fi
            How to interpret user supplied function using Shap (Shapley Additive explanation)?
            Pythondot img9Lines of Code : 13dot img9License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            import numpy as np
            from shap import KernelExplainer
            
            
            def f(x):
                y = x[:, 0] ** 2.5 + 3 * x[:, 1] + 10
                return np.array(y)
            
            
            x = np.arange(20).reshape((10,2))
            explainer = KernelExplainer(f, x)
            shap_values = explainer.shap_values(x)
            <
            Plot SHAP values in waterfall and beeswarm plots
            Pythondot img10Lines of Code : 6dot img10License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            10 print("Prediction From Adding SHAP Values to Base Value : ", explainer.expected_value + shap_vals.sum())
            
            AttributeError: 'list' object has no attribute 'sum'
            
            print("Prediction From Adding SHAP Values to Base Va

            Community Discussions

            QUESTION

            export SHAP waterfall plot to dataframe
            Asked 2022-Apr-05 at 09:38

            I am working on a binary classification using random forest model, neural networks in which am using SHAP to explain the model predictions. I followed the tutorial and wrote the below code to get the waterfall plot shown below

            ...

            ANSWER

            Answered 2022-Apr-05 at 09:38

            If I recall correctly, you can do something like this with pandas

            Source https://stackoverflow.com/questions/71688298

            QUESTION

            Shap - The color bar is not displayed in the summary plot
            Asked 2022-Apr-05 at 00:40

            When displaying summary_plot, the color bar does not show.

            ...

            ANSWER

            Answered 2021-Dec-26 at 21:17

            I had the same problem as you did, and I found that the solution was to downgrade matplotlib to 3.4.3.. It appears SHAP isn't optimized for matplotlib 3.5.1 yet.

            Source https://stackoverflow.com/questions/70461753

            QUESTION

            How to interpret user supplied function using Shap (Shapley Additive explanation)?
            Asked 2022-Apr-04 at 08:35

            I want to use the python Shap module to interpret user supplied nonlinear functions. I'll take just one simple example as a representative, but it cannot run successfully. I would like to ask if Shap can be used for this simple model and if yes how to implement it.

            Here is my code.

            ...

            ANSWER

            Answered 2022-Apr-04 at 08:35

            Did you mean this: 10 datapoints, 2 features, 1 outcome?

            Source https://stackoverflow.com/questions/71715658

            QUESTION

            How to export SHAP local explanations to dataframe?
            Asked 2022-Mar-25 at 03:23

            I am working on a binary classification using random forest and trying out SHAP to explain the model predictions.

            However, I would like to convert the SHAP local explanation plots with values into a pandas dataframe for each instance.

            Is there any one here who can help me with exporting SHAP local explanations to pandas dataframe for each instance?

            I know that SHAPASH has .to_pandas() method but couldn't find anything like that in SHAP

            I tried something like below based on the SO post here but it doesn't help

            ...

            ANSWER

            Answered 2022-Mar-25 at 03:23

            If you have a model like this:

            Source https://stackoverflow.com/questions/71599628

            QUESTION

            SHAP: How do I interpret expected values for force_plot?
            Asked 2022-Mar-23 at 04:07

            I'm trying to create a force_plot for my Random Forest model that has two classes (1 and 2), but I am a bit confused about the parameters for the force_plot.

            I have two different force_plot parameters I can provide the following:

            ...

            ANSWER

            Answered 2022-Mar-22 at 18:14

            Let's try reproducible:

            Source https://stackoverflow.com/questions/71559181

            QUESTION

            How to output Shap values in probability and make force_plot from binary classifier
            Asked 2022-Mar-22 at 03:32

            I need to plot how each feature impacts the predicted probability for each sample from my LightGBM binary classifier. So I need to output Shap values in probability, instead of normal Shap values. It does not appear to have any options to output in term of probability.

            The example code below is what I use to generate dataframe of Shap values and do a force_plot for the first data sample. Does anyone know how I should modify the code to change the output? I'm new to Shap value and the Shap package. Thanks a lot in advance.

            ...

            ANSWER

            Answered 2022-Mar-14 at 13:40

            You can consider running your output values through a softmax() function. For reference, it is defined as :

            Source https://stackoverflow.com/questions/71446065

            QUESTION

            How to filter redundant features using shap.utils.hclust not only by visual inspection barplot?
            Asked 2022-Mar-20 at 16:26

            I'm using shap.utils.hclust to figure out which features are redundant and following the documentation

            Reproducible example:

            ...

            ANSWER

            Answered 2022-Mar-20 at 16:16
            1. Underneath, even tree models for classification are regression tasks. SHAP calls it "raw" feature output space, Tensorflow would call it logits. To convert raw to proba space sigmoid or softmax are used. So, answering your first question:

            Source https://stackoverflow.com/questions/71534923

            QUESTION

            Cannot display SHAP text visualization in Streamlit
            Asked 2022-Mar-15 at 23:58

            I am trying to build a dashboard of my NLP project. So I am using a BERT model for the predictions, the SHAP package for the visualization and Streamlit for creating dashboard:

            ...

            ANSWER

            Answered 2021-Dec-10 at 05:15

            This can be visualized with Streamlit Components and latest SHAP v0.36+ (which define a new getjs method), to plot JS SHAP plots

            (some plots like summary_plot are actually Matplotlib and can be plotted with st.pyplot)

            Source https://stackoverflow.com/questions/70277425

            QUESTION

            TensorFlow Shape Error After Model is Already Trained
            Asked 2022-Feb-08 at 07:13

            I am training a TensorFlow RNN model using LSTM layers to determine if sound is coming more from the right or left in a stereo audio signal. The model training goes smoothly, then, once it is done training, I get an Invalid Argument Error as shown below. Does anyone know what could be causing this? I have tried fixing it using the solution to a similar question found here, but to no avail.

            I do not understand why it is expecting a tensor of shape [32,2]. Did I define that somewhere I am unaware of?

            Here is my code:

            ...

            ANSWER

            Answered 2022-Feb-08 at 07:13

            You get this error because you hard-coded the batch size in the first LSTM layer and the number of data samples is not evenly divisible by 100. You have to take care of the remainder somehow. I would recommend removing the batch size from the first layer and only entering the batch size in model.fit. This way your model will be able to handle the remaining smaller batch(es). Here is an example:

            Source https://stackoverflow.com/questions/71026580

            QUESTION

            Dataproc Cluster creation is failing with PIP error "Could not build wheels"
            Asked 2022-Jan-24 at 13:04

            We use to spin cluster with below configurations. It used to run fine till last week but now failing with error ERROR: Failed cleaning build dir for libcst Failed to build libcst ERROR: Could not build wheels for libcst which use PEP 517 and cannot be installed directly

            ...

            ANSWER

            Answered 2022-Jan-19 at 21:50

            Seems you need to upgrade pip, see this question.

            But there can be multiple pips in a Dataproc cluster, you need to choose the right one.

            1. For init actions, at cluster creation time, /opt/conda/default is a symbolic link to either /opt/conda/miniconda3 or /opt/conda/anaconda, depending on which Conda env you choose, the default is Miniconda3, but in your case it is Anaconda. So you can run either /opt/conda/default/bin/pip install --upgrade pip or /opt/conda/anaconda/bin/pip install --upgrade pip.

            2. For custom images, at image creation time, you want to use the explicit full path, /opt/conda/anaconda/bin/pip install --upgrade pip for Anaconda, or /opt/conda/miniconda3/bin/pip install --upgrade pip for Miniconda3.

            So, you can simply use /opt/conda/anaconda/bin/pip install --upgrade pip for both init actions and custom images.

            Source https://stackoverflow.com/questions/70743642

            Community Discussions, Code Snippets contain sources that include Stack Exchange Network

            Vulnerabilities

            No vulnerabilities reported

            Install shap

            SHAP can be installed from either PyPI or conda-forge:.

            Support

            These notebooks comprehensively demonstrate how to use specific functions and objects.
            Find more information at:

            Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items

            Find more libraries
            Install
          • PyPI

            pip install shap

          • CLONE
          • HTTPS

            https://github.com/slundberg/shap.git

          • CLI

            gh repo clone slundberg/shap

          • sshUrl

            git@github.com:slundberg/shap.git

          • Stay Updated

            Subscribe to our newsletter for trending solutions and developer bootcamps

            Agree to Sign up and Terms & Conditions

            Share this Page

            share link