Skip to main content

Explaining Machine Learning Classifiers in Python

Project description

PyXAI - Python eXplainable AI

PyXAI

Documentation: http://www.cril.univ-artois.fr/pyxai/

Git: https://github.com/crillab/pyxai

PyXAI is a Python library (version 3.6 or later) allowing to bring explanations of various forms from classifiers resulting of machine learning techniques (Decision Tree, Random Forest, Boosted Tree).

More precisely, several types of explanations for the classification task of a given instance X can be computed:

  • Abductive explanations for X are intended to explain why X has been classified in the way it has been classified by the ML model (thus, addressing the “Why?” question).
  • Contrastive explanations for X is to explain why X has not been classified by the ML model as the user expected it (thus, addressing the “Why not?” question).

In addition to finding explanations, PyXAI also contains methods that perform operations (production, saving, loading) on models and instances. Currently, these helping methods are available using two ML libraries:

  • Scikit-learn: a software machine learning library
  • XGBoost: an optimized distributed gradient boosting library

Note that it is quite possible to find explanations of models coming from other libraries.

As an illustration, below, you can find an example of use:

from pyxai import Learning, Explainer, Tools

learner = Learning.Scikitlearn("../dataset/iris.csv")
model = learner.evaluate(method=Learning.HOLD_OUT, output=Learning.DT)
instance, prediction = learner.get_instances(model, n=1, correct=True, predictions=[0])

explainer = Explainer.initialize(model, instance)
print("instance:", instance)
print("implicant:", explainer.binary_representation)

sufficient_reason = explainer.sufficient_reason(n=1)
print("sufficient_reason:", sufficient_reason)
print("to_features:", explainer.to_features(sufficient_reason))

instance, prediction = learner.get_instances(model, n=1, correct=False)
explainer.set_instance(instance)
contrastive_reason = explainer.contrastive_reason()
print("contrastive reason", contrastive_reason)
print("to_features:", explainer.to_features(contrastive_reason))

Installation

Installation from PyPi

The Python Package Index (PyPi) is the easiest way of installing PyXAI.

Note that you need first Python 3 (version 3.6, or later) to be installed. You can do it, for example, from python.org

Installing PyXAI (Linux)

Check if 'python3-pip' is installed. If it is not the case, execute:

sudo apt install python3-pip

Or check if you have the last installed version:

python3 -m pip install --upgrade pip

Then, install PyXAI with the command 'pip3':

python3 -m pip install pyxai

Installing PyXAI (Mac OS)

PyXAI is currently compatible with Mac OS but without PyPi, see the installation (alternative) by Cloning from GitHub section for more details.

Installing PyXAI (Windows)

PyXAI is currently not compatible with windows (work in progress).

Updating the Version of PyXAI (for PyPi)

For updating your version of PyXAI, simply execute:

For linux/Mac:

python3 -m pip install -U pyxai

Installation (alternative) by Cloning from GitHub

An alternative to PyPi is to clone the code from GitHub.

Here is an illustration for linux. We assume that Python 3 is installed, and consequently 'pip3' is also installed. In a console, type:

git clone https://gitlab.univ-artois.fr/expekctation/software/PyLearningExplanation.git

You may need to update the environment variable 'PYTHONPATH', by typing for example:

export PYTHONPATH="${PYTHONPATH}:${PWD}/.."

Get the last version of pip:

python3 -m pip install --upgrade pip

There are a few packages that PyXAI depends on that must be installed:

python3 -m pip install numpy
python3 -m pip install wheel
python3 -m pip install pandas
python3 -m pip install termcolor
python3 -m pip install shap
python3 -m pip install wordfreq
python3 -m pip install python-sat[pblib,aiger]
python3 -m pip install xgboost
python3 -m pip install lxml
python3 -m pip install pycsp3
python3 -m pip install matplotlib

To compile the c++ code (python C extensions):

python3 setup.py install --user

Of course, for this step, you need a C++ compiler.

Unfortunately, the compiled C extensions are not take into account in a virtual environment, therefore you must type (we consider here that the virtual environment is in the 'env' directory and you are in the 'PyXAI' directory):

cp build/lib.linux-x86_64-3.6/c_explainer.cpython-36m-x86_64-linux-gnu.so env/lib/python3.6/site-packages/.

This last command depend of your python version (here: 3.6).

Finally, you can test an example:

python3 examples/DT/BuilderOrchids.py 

Virtual Environment

Create and activate a new virtual environment:

sudo apt-get install python3.6-venv
python3.6 -m venv env
source env/bin/activate

Update pip:

python3.6 -m pip install -U pip

With this new version of pip, it is possible that you have to clear the pip cache:

python3 -m pip cache purge

Now you can do the "Installation from PyPi" or the "Installation (alternative) by Cloning from GitHub".

Note that if you want install dependencies without internet connection, you can build a requirement.txt file:

python3.6 -m pip freeze > requirements.txt 
python3.6 -m pip download -r requirements.txt -d requirements-download/
pip install -r requirements.txt --find-links=requirements-download --no-index

For deactivating the environment:

deactivate

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

pyxai-0.8.6-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.1 MB view details)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pyxai-0.8.6-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.1 MB view details)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pyxai-0.8.6-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.1 MB view details)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pyxai-0.8.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.4 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

pyxai-0.8.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.4 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pyxai-0.8.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.4 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pyxai-0.8.6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.4 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

pyxai-0.8.6-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.4 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

pyxai-0.8.6-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.4 MB view details)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

File details

Details for the file pyxai-0.8.6-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyxai-0.8.6-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cb080ec5c033e9ce1db6d8ad413c0f406d08c161c7b0869e323e498cb1720170
MD5 483d1859f3ac3fde01b47c50582c8d7b
BLAKE2b-256 b3ea087765ed67df4f6fbecdc52d24ef37904fb6ba0d5911e33336f22efbe17d

See more details on using hashes here.

File details

Details for the file pyxai-0.8.6-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyxai-0.8.6-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 95c7e9f9f7082e36481a171ef6f7d8076acd9db97cd395026833d300b29a1a00
MD5 81811b00ef3945e0e9d690b3f183b043
BLAKE2b-256 1ed9e7c900af1bd970067fe58992d1b3250d08baf062c570e85cac115e2b10f5

See more details on using hashes here.

File details

Details for the file pyxai-0.8.6-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyxai-0.8.6-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a95e028a65933370ebae1d2ad421fff25e6d775b2cfaf1909eb9ffb1ee4a4925
MD5 b1e3617e9a82303e9eb466658e3830f5
BLAKE2b-256 48dfb67c98c57ec906ef148d6b254a1d796ef6cdd142771b32faaf2fa7692c55

See more details on using hashes here.

File details

Details for the file pyxai-0.8.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyxai-0.8.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 969f4a738545b0524c52875a170f09939a5b532d3ddd0065be98c9bf232282e8
MD5 8d4e55e6724829fb8e4eefd131661feb
BLAKE2b-256 34f31e5db25aa07278b2acc532a909f622a96b1e41d5dc74503e509fc362042b

See more details on using hashes here.

File details

Details for the file pyxai-0.8.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyxai-0.8.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 efdcbb91a2d4fbdfa30f972d7825e13be11e6588f2716524f17fe66dfbc6594b
MD5 9a350e3ffad779b1f34cd368036dfb08
BLAKE2b-256 e11394f1c481934d50931c245640af213e7a730433e18c4a6671d73be62cec98

See more details on using hashes here.

File details

Details for the file pyxai-0.8.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyxai-0.8.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4cf5e42172282105daf477c13fab21a279f5380384004110b8737282833c4ed7
MD5 8ae7f1ae724dc821353c3db86f5999f1
BLAKE2b-256 285b1357a5cc2362f879fb3271420d344bd3f2a5de440b4626b851a4429f10c7

See more details on using hashes here.

File details

Details for the file pyxai-0.8.6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyxai-0.8.6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 23cfe5439c0d27a26a7953b24d896c245beb282b0c97ca8daafd666407cfa742
MD5 1b0e800ce7233e2f5c4e95f470f7b089
BLAKE2b-256 98092c712151454c1bd4727817c47aba4c62b8908cc9f3f285ee6c4c0fefd3a0

See more details on using hashes here.

File details

Details for the file pyxai-0.8.6-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyxai-0.8.6-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4e2bfab5c444db79581ce334321e2391e0d70a794aff18c73c77dc26eec0d882
MD5 668d5b7a5660321e715f3713fb3ed517
BLAKE2b-256 434616a14fc1476befaab12b1bb0293d4fd2e40638c159a29ab1b1ddb91e0dcb

See more details on using hashes here.

File details

Details for the file pyxai-0.8.6-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyxai-0.8.6-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2779959bd0cab9ad9a0da7cee7717bbac1fdd7dd4db44c10dd4f0c941010114d
MD5 33874ffc24844d2c6695156c86b2c1f5
BLAKE2b-256 be1638feea5209a6101a0b07e8469f695c3715fc542ec186078ac54db2a3faba

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