Explaining Machine Learning Classifiers in Python
Project description
PyXAI - Python eXplainable AI
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
File details
Details for the file pyxai-0.8.5-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: pyxai-0.8.5-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 7.1 MB
- Tags: PyPy, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b666c23f648a072a5456ca02e45675f3fb0ed5f000381ea8efa0b6a0cc1f4421 |
|
MD5 | b033a9206cfebaca530f677192f19fee |
|
BLAKE2b-256 | 9800d216a3c227443dd7adb2618d35b06795c379d9c3923047c1b7542bf5ea3a |
File details
Details for the file pyxai-0.8.5-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: pyxai-0.8.5-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 7.1 MB
- Tags: PyPy, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 07a7c2eeb32688842568b366e81636bd10a289266a58ae0cfed6ed6d02f1b687 |
|
MD5 | cdf71c68ccea389fdde899d61d33ddf2 |
|
BLAKE2b-256 | 8a18b98c034b035cf1058f31854ead7ede0660eba4eeb036448474d823a7d99e |
File details
Details for the file pyxai-0.8.5-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: pyxai-0.8.5-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 7.1 MB
- Tags: PyPy, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 61769c2910ee13fc9578bb8eccaa92f196ff68c0bc125cdc6a70c2bfd10ffa98 |
|
MD5 | e97b15eff1d2ef1e27df5532dd3f52f0 |
|
BLAKE2b-256 | 4c2f11f24969a382e0de122464360a9a551cfbacd554fcb9a0bc50dfa1881e3f |
File details
Details for the file pyxai-0.8.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: pyxai-0.8.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 7.4 MB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c60fe7bfa54eafcd3feea033b06e929c6230aea83beecfebd0ca5c24367ca66 |
|
MD5 | e0857c113316dbc84fc7a98d93afbddd |
|
BLAKE2b-256 | 78262dbaeed8a3f9666b77ebc35118fff16508e6026ee31f9e5599adbb20ee69 |
File details
Details for the file pyxai-0.8.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: pyxai-0.8.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 7.4 MB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2f256e96cf9b903bbd90d23a4f3b434c90a10af6a9f572fd2fc9b138076445e |
|
MD5 | 479c21db7bf67fdb0d076a793caf5cea |
|
BLAKE2b-256 | 0fe41e81d2d96c7d691bcb22bafe59aa8845c3ec36812a86ada4f0f594abc181 |
File details
Details for the file pyxai-0.8.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: pyxai-0.8.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 7.4 MB
- Tags: CPython 3.9, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3fd146f20683fa8f97d01a864af92c6d2fda837a6edbd42b32a77f7fc3446954 |
|
MD5 | d4fc2c921a7d045e52d0911a11226d88 |
|
BLAKE2b-256 | 338da33f6f8b65323369bce17521f2a956d0d760bd271adaffa655c3a578685d |
File details
Details for the file pyxai-0.8.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: pyxai-0.8.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 7.4 MB
- Tags: CPython 3.8, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e8e46dcd6a70c497ea502dfbc16119c98fe372db6d8ae0c755cea1473ecc70dc |
|
MD5 | 579b74219b895c60d3147fde9f675475 |
|
BLAKE2b-256 | 32f193ff357b5488737cf7f00e8a97031857661b1ca07a4f7242188e6a78bc61 |
File details
Details for the file pyxai-0.8.5-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: pyxai-0.8.5-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 7.4 MB
- Tags: CPython 3.7m, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc0b98f1b1db27820848590398daf7ce679f9379795f64501c67882de372ce2a |
|
MD5 | 68c1bd0c538434201022299cb18a0125 |
|
BLAKE2b-256 | bf513564262f37b7fedf59151280a67921a5d1f6e62aba0d53c79c2b9a0a0bdf |
File details
Details for the file pyxai-0.8.5-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: pyxai-0.8.5-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 7.4 MB
- Tags: CPython 3.6m, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b6e189761559f3f2263e8868cd171088ff186ff9b62b046aed51aea0754c4cb9 |
|
MD5 | 66e8f62579042e22975ae618e8a294ad |
|
BLAKE2b-256 | 58a275ff570427caa21bbcd2db6b94c789c2b35ded6a77b2047caaabca3789db |