{"id":8185,"date":"2021-04-05T00:18:03","date_gmt":"2021-04-05T00:18:03","guid":{"rendered":"https:\/\/wealthrevelation.com\/data-science\/2021\/04\/05\/shapash-making-machine-learning-models-understandable\/"},"modified":"2021-04-05T00:18:03","modified_gmt":"2021-04-05T00:18:03","slug":"shapash-making-machine-learning-models-understandable","status":"publish","type":"post","link":"https:\/\/wealthrevelation.com\/data-science\/2021\/04\/05\/shapash-making-machine-learning-models-understandable\/","title":{"rendered":"Shapash: Making Machine Learning Models Understandable"},"content":{"rendered":"<div id=\"post-\">\n   <!-- post_author Yann Golhen -->  <\/p>\n<p><b>By <a href=\"https:\/\/twitter.com\/GolhenY\" target=\"_blank\" rel=\"noopener\">Yann Golhen<\/a>, MAIF, Lead Data Scientist<\/b>.<\/p>\n<p><img class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/max\/870\/1*oaJTad5H3PZk5oUNrcoXag.gif\" width=\"90%\"><\/p>\n<p><em><a href=\"https:\/\/shapash-demo.ossbymaif.fr\/\" target=\"_blank\" rel=\"noopener\">Shapash Web App Demo<\/a><\/em><\/p>\n<p><a href=\"https:\/\/github.com\/MAIF\/shapash\" target=\"_blank\" rel=\"noopener\">Shapash<\/a>\u00a0by\u00a0<a href=\"https:\/\/www.maif.fr\/\" target=\"_blank\" rel=\"noopener\">MAIF<\/a>\u00a0is a Python Toolkit that facilitates the understanding of Machine Learning models to data scientists. It makes it easier to share and discuss the model interpretability with non-data specialists: business analysts, managers, and end-users.<\/p>\n<p>Concretely, Shapash provides easy-to-read visualizations and a\u00a0<a href=\"https:\/\/shapash-demo.ossbymaif.fr\/\" target=\"_blank\" rel=\"noopener\">web app<\/a>. Shapash displays results with appropriate wording (preprocessing inverse\/post-processing). Shapash<strong>\u00a0<\/strong>is useful in an operational context as it enables data scientists to use explicability from exploration to production: You can easily deploy local explainability in production to complete each of your forecasts\/recommendations with a summary of the local explainability.<\/p>\n<p>In this post, we will present the main features of\u00a0Shapash\u00a0and how it operates. We will illustrate the implementation of the library on a concrete use case.<\/p>\n<p>\u00a0<\/p>\n<h3>Elements of context<\/h3>\n<p>\u00a0<\/p>\n<p>Interpretability and explicability of models are hot topics. There are many articles, publications, and open-source contributions about it. All these contributions do not deal with the same issues and challenges.<\/p>\n<p>Most data scientists use these techniques for many reasons: to better understand their models, to check that they are consistent and unbiased, as well as for debugging.<\/p>\n<p>However, there is more to it:<\/p>\n<blockquote>\n<p><em>Intelligibility matters for pedagogic purposes. Intelligible Machine Learning models can be debated with people that are not data specialists: business analysts, final users\u2026<\/em><\/p>\n<\/blockquote>\n<p>Concretely, there are two steps in our Data Science projects that involve non-specialists:<\/p>\n<p><strong>Exploratory step &amp; Model fitting<\/strong><\/p>\n<p>At this step, data scientists and business analysts discuss what is at stake and define the essential data they will integrate into the projects. It requires understanding the subject well and the main drivers of the problem we are modeling.<\/p>\n<p>To do this, data scientists study global explicability, features importance, and which role the model\u2019s top features play. They can also locally look at some individuals, especially outliers. A Web App is interesting at this phase because they need to look at visualizations and graphics. Discussing these results with business analysts is interesting to challenge the approach and validate the model.<\/p>\n<p><strong>Deploying the model in a production environment<\/strong><\/p>\n<p>That\u2019s it! The model is validated, deployed, and gives predictions to the end-users. Local explicability can bring them a lot of value, only if there is a way to provide them with a good, useful, and understandable summary. It will be valuable to them for two reasons:<\/p>\n<ul>\n<li>Transparency brings trust: They will trust models if they understands them.<\/li>\n<li>Human stays in control: No model is 100% reliable. When they can understand the algorithm\u2019s outputs, users can overturn the algorithm suggestions if they think they rest on incorrect data.<\/li>\n<\/ul>\n<p>Shapash\u00a0has been developed to help data scientists to meet these needs.<\/p>\n<p><img class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/max\/625\/1*4rjD3q7UJprqwA9CJ6PyOA.png\" width=\"80%\"><\/p>\n<p>\u00a0<\/p>\n<h3>Shapash key features<\/h3>\n<p>\u00a0<\/p>\n<ul>\n<li>Easy-to-read visualizations, for everyone.<\/li>\n<li>A web app: To understand how a model works, you have to look at multiple graphs, features importance, and global contribution of a feature to a model. A web app is a useful tool for this.<\/li>\n<li>Several methods to show results with appropriate wording (preprocessing inverse, post-processing). You can easily add your data dictionaries,\u00a0<em>category-encoders<\/em>object, or sklearn\u00a0<em>ColumnTransformer<\/em>\u00a0for more explicit outputs.<\/li>\n<li>Functions to easily save\u00a0<em>Pickle<\/em>files and to export results in tables.<\/li>\n<li>Explainability summary: the summary is configurable to fit with your need and to focus on what matters for local explicability.<\/li>\n<li>Ability to easily deploy in a production environment and to complete every prediction\/recommendation with a local explicability summary for each operational apps (Batch or API)<\/li>\n<li>Shapashis open to several ways of proceeding: It can be used to easily access results or to work on better wording. Very few arguments are required to display results. But the more you work with cleaning and documenting the dataset, the clearer the results will be for the end-user.<\/li>\n<\/ul>\n<p>Shapash\u00a0works for Regression, Binary Classification, or Multiclass problems. It is compatible with many models:\u00a0<em>Catboost<\/em>,\u00a0<em>Xgboost<\/em>,\u00a0<em>LightGBM<\/em>,\u00a0<em>Sklearn Ensemble<\/em>,\u00a0<em>Linear models<\/em>,\u00a0<em>SVM<\/em>.<\/p>\n<p>Shapash\u00a0is based on local contributions calculated with Shap (shapley values), Lime, or any technique which allows computing summable local contributions.<\/p>\n<p>\u00a0<\/p>\n<h3>Installation<\/h3>\n<p>\u00a0<\/p>\n<p>You can install the package through pip:<\/p>\n<p>\u00a0<\/p>\n<h3>Shapash Demonstration<\/h3>\n<p>\u00a0<\/p>\n<p>Let\u2019s use<strong>\u00a0<\/strong>Shapash on a concrete dataset. In the rest of this article, we will show you how\u00a0Shapash\u00a0can explore models.<\/p>\n<p>We will use the famous \u201cHouse Prices\u201d dataset from\u00a0<a href=\"https:\/\/www.kaggle.com\/c\/house-prices-advanced-regression-techniques\" target=\"_blank\" rel=\"noopener\">Kaggle<\/a>\u00a0to fit a regressor and predict house prices! Let\u2019s start by loading the Dataset:<\/p>\n<div>\n<pre>import pandas as pd\r\nfrom shapash.data.data_loader import data_loading\r\n\r\nhouse_df, house_dict = data_loading('house_prices')\r\ny_df=house_df['SalePrice'].to_frame()\r\nX_df=house_df[house_df.columns.difference(['SalePrice'])]\r\nhouse_df.head(3)\r\n\r\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p><img class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/proxy\/1*XbL4MPkUPL4Rx9bPOEeQKA.png\" width=\"95%\"><\/p>\n<p>Encode the categorical features:<\/p>\n<div>\n<pre>from category_encoders import OrdinalEncoder\r\n\r\ncategorical_features = [col for col in X_df.columns if X_df[col].dtype == 'object']\r\nencoder = OrdinalEncoder(cols=categorical_features).fit(X_df)\r\nX_df=encoder.transform(X_df)\r\n\r\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p>Train, test split and model fitting:<\/p>\n<div>\n<pre>from sklearn.model_selection import train_test_split\r\nfrom sklearn.ensemble import RandomForestRegressor\r\n\r\nXtrain, Xtest, ytrain, ytest = train_test_split(X_df, y_df, train_size=0.75)\r\nreg = RandomForestRegressor(n_estimators=200, min_samples_leaf=2).fit(Xtrain,ytrain)\r\n\r\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p>And predict test data:<\/p>\n<div>\n<pre>y_pred = pd.DataFrame(reg.predict(Xtest), columns=['pred'], index=Xtest.index)\r\n\r\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<h3>Let\u2019s discover and use Shapash SmartExplainer.<\/h3>\n<p>\u00a0<\/p>\n<p><em>Step 1 \u2014 Import<\/em><\/p>\n<div>\n<pre>from shapash.explainer.smart_explainer import SmartExplainer\r\n\r\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p><em>Step 2 \u2014 Initialise a SmartExplainer Object<\/em><\/p>\n<div>\n<pre>xpl = SmartExplainer(features_dict=house_dict) # Optional parameter \r\n\r\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<ul>\n<li>features_dict: dict that specifies the meaning of each column name of the x pd.DataFrame.<\/li>\n<\/ul>\n<p><em>Step 3 \u2014 Compile<\/em><\/p>\n<div>\n<pre>xpl.compile(\r\n    x=Xtest,\r\n    model=regressor,\r\n    preprocessing=encoder,# Optional: use inverse_transform method\r\n    y_pred=y_pred # Optional\r\n)\r\n\r\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p>The compile method permits to use of another optional parameter:\u00a0<em>postprocess.\u00a0<\/em>It gives the possibility to apply new functions to specify to have better wording (regex, mapping dict, \u2026).<\/p>\n<p>Now, we can display results and understand how the regression model works!<\/p>\n<p><em>Step 4 \u2014 Launching the Web App<\/em><\/p>\n<p>\u00a0<\/p>\n<p>The web app link appears in Jupyter output (access the demo\u00a0<a href=\"https:\/\/shapash-demo.ossbymaif.fr\/\" target=\"_blank\" rel=\"noopener\">here<\/a>).<\/p>\n<p><strong>There are four parts to this Web App:<\/strong><\/p>\n<p><img class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/max\/875\/1*JSQmzdlzjom6X_iRkNnp5g.png\" width=\"90%\"><\/p>\n<p>Each one interacts to help to explore the model easily.<\/p>\n<p><strong><em>Features Importance<\/em><\/strong><strong>:<\/strong>\u00a0you can click on each feature to update the contribution plot below.<\/p>\n<p><strong><em>Contribution plot<\/em><\/strong><strong>:<\/strong>\u00a0How does a feature influence the prediction? Display violin or scatter plot of each local contribution of the feature.<\/p>\n<p><strong><em>Local Plot:<\/em><\/strong><\/p>\n<ul>\n<li>Local explanation: which features contribute the most to the predicted value.<\/li>\n<li>You can use several buttons\/sliders\/lists to configure the summary of this local explainability. We will describe below with the\u00a0<em>filter\u00a0<\/em>method the different parameters you can work your summary with.<\/li>\n<li>This web app is a useful tool to discuss with business analysts the best way to summarize the explainability to meet operational needs.<\/li>\n<\/ul>\n<p><strong><em>Selection Table:<\/em><\/strong>\u00a0It allows the Web App user to select:<\/p>\n<ul>\n<li>A subset to focus the exploration on this subset<\/li>\n<li>A single row to display the associated local explanation<\/li>\n<\/ul>\n<p>How do you use the data table to select a subset? At the top of the table, just below the name of the column that you want to use to filter, specify:<\/p>\n<ul>\n<li><em>=Value, &gt;Value, &lt;Value<\/em><\/li>\n<li><em>If you want to select every row containing a specific word, just type that word without \u201c=\u201d<\/em><\/li>\n<\/ul>\n<p>There are a few options available on this web app (top right button). The most important one is probably the size of the sample (default: 1000). To avoid latency, the web app relies on a sample to display the results. Use this option to modify this sample size.<\/p>\n<p>To kill the app:<\/p>\n<p>\u00a0<\/p>\n<p><em>Step 5 \u2014 The plots<\/em><\/p>\n<p>All the plots are available in jupyter notebooks, and the paragraph below describes the key points of each plot.<\/p>\n<p><strong>Feature Importance<\/strong><\/p>\n<p>This parameter allows comparing features importance of a subset. It is useful to detect specific behavior in a subset.<\/p>\n<div>\n<pre>subset = [ 168, 54, 995, 799, 310, 322, 1374,\r\n          1106, 232, 645, 1170, 1229, 703, 66,  \r\n          886, 160, 191, 1183, 1037, 991, 482,  \r\n          725, 410, 59, 28, 719, 337, 36 ]\r\nxpl.plot.features_importance(selection=subset)\r\n\r\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p><img class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/max\/875\/1*DBVm8lGPnj8hs6FRIeu6vQ.png\" width=\"90%\"><\/p>\n<p><strong>Contribution plot<\/strong><\/p>\n<p>Contribution plots are used to answer questions like:<\/p>\n<p>How a feature impacts my prediction? Does it contribute positively? Is the feature increasingly contributing? decreasingly? Are there any threshold effects? For a categorical variable, how does each modality contributes? This plot completes the importance of the features for the interpretability, the global intelligibility of the model to better understand the influence of a feature on a model.<\/p>\n<p>There are several parameters on this plot. Note that the plot displayed adapts depending on whether you are interested in a categorical or continuous variable (Violin or Scatter) and depending on the type of use case you address (regression, classification).<\/p>\n<div>\n<pre>xpl.plot.contribution_plot(\"OverallQual\")\r\n\r\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p><img class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/max\/875\/1*elwHcvKkztSVVm3khLOKDg.png\" width=\"90%\"><\/p>\n<p>Contribution plot applied to a continuous feature.<\/p>\n<p>Classification Case: Titanic Classifier \u2014 Contribution plot applied to categorical feature.<\/p>\n<p><img class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/max\/625\/1*zA5vqTsbgwkRZ4LG6Q8fRw.png\" width=\"90%\"><\/p>\n<p><img class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/max\/625\/1*n4cCMy0OAsFfiWl-f35WBg.png\" width=\"90%\"><\/p>\n<p><strong>Local plot<\/strong><\/p>\n<p>You can use local plots for local explainability of models.<\/p>\n<p>The <em>filter()<\/em> and <em>local_plot()<\/em> methods allow you to test and choose the best way to summarize the signal that the model has picked up. You can use it during the exploratory phase. You can then deploy this summary in a production environment for the end-user to understand in a few seconds what are the most influential criteria for each recommendation.<\/p>\n<p>We will publish a second article to explain how to deploy local explainability in production.<\/p>\n<p><strong>Combine the filter and local_plot methods<\/strong><\/p>\n<p>Use the <em>filter<\/em> method to specify how to summarize local explainability. You have four parameters to configure your summary:<\/p>\n<ul>\n<li>max_contrib: maximum number of criteria to display<\/li>\n<li>threshold: minimum value of the contribution (in absolute value) necessary to display a criterion<\/li>\n<li>positive: display only positive contribution? Negative? (default None)<\/li>\n<li>features_to_hide: list of features you don\u2019t want to display<\/li>\n<\/ul>\n<p>After defining these parameters, we can display the results with the <em>local_plot()<\/em> method, or export them with <em>to_pandas().<\/em><\/p>\n<div>\n<pre>xpl.filter(max_contrib=8,threshold=100)\r\nxpl.plot.local_plot(index=560)\r\n\r\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p><img class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/max\/875\/1*Cy_nLo4XeVJtHviV5wuxZg.png\" width=\"90%\"><\/p>\n<p>Export to pandas DataFrame:<\/p>\n<div>\n<pre>xpl.filter(max_contrib=3,threshold=1000)\r\nsummary_df = xpl.to_pandas()\r\nsummary_df.head()\r\n\r\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p><img class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/max\/875\/1*68OibxE1EbdWvzTXBN5umQ.png\" width=\"95%\"><\/p>\n<p><strong>Compare plot<\/strong><\/p>\n<p>With the <em>compare_plot()<\/em> method, the SmartExplainer object makes it possible to understand why two or more individuals do not have the same predicted values. The most decisive criterion appears at the top of the plot.<\/p>\n<div>\n<pre>xpl.plot.compare_plot(row_num=[0, 1, 2, 3, 4], max_features=8)\r\n\r\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p><img class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/max\/875\/1*pX-isybSbJTxU3VYBFIWWQ.png\" width='\"90%'><\/p>\n<p>We hope that\u00a0Shapash\u00a0will be useful in building trust in AI. Thank you in advance to all those who will give us their feedback, idea\u2026\u00a0Shapash\u00a0is opensource! Feel free to contribute by commenting on this post or directly on the\u00a0<a href=\"https:\/\/github.com\/MAIF\/shapash\/discussions\" target=\"_blank\" rel=\"noopener\">GitHub discussions<\/a>.<\/p>\n<p><a href=\"https:\/\/pub.towardsai.net\/shapash-making-ml-models-understandable-by-everyone-8f96ad469eb3\" target=\"_blank\" rel=\"noopener\">Original<\/a>. Reposted with permission.<\/p>\n<p>\u00a0<\/p>\n<p><b>Related:<\/b><\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/www.kdnuggets.com\/2021\/04\/shapash-machine-learning-models-understandable.html<\/p>\n","protected":false},"author":0,"featured_media":8186,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/posts\/8185"}],"collection":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/comments?post=8185"}],"version-history":[{"count":0,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/posts\/8185\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/media\/8186"}],"wp:attachment":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/media?parent=8185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/categories?post=8185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/tags?post=8185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}