Machine Learning

Shapash 1.3.2, announcing new features for more auditable AI

Yann Golhen
May 4, 20216 min read
Photo by AMIT MAHATO on Unsplash
Photo by AMIT MAHATO on Unsplash

Shapash version 1.3.2 is now available and the library has been enriched with a new feature: The Shapash Report, an HTML document that helps to make your models auditable!

Shapash is a Python library released by MAIF data team in January 2021 to make Machine Learning models understandable by everyone.

Shapash allows :

  • the data scientist to easily analyze his models, to share and to validate his approach with his clients

  • the end-user to understand a recommendation/prediction coming from an ML model thanks to an adapted summary of the criteria lead to it.

[Shap](https://github.com/slundberg/shap)ash is currently using a Shap backend to compute local contributions. Shapash provides easy-to-read visualizations and a web app. It displays results with appropriate wording (preprocessing inverse, postprocessing, ...)

You will find the general presentation of Shapash in this article.

Version 1.3.2 is now available and Shapash allows the Data Scientist to document each model he releases into production. Within a few lines of code, he can include in an HTML report all the information about his model (and its associated performance), the data he uses, his learning strategy, ... this report is designed to be easily shared with a Data Protection Officer, an internal audit department, a risk control department, a compliance department or anyone who wants to understand his work.

Context

Artificial intelligence is scary: "What usage is made of personal data? For what purpose is AI developed? what is its impact on employment? on our freedoms? ..."

So many questions that often remain unanswered and leave room for speculation. This is a very topical issue and many players are taking it up.

First of all, public authorities: in France, the ACPR published in 2020 a discussion paper on the governance of artificial intelligence algorithms. The European Union has established a first framework with the GDPR, a framework that could soon be completed by a regulation on AI that would notably address the issues of governance, auditability and control.

Beyond the legal framework, organizations are working for a Responsible and Trustworthy Data Science. For example, Substra Foundation shared an assessment tool that allows each company to independently evaluate its Data Science practice and identify areas for improvement.

Finally, some private companies are actors of a trustworthy AI within their organization. Some French companies have set up governance mechanisms for the ethics of artificial intelligence. On this subject, the Impact IA Think & Do Tank published a guide at the end of 2020 that introduce concrete cases of setting up governance for trustworthy AI. Finally, other players have implemented best practice guides and are sharing them.

With this new version 1.3.2, Shapash by MAIF goes further by contributing to the major issue of model auditability, by facilitating the systematic documentation of these models using templates.

The Shapash Report :

The Shapash Report is a "Standalone" HTML document generated with a line of code by the Data Scientist when he deploys his model to production. It allows him to document his project and to freeze in a "snapshot at the moment" all the information related to the training of his model and to the upstream steps (data prep, scoping,...).

This report helps to improve AI governance by documenting in detail each algorithm deployed. It allows everyone in the organization to understand why, how, with what data, and for what purpose the model was built.

We hope it will be a valuable tool for model auditing and for better AI governance!

An open tool: Each organization can adapt the content of its Shapash Report.

While waiting for a regulation that specifies the standard content for this documentation, it was important for us to offer an open solution. Thus, each company can adapt the tool and define its own "standard" that each data scientist will implement in his projects.

A proposal for a Template

Shapash 1.3.2 proposes a template that everyone can adapt. You will find on the Github repo of the project a tutorial showing how to implement this template and how to modify it as you wish. In the example we propose, you will find the following information:

  • General information: The project title, its description, its purpose, who worked on the development of the template, when...

  • Dataprep: Where do the data come from, where can I find the data preparation programs,...

  • Features Engineering and training strategy: What was the strategy implemented, what are the programs associated with this part.

  • Information about the model: What type of model? Which hyper-parameters were used?

  • Analysis of training and test datasets: Descriptions of all features introduced in the model and analysis of correlations.

  • Overall model explainability: What are the top features of the model? What influence does each feature have on the recommendation/prediction?

  • The performance of the model: Restitution of the metrics associated with the model, distribution of the forecast on the test dataset.

The content of this report is very complete. Also, to facilitate the navigation, the Shapash Report offers a "Table of contents" section as well as different menu buttons.

The [Shapash](https://github.com/MAIF/shapash) Report benefits from all the Shapash data wording functionalities: the functional labels associated with each feature are rendered in this HTML document so that the shared content is understandable by all, and especially by those who did not take part in the model construction.

How to create this report?

As an example, let's test Shapash together on a concrete dataset and create this report. Again, we suggest you to use the famous Kaggle House dataset we love: "Housing Prices" dataset. Let's fit a regressor that predicts house prices! You start by loading the dataset:

text
import pandas as pdfrom shapash.data.data_loader import data_loadinghouse_df, house_dict = data_loading('house_prices')y_df=house_df['SalePrice'].to_frame()X_df=house_df[house_df.columns.difference(['SalePrice'])]
text
house_df.head(3)

Encode the categorical features:

text
from category_encoders import OrdinalEncoder
text
categorical_features = [col for col in X_df.columns if X_df[col].dtype == 'object']encoder = OrdinalEncoder(cols=categorical_features).fit(X_df)X_df=encoder.transform(X_df)

Train, test split and model fitting.

text
Xtrain, Xtest, ytrain, ytest = train_test_split(X_df, y_df, train_size=0.75)regressor = RandomForestRegressor(n_estimators=50).fit(Xtrain, ytrain)

Good job, you have a model ready to go to production! But before that, let's document our project!

Fill your project information

1st step: Create a YML file containing information about your project. You can use the example file available here.

You are welcome to use this file as a template for your own report.

You have now specified all of the information you want to appear, and you can use Shapash to create the report.

Step 2: Declare and compile SmartExplainer object

text
from shapash.explainer.smart_explainer import SmartExplainerxpl = SmartExplainer(features_dict=house_dict) #Optional: Labels 
text
xpl.compile(    x=Xtest,    model=regressor,    preprocessing=encoder, # Optional: Apply inverse_transform )

At this step you can use any method to explore and understand your model. But let's stay focused and create our Shapash report.

You need to pass x_train, y_train and y_test parameters in order to explore the data used when training the model. You will also specify the metrics that you want to display in the report.

text
xpl.generate_report( output_file='output/report.html',  project_info_file='utils/project_info.yml', x_train=Xtrain, y_train=ytrain, y_test=ytest, title_story="House prices report", title_description="""This document is a data science report of the kaggle house prices tutorial project.  It was generated using the Shapash library.""", metrics=[ { 'path': 'sklearn.metrics.mean_absolute_error', 'name': 'Mean absolute error',  }, { 'path': 'sklearn.metrics.mean_squared_error', 'name': 'Mean squared error', } ])

It's done! You created this report !

Customize your own report!

You can go further! Let's customize your report by adding some new sections.

  • First, copy the base report notebook you can find here. This is the notebook that is used to generate the Shapash Report. It is executed and then converted to an HTML file. Only the output of each cell is kept and the code is deleted.

  • Then, delete or add cells depending on what you want to change.

  • Finally, add the parameter notebook_path="path/to/your/custom/report.ipynb" in the generate_report method.

More details are available here.

đź’ˇ Share your ideas!

Feel free to visit [Shapash](https://github.com/MAIF/shapash) GitHub: you'll find exhaustive documentation about this new feature. You can also leave your messages there (discussions tab) and contribute to the solution by sharing your templates or any other idea. May Shapash contribute to the development of a trustworthy AI!

Related Articles