Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

xMOAI 🗿: Multiobjective Optimization in Explainable Artificial Intelligence

Documentation Status Wheel Version

xMOAI is an open-source package implementing Explainable Artificial Intelligence (XAI) using Multiobjective Optimization (MOO). It is capable of generating a large number of counterfactuals in datasets with several attributes - most of them immutable or very constrained. It supports both regression or classification problems. For classification problems, it does support both problems with trained machine learning models exposing the predicted class probabilities or only the predicted class. It was tested throughly with trained models in scikit-learn, XGBoost, LightGBM and Tensorflow. In practice, it works with any model that exposes an output similar to scikit-learn or Tensorflow predict methods.

Usage

import numpy as np

from xmoai.setup.configure import generate_counterfactuals_classification_proba

from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

# seed
random_state = 0

# getting a dataset
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, 
                                                    random_state=random_state)

# training a machine learning model
clf = RandomForestClassifier(max_depth=2, random_state=random_state)
clf.fit(X_train, y_train)

# getting an individual (X_original), its original prediction (y_original) and
# the desired output (y_desired)
index = 0
X_original = X_test[0,:].reshape(1, -1)
y_original = clf.predict(X_original)
y_original_proba = clf.predict_proba(X_original)
y_desired = 1

print(f'The original prediction was {y_original} with probabilities {y_original_proba}')
print(f'We will attempt to generate counterfactuals where the outcome is {y_desired}.')

# generating counterfactuals
immutable_column_indexes = [2] # let's say we can't change the last column
categorical_columns = {} # there are no categorical columns
integer_columns = [] # there are no columns that only accept integer values
y_acceptable_range = [0.5, 1.0] # we will only accept counterfactuals with the predicted prob. in this range

upper_bounds = np.array(X_train.max(axis=0)*0.8) # this is the maximum allowed number per column
lower_bounds = np.array(X_train.min(axis=0)*0.8) # this is the minimum allowed number per column.
# you may change the bounds depending on the needs specific to the individual being trained.

# running the counterfactual generation algorithm
front, X_generated, algorithms = generate_counterfactuals_classification_proba(clf,
                          X_original, y_desired, immutable_column_indexes,
                          y_acceptable_range, upper_bounds, lower_bounds,
                          categorical_columns, integer_columns, n_gen=20,
                          pop_size=30, max_changed_vars=3, verbose=False, 
                          seed=random_state)

Features

The documentation as well as the code are part of an ongoing research. Currently, it does support:

  • Regression problems
  • Classification problems (probability or single class as outputs)

On the variables, it does support:

  • Decimal and integer variables as values (such as counts, quantities, etc.)
  • Ordinally encoded categorical variables (categories encoded as integers)
  • Setting the upper and lower bounds per variable
  • Setting which columns are immutable
  • Setting which categories are bound to be modified (xMOAI is able to understand only the categories 1, 5, 7 and 15 are allowed categories instead of treating it as a numerical range)
  • Setting the target desired (for regression problems, you can inform the value you want to have as an output; for classification problems, the desired class)
  • Setting the "allowed output range" (for regression problems, you can inform what values are acceptable as outputs instead of a single value. As an example, for a housing prices dataset you may want to find a counterfactual with an output of $100000.00. However, anything between $99000.00 and $105000.00 could also be good prices for your problem. For a classification problem, it is the percentage of certainity of the predicted class considering your problem).

It does not support at the present moment:

  • One-hot encoded categories
  • Models available in hosted servers (i.e. with a REST API endpoint)
  • Multiple allowed intervals for a single attribute (e.g. for a single column, instead of a range of -10 to +20, two ranges of -10 to 0 and +10 to +20).

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Paper

Preprint available in https://doi.org/10.13140/RG.2.2.30680.52480

License

MIT

Metadata

Release files for xmoai 0.0.dev4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xmoai 0.0.dev4
File Size Uploaded
xmoai-0.0.dev4.tar.gz 12.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for xmoai 0.0.dev4
File Interpreter ABI Platform
xmoai-0.0.dev4-py3-none-any.whl Python 3 none any Details

Total release size: 27.1 kB

Release files / xmoai-0.0.dev4.tar.gz

Download URL xmoai-0.0.dev4.tar.gz
Size 12.6 kB
Tags Source
SHA-256 checksum
How to use checksums
d077955b77d45ebaddd471d4435deb0413caa7d56b276130cec50cae291d2662
BLAKE2b-256 checksum
How to use checksums
9b5007a1d3066bc8db5454da04ab4deea5e05881f186ec4e968c3444d359cf4b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6

Release files / xmoai-0.0.dev4-py3-none-any.whl

Download URL xmoai-0.0.dev4-py3-none-any.whl
Size 14.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5bee9347aaa7cf249143fc6d92adbc2a69cedd5b3604bca683bb23cfd684cf89
BLAKE2b-256 checksum
How to use checksums
921982813c2697af2830e27d6ff8c7bb215f4e527296228a5822523710a2cb96
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6

Release history Release notifications | RSS feed

This release

0.0.dev4 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page