Skip to main content

Post-host prototype-based explanations with rules for time-series classifiers

Project description

PyPI License PyPI - Downloads Documentation Status

TSProto

Post-host prototype-based explanations with rules for time-series classifiers.

Key features:

  • Extracts interpretable prototype for any black-box model and creates a decision tree, where each node is constructed from the visual prototype
  • Integrated with SHAP explainer, as a backbone for extraction of interpretable components (However, SHAP can be replaced with any other feature-importance method)

Install

TSProto can be installed from either PyPI or directly from source code from this repository.

To install form PyPI:

pip install tsproto

To install from source code:

git clone https://github.com/sbobek/tsproto
cd tsproto
pip install .

Usage

For full examples on two illustrative cases go to:

  • Example of extracting sine wave prototype and explaining class with existence ora absence of a prototype: Jupyter Notebook
  • Example of extracting sine wave as a prototype end explaining class by difference in frequency of a prototype Jupyter Notebook

The basic usage of the TSProto assuming you have your model trained is straightforward:

from tsproto.models import *
from tsproto.utils import *

#assuming that trainX, trainy and model are given

pe = PrototypeEncoder(clf, n_clusters=2, min_size=50, method='dtw',
                      descriptors=['existance'],
                      jump=1, pen=1,multiplier=2,n_jobs=-1,
                      verbose=1)

trainX, shapclass = getshap(model=model, X=trainX, y=trainy,shap_version='deep',
                        bg_size = 1000,  absshap = True)

#The input needs to be a 3D vector: number of samples, lenght of time-series, number of dimensions (features)
trainXproto = train.reshape((trainX.shape[0], trainX.shape[1],1))
shapclassXproto = shapclass.reshape((shapclass.shape[0], shapclass.shape[1],1))

ohe_train, features, target_ohe,weights = pe.fit_transform(trainXproto,shapclassXproto)

im  = InterpretableModel()
acc,prec,rec,f1,interpretable_model = im.fit_or_predict(ohe_train, features,
                        target_ohe,
                        intclf=None, # if intclf is given, the funciton behaves as predict,
                        verbose=0, max_depth=2, min_samples_leaf=0.05,
                        weights=None)
                 

After the Interpretable model has been created it now can be visualised.

                       
# Visualize model
from  tsproto.plots import *

ds_final = ohe_train.copy()
dot = export_decision_tree_with_embedded_histograms(decision_tree=interpretable_model, 
                                              dataset=ds_final, 
                                              target_name='target', 
                                              feature_names=features, 
                                              filename='synthetic', 
                                              proto_encoder=pe, figsize=(6,3))

from IPython.display import SVG, Image
Image('synthetic.png')

Prototype visualization

Cite this work

More details on how the TSProto works and evaluation benchmarks can eb found in the following paper:

Comming soon

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

tsproto-0.3.0.tar.gz (24.4 kB view details)

Uploaded Source

Built Distribution

tsproto-0.3.0-py3-none-any.whl (23.5 kB view details)

Uploaded Python 3

File details

Details for the file tsproto-0.3.0.tar.gz.

File metadata

  • Download URL: tsproto-0.3.0.tar.gz
  • Upload date:
  • Size: 24.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.1

File hashes

Hashes for tsproto-0.3.0.tar.gz
Algorithm Hash digest
SHA256 6d96839062861057c43e841c9bdada95bcb31c0345abcd0c361114cd64a50257
MD5 04f1a199986bcc766781b5041a894f9c
BLAKE2b-256 eeb64841ba486575aae106634e6ff7d5a95270541496a658d732ea5d4e3ad596

See more details on using hashes here.

File details

Details for the file tsproto-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: tsproto-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 23.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.1

File hashes

Hashes for tsproto-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 629dac189813233cf949c4ca42619ba8c01deb44878205f75e4bd84be2e89f06
MD5 b0169eb238a6760ab75d8ee545d477ff
BLAKE2b-256 22588f80e888cfbcd8439838bd39a0b93921b4853030dafd9edd6e4986ff0600

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