Skip to main content

Post hoc explanations for ML models through measures of statistical dependence

Project description

build coverage Python 3.8 Python 3.9 Python 3.10 Python 3.11

Xi - Post hoc explanations

Xi is a python package that implements the paper "Explaining classifiers with measures of statistical association"[1] and "The Xi method: unlocking the mysteries of regression with Statistics"[2

The growing size and complexity of data as well as the need of accurate predictions, forces analysts to use black-box model. While the success of those models extends statistical application, it also increases the need for interpretability and ,when possible, explainability.

The paper proposes an approach to the problem based on measures of statistical association.
Measures of statistical association deliver information regarding the strength of the statistical dependence between the target and the feature(s) of interest, inferring this insight from the data in a model-agnostic fashion.
In this respect, we note that an important class of measures of statistical associations is represented by probabilistic sensitivity measures.

We use these probabilistic sensitivity measures as part of the broad discourse of interpretability in statistical machine learning. For brevity, we call this part the Xi-method.
Briefly, the method consists in evaluating ML model predictions comparing the values of probabilistic sensitivity measures obtained in a model-agnostic fashion, i.e., directly from the data, with the same indices computed replacing the true targets with the ML model forecasts.

To sum up, Xi has three main advantages:

  • Model agnostic: as long as your model outputs predictions, you can use Xi with any model
  • Data agnostic: Xi works with structured (tabular) and unstructured data ( text, image ).
  • Computationally reasonable

Installation

Install from pypi:

pip install xi-method

Usage

The package is quite simple and it's designed to give you post hoc explainations for your dataset and machine learning model with minimal effort.
Import your data in a pandas dataframe format, splitting covariates and independent variable.

from xi_method.utils import load_dataset
from xi_method.ximp import *

# load wine quality
df = load_wine_quality_red_dataset()

Y = df.quality
df.drop(columns='quality', inplace=True)

Create an instance of XIClassifier or XIRegressor depending on the type of problem you are working with:

xi = XIClassifier(m=20)

For the classification tasks, you can specify the number of partitions in three different ways:

  • m: number of partitions can be a dictionary or an integer. The dictionary should have covariate name as key and number of desired partition as value. If m is an integer, the desired number of partition will be applied to all covariates.
  • discrete: A list of covariates name you want to treat as categorical.
  • obs: A dictionary mapping covariates name to number of desired observations in each partition.

For regression tasks, you can only specify m as an integer.

A default m value will be computed if nothing is provided by the user, as indicated in the paper.

To obtain post hoc explanations, run your favorite ML model, save the predictions as numpy array and provide the covariates ( test set) and the predictions to the method explain:

from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(df.values, Y, test_size=0.3, random_state=42)
lr = LogisticRegression(multi_class='multinomial',max_iter=100)
lr.fit(x_train, y_train)
y_pred = lr.predict(x_test)

xi = XIClassifier(m=20)
p = xi.explain(X=x_test, y=y_pred, replicates=10, separation_measurement='L1')

Object p is a python dictionary mapping separation measurement and explanation.
You can easily have access to the explanation:

p.get('L1').explanation

You can choose from different separation measurement, as specified in the paper. You can specify one separation measurement or more than one, using a list.

p = xi.explain(X=x_test, y=y_pred, separation_measurement=['L1','Kuiper'])

Implemented separation measurement:

  • Kullback - Leibler
  • Kuiper
  • L1
  • L2
  • Hellinger

You can get a list of implemented separation measurement running:

from xi_method.utils import *
get_separation_measurement()

Plot your result:

plot(separation_measurement='L1', type='tabular', explain=P, k=10)

References

[1] E. Borgonovo, V. Ghidini, R. Hahn a, E. Plischke (2023). Explaining classifiers with measures of statistical association Computational Statistics and Data Analysis, Volume 182, June 2023, 107701

[2] V. Ghidini (2023). The Xi method: unlocking the mysteries of regression with Statistics

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xi_method-0.1.5.tar.gz (2.8 MB view details)

Uploaded Source

Built Distribution

xi_method-0.1.5-py3-none-any.whl (2.9 MB view details)

Uploaded Python 3

File details

Details for the file xi_method-0.1.5.tar.gz.

File metadata

  • Download URL: xi_method-0.1.5.tar.gz
  • Upload date:
  • Size: 2.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for xi_method-0.1.5.tar.gz
Algorithm Hash digest
SHA256 4c1f834555ef1b4096221a93d3255b2b3578dffafb5450c4e294ee83626eb280
MD5 64b2e0cf18d58fdb16a9cee054d19281
BLAKE2b-256 ac3ca7fcc738b42622a5b157159587e461a07536c719ba1b11b0db7bd1aa2b8a

See more details on using hashes here.

File details

Details for the file xi_method-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: xi_method-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 2.9 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for xi_method-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 253068702cd1f375390a5c1208cb552c0cf07666824bca18ad1d7f831741eacd
MD5 337275d1186da13da536f862ae9195b3
BLAKE2b-256 10038958611498fbe933d9f5053bfe0385b2dbb7d9d9551ad3bb9e15c972899f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page