Skip to main content

From large, deep trees to short, clear explanations.

Project description

crystal-tree

Binder

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.)

Try it out without installing anything in Crystal-tree Binder

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]],factor=1, feature_names= load_iris().feature_names):
    print(e)

This will produce the following output.

  *
  |__"Predicted class 0 for instance 0 (100 probability)"
  |  |__"petal width (cm) <= 0.8"

  *
  |__"Predicted class 1 for instance 1 (100 probability)"
  |  |__"petal width (cm) in (0.8,1.6]"
  |  |__"petal length (cm) <= 4.9"

More examples concerning personalization of explanations can be found in the examples/ directory in this repository.

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

crystal_tree-0.3.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

crystal_tree-0.3-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file crystal_tree-0.3.tar.gz.

File metadata

  • Download URL: crystal_tree-0.3.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for crystal_tree-0.3.tar.gz
Algorithm Hash digest
SHA256 f5763e827c9bf3fcdce416fae47b5b5f0929b0054dfbdfabb277c12b4eb48671
MD5 9e7883b1cc0ba103a576be397cecb52c
BLAKE2b-256 478b78045bace9639bb4541b396415f28308bf0138fe60538840d46f562ecb8b

See more details on using hashes here.

File details

Details for the file crystal_tree-0.3-py3-none-any.whl.

File metadata

  • Download URL: crystal_tree-0.3-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for crystal_tree-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9fc579d1f6bc660505b69039828f9ef7f3edf59221b2de38a858cabce0efa22a
MD5 8ea17d159ba0262b3de176f55dce1594
BLAKE2b-256 74df5fac9adbc548d8f1edbf9df737a7d037c1faf396485262714c3ee9e95b30

See more details on using hashes here.

Supported by

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