pyCeterisParibus: explaining Machine Learning models with Ceteris Paribus Profiles in Python

Machine learning is needed and used everywhere. It has fundamentally changed all datadriven disciplines, like health-care, biology, finance, legal, military, security, transportation, and many others. The increasing availability of large annotated data sources combined with recent developments in Machine Learning revolutionizes many disciplines. However, predictive models become more and more complex. It is not uncommon to have ensembles of predictive models with thousands or millions of parameters. Such models act as blackboxes. It is almost impossible for a human to understand reasons for model decisions.


Introduction
Machine learning is needed and used everywhere. It has fundamentally changed all datadriven disciplines, like health-care, biology, finance, legal, military, security, transportation, and many others. The increasing availability of large annotated data sources combined with recent developments in Machine Learning revolutionizes many disciplines. However, predictive models become more and more complex. It is not uncommon to have ensembles of predictive models with thousands or millions of parameters. Such models act as blackboxes. It is almost impossible for a human to understand reasons for model decisions.
This lack of interpretability often leads to harmful situations. Models are not working properly or are hard to debug, results are biased in a systematic way, data drift leads to the deterioration in models performance, the model is wrong but no-one can explain what caused a wrong prediction. Many examples for these problems are listed in the Weapons of Math Destruction, a bestseller with an expressive subtitle How Big Data Increases Inequality and Threatens Democracy (O'Neil, 2016). Reactions for some of these problems are new legal regulations, like the General Data Protection Regulation and the Right to Explanation (Ruiz, 2018).
An interesting alternative to these tools is the methodology of Ceteris Paribus Profiles and their averages called Partial Dependency Plots. They enable to understand how the model response would change if a selected variable is changed. It's a perfect tool for What-If scenarios. Ceteris Paribus is a Latin phrase meaning all else unchanged. These plots present the change in model response as the values of one feature change with all others being fixed. Ceteris Paribus method is model-agnostic -it works for any Machine Learning model. The idea is an extension of PDP (Partial Dependency Plots) (Friedman, 2001) and ICE (Individual Conditional Expectations) plots (Goldstein, Kapelner, Bleich, & Pitkin, 2015). It allows explaining single observations for multiple variables at the same time.
In this paper, we introduce a pyCeterisParibus library for Python that supports a wide range of tools built on Ceteris Paribus Profiles. There might be several motivations behind utilizing this idea. Imagine a person gets a low credit score. The client wants to understand how to increase the score and the scoring institution (e.g., a bank) should be able to answer such questions. Moreover, this method is useful for researchers and developers to analyze, debug, explain and improve Machine Learning models, assisting the entire process of the model design. The more detailed demonstration is available in the Examples section.

pyCeterisParibus library
pyCeterisParibus is a Python library based on an R package CeterisParibus (Biecek, 2019). It is an open source software released on the Apache license.
The workflow consists of three steps: • wrapping models into a unified representation • calculating profiles for given observations, variables and models • plotting the profiles Plots are drawn using a separate D3.js library which produces interactive Ceteris Paribus plots for both Python (this package) and R implementations. This package allows explaining multiple observations and observing local behaviours of the model. For this purpose, methods for sampling and selecting neighbouring observations are implemented along with the Gower's distance (Gower, 1971) function. A more detailed description might be found in the package documentation.

Examples
We demonstrate Ceteris Paribus Plots using the well-known Titanic dataset. In this problem, we examine the chance of survival for Titanic passengers. We start with preprocessing the data and creating an XGBoost model. import pandas as pd df = pd.read_csv('titanic_train.csv') Here the pyCeterisParibus starts. Since this library works in a model agnostic fashion, first we need to create a wrapper around the model with uniform predict interface.

Single variable profile
Let's look at Mr Ernest James Crease, the 19-year-old man, travelling on the 3rd class from Southampton with an 8 pounds ticket in his pocket. He died on the Titanic. Most likely, this would not have been the case had Ernest been a few years younger. Figure  1 presents the chance of survival for a person like Ernest at different ages. We can see things were tough for people like him unless they were a child. Having calculated the profile we can plot it. Note, that plot_notebook might be used instead of plot when used in Jupyter notebooks.

Many models
The above picture explains the prediction of XGBoost model. What if we compare various models?

Profiles for many variables
Would she have returned home if she had travelled at 3rd class or if she had been a man? As we can observe (Figure 3)     passenger chances of survival were high regardless of age. Note, this was different in the case of Ernest. Place of embarkment (Cherbourg) has no influence, which is expected behaviour.

Feature interactions and average response
Now, what if we look at passengers most similar to Miss. Eustis (middle-aged, upper class)?
Plot function comes with extensive customization options. List of all parameters might be found in the documentation. Additionally, one can interact with the plot by hovering over a point of interest to see more details. Similarly, there is an interactive table with options for highlighting relevant elements as well as filtering and sorting rows.