Building and Deploying Explainable AI Dashboards using Dash and SHAP

Xing Han Lu
Plotly
Published in
7 min readAug 3, 2020

In recent years, we have seen an explosion in the usage of Machine Learning (ML) algorithms for automating and supporting human decisions. From helping doctors in diagnosing potential diseases that could render diabetic patients blind, to supporting financial analysts in equity management, the impact of ML is irrefutable. However, as algorithms become more sophisticated, it becomes harder to understand how it makes decisions. As a result, various harmful biases could arise in critical situations, such as when ML is used to determine credit limit for credit cards or applied to facial recognition by law enforcement agencies. In order to identify and mitigate the effect of such biases before the model is deployed, it is important to leverage explainable AI (xAI) to better understand which features and factors have the most impact on the final output of the model. This information can then be utilized to help AI developers understand why a certain model might not be performing well in certain scenarios, or just in general. At the same time, such techniques could help us uncover more insights about a problem than simply using black-box models. Ultimately, such techniques can help both technical and non-technical stakeholders better understand and appreciate AI models.

Explaining black-box models with Shapley Values

Image retrieved from the original author’s repository

Many researchers and software developers have been working on the subject of explainable AI systems for many years. One approach, which is called SHapley Additive exPlanations (SHAP), has been increasingly popular in the past few years. It has been used to identify chronic kidney disease, to model US mortality factors and to help anaesthesiologists during surgeries. By applying a game theory concept called Shapley values, you can interpret computer vision models, linear models, tree-based models, and more.

Building a production-ready xAI app with Dash

The dashboard was fully built in Python and runs SHAP and LightGBM in real-time. Try it out!

Let’s take, as an example, the task of predicting tips received by waiters based on features such as the total bill, the gender of the payer, the day and time, etc. A black-box model that ingests all those features just to predict the tips could be highly biased and might not be useful for business analysts trying to understand the behavior of the customers. However, by applying SHAP, we can gain more insights on the impact of each feature on the final value predicted by the model, which can be useful for understanding how the model perceives factors prone to biases, such as sex, age, race, etc.

In order to make such xAI methods more accessible and interactive, we used Dash to build an end-to-end application that runs an ensemble model called LightGBM on custom inputs. The dashboard and the full model are deployed on our Dash Enterprise Kubernetes servers, and both the black-box model and SHAP are running in real-time.

In the app, you will find controls that let you control the total bill, sex, day of the week, etc. Each one of those controls defines an input feature, and every time you update them, a new sample is given to the trained LightGBM model.

However, since LightGBM is a type of gradient boosting method, it is hard to directly interpret it. Therefore, we simulated the controls to allow the app to compute the SHAP values and display them in a waterfall chart.

In addition to specifying custom model inputs, you can also select a random example from the training set. Whenever you do this, you will see the real label appear on the right side (as a scatter point). You can then tweak the feature values to see how the various SHAP values change.

Moreover, you can also decide to make binary predictions (e.g. the sex of the customer) and interact with the graph using the Plotly modebar.

Bridging the gap between Python and advanced analytics

The current state-of-the-art ML algorithms (e.g. gradient boosting and neural networks) for modeling continuous and categorical features are usually written in optimized C/C++ codes, but they can be conveniently used through Python. As a result, powerful xAI libraries like SHAP are also interfaced in the same language, which lets us train and explain powerful models in just a few lines of Python code. However, although such models are popular in the ML community, considerable effort needs to be made to port them into traditional BI tools, including having to connect external servers and add third-party extensions. Furthermore, building UIs that lets you train these Python-based ML libraries can quickly become cumbersome if you are using those BI tools.

With Dash, you can seamlessly integrate popular and up-to-date ML libraries, which enable Dash app users to quickly answer “what if?” questions, and probe what the ML models have learned from the data. Most of the time, all you need to do is to install and freeze such libraries using pip, which is usually done in a few lines:

pip install dash shap lightgbm
pip freeze > requirements.txt

When you are ready to share your dashboard, all the dependencies and deployment are handled by the Dash Enterprise App Manager.

Gaining insights beyond black-box predictions

A classical argument against the use of more advanced models is that, although they can improve the accuracy, the complexity makes them harder to interpret. With linear regression, you can use the coefficients to judge which features weigh more than others for making a prediction; in the case of decision trees, you can visualize how the tree splits and set thresholds for deciding the output. In the case of deep neural networks and ensemble models, you can’t visualize the tree structure nor the coefficients; however, with SHAP, it’s possible to not only explain how features generally affect the model, but also how each feature value influences the output in a specific example. For example, the model might believe that female customers tend to tip more when they are going out with a friend for dinner (on a Saturday) than when they are grabbing lunch alone (on a Thursday).

Left: Thursday lunch alone. Right: Saturday Dinner with friends.

Such insight could either lead to a stronger understanding of customer behavior if it is backed by additional studies, or it could reveal some degree of systematic bias that would not have been otherwise uncovered without SHAP. With Dash, we make it easier to build and deploy custom dashboards that let you interpret all sorts of ML models, whether they are trained on predicting tips, or other types of data.

Putting the power of Python in the hands of business users

At Plotly, we are working on keeping Dash flexible, yet easy-to-use for building ML apps and dashboards. For this reason, we built our app with components from the Dash Enterprise Design Kit, which makes it easy to tailor apps to meet style guidelines without delving into HTML and CSS. For example, if you don’t like the color of the bars in the default waterfall charts, you can easily change it with the Design Kit Theme Editor.

Furthermore, new features in Dash like pattern-matching callbacks let you simplify the process of creating callbacks. As a result, you can create very complex callbacks between components with very little effort. As an example, there are six controls in our app (one for each input feature), but we only need to specify one Input and one State to our callback to control all the components at the same time:

Then, in one line, we were able to construct a dictionary where the keys are the feature names and the values are what we will input to the model. From then on, it’s easy to process the dictionary in the input format most suitable to your model.

Are you interested in creating similar apps using state-of-the-art models and xAI algorithms? Contact us to learn more about how Dash Enterprise can help you build, design, and deploy ML dashboards — with no compromise.

--

--