From large, deep trees to short, clear explanations.
Project description
crystal-tree
The goal of crystal-tree package is to provide simple, crystal-clear, natural language explanations for the predictions from classification Decision Trees.
This simple python package provides an object for obtaining explanations from a (for now scikit-learn's) Decision Tree Classifier.
The implementations consists in the translation of the Decision Tree into an explainable logic program for xclingo.
Given an input, the CrystalTree object will provide the prediction from the original tree, justified by a summarized version of the conditions checked by the tree to ultimately produce the prediction. The explanations are provided as python objects which can be easily represented as text. The text used for the explanations admit some personalization, which allows the user to adapt them particular contexts (different languages, different levels of expertise, etc.)
Installation
python -m pip install crystal-tree
Usage
The following program will train a Decision Tree into the task of predicting types of flowers (throught the well-known Iris dataset), and then it will some explanations as an example. The program requires the following modules to be installed:
python -m pip instll sklearn pandas
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from crystal_tree import CrystalTree
# Loads dataset
X, y = load_iris(return_X_y=True, as_frame=True)
# Trains decision tree
clf = DecisionTreeClassifier()
clf.fit(X,y)
# Translates the classifier into an explainable logic program
crys_tree = CrystalTree(clf)
# Print explanations of input X (two arbitrary rows)
for e in crys_tree.explain(X.iloc[[0, 54]]):
print(e.ascii_tree())
This will produce the following output.
*
|__Predicted class 0 for instance 0
| |__petal width (cm) <= 0.8
*
|__Predicted class 1 for instance 1
| |__petal length (cm) <= 4.9
| |__petal width (cm) in (0.8,1.6]
More examples concerning personalization of explanations can be found in the examples/
directory in this repository.
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 Distribution
Built Distribution
File details
Details for the file crystal-tree-0.2.linux-x86_64.tar.gz
.
File metadata
- Download URL: crystal-tree-0.2.linux-x86_64.tar.gz
- Upload date:
- Size: 12.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 18ab4a163e228a08b15e46dea9eb4b9baa1c3127a4d9291c212004dfa9626748 |
|
MD5 | 66746c3d904b610a587b6f7043a53c18 |
|
BLAKE2b-256 | d0da21775d9ef36dbc0e1700fa4668e7f7110ebc4f735d733fc1c0a205d4eda4 |
File details
Details for the file crystal_tree-0.2-py3-none-any.whl
.
File metadata
- Download URL: crystal_tree-0.2-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7679635a8a8e410b88a99a86d0bddb19208978924252aa56a8d7ae06036c89d8 |
|
MD5 | 16b9f7383d9a66fe52ab4fa3a72f6231 |
|
BLAKE2b-256 | bb29b73c2ec5b5b252b8c9ef915b4aff39a987d9df259b7c7339e634e01352d7 |