Package: text-explainability
A generic explainability architecture for explaining text machine learning modelstext_explainabilityprovides ageneric architecturefrom which well-known state-of-the-art explainability approaches for text can be composed. This modular architecture allows components to be swapped out and combined, toquickly develop new types of explainability approachesfor (natural language) text, or toimprove a plethora of approaches by improving a single module.Several example methods are included, which providelocal explanations(explaining the prediction of a single instance, e.g.LIMEandSHAP) orglobal explanations(explaining the dataset, or model behavior on the dataset, e.g.TokenFrequencyandMMDCritic). By replacing the default modules (e.g. local data generation, global data sampling or improved embedding methods), these methods can be improved upon or new methods can be introduced.© Marcel Robeer, 2021Quick tourLocal explanation: explain a models' prediction on a given sample, self-provided or from a dataset.fromtext_explainabilityimportLIME,LocalTree# Define sample to explainsample='Explain why this is positive and not negative!'# LIME explanation (local feature importance)LIME().explain(sample,model).scores# List of local rules, extracted from treeLocalTree().explain(sample,model).rulesGlobal explanation: explain the whole dataset (e.g. train set, test set), and what they look like for the ground-truth or predicted labels.fromtext_explainabilityimportimport_data,TokenFrequency,MMDCritic# Import datasetenv=import_data('./datasets/test.csv',data_cols=['fulltext'],label_cols=['label'])# Top-k most frequent tokens per labelTokenFrequency(env.dataset).explain(labelprovider=env.labels,explain_model=False,k=3)# 2 prototypes and 1 criticisms for the datasetMMDCritic(env.dataset)(n_prototypes=2,n_criticisms=1)InstallationSee theinstallationinstructions for an extended installation guide.MethodInstructionspipInstall fromPyPIviapip3 install text_explainability. To speed up the explanation generation process usepip3 install text_explainability[fast].LocalClone this repository and install viapip3 install -e .or locally runpython3 setup.py install.DocumentationFull documentation of the latest version is provided athttps://text-explainability.readthedocs.io/.Example usageSeeexample usageto see an example of how the package can be used, or run the lines inexample_usage.pyto do explore it interactively.Explanation methods includedtext_explainabilityincludes methods for model-agnosticlocal explanationandglobal explanation. Each of these methods can be fully customized to fit the explainees' needs.TypeExplanation methodDescriptionPaper/linkLocal explanationLIMECalculate feature attribution withLocal Intepretable Model-Agnostic Explanations(LIME).[Ribeiro2016],interpretable-ml/limeKernelSHAPCalculate feature attribution withShapley Additive Explanations(SHAP).[Lundberg2017],interpretable-ml/shapLocalTreeFit a local decision tree around a single decision.[Guidotti2018]LocalRulesFit a local sparse set of label-specific rules usingSkopeRules.github/skope-rulesFoilTreeFit a local contrastive/counterfactual decision tree around a single decision.[Robeer2018]BayLIMEBayesian extension of LIME for include prior knowledge and more consistent explanations.[Zhao201]Global explanationTokenFrequencyShow the top-knumber of tokens for each ground-truth or predicted label.TokenInformationShow the top-ktoken mutual information for a dataset or model.wikipedia/mutual_informationKMedoidsEmbed instances and find top-nprototypes (can also be performed for each label usingLabelwiseKMedoids).interpretable-ml/prototypesMMDCriticEmbed instances and find top-nprototypes and top-ncriticisms (can also be performed for each label usingLabelwiseMMDCritic).[Kim2016],interpretable-ml/prototypesReleasestext_explainabilityis officially released throughPyPI.SeeCHANGELOG.mdfor a full overview of the changes for each version.Extensionstext_explainabilitycan be extended to also performsensitivity testing, checking for machine learning model robustness and fairness. Thetext_sensitivitypackage is available throughPyPIand fully documented athttps://text-sensitivity.rtfd.io/.Citation@misc{text_explainability,title={Python package text\_explainability},author={Marcel Robeer},howpublished={\url{https://git.science.uu.nl/m.j.robeer/text_explainability}},year={2021}}MaintenanceContributorsMarcel Robeer(@m.j.robeer)Michiel Bron(@mpbron-phd)TodoTasks yet to be done:Implement local post-hoc explanations:Implement AnchorsImplement global post-hoc explanations:Representative subsetAdd support for regression modelsMore complex data augmentationTop-k replacement (e.g. according to LM / WordNet)Tokens to exclude from being changedBag-of-words style replacementsAdd rule-based return typeWrite more testsCreditsFlorian Gardin, Ronan Gautier, Nicolas Goix, Bibi Ndiaye and Jean-Matthieu Schertzer.Skope-rules. 2020.Riccardo Guidotti, Anna Monreale, Salvatore Ruggieri, Dino Pedreschi, Franco Turini and Fosca Gianotti.Local Rule-Based Explanations of Black Box Decision Systems. 2018.Been Kim, Rajiv Khanna and Oluwasanmi O. Koyejo.Examples are not Enough, Learn to Criticize! Criticism for Interpretability.Advances in Neural Information Processing Systems (NIPS 2016). 2016.Scott Lundberg and Su-In Lee.A Unified Approach to Interpreting Model Predictions.31st Conference on Neural Information Processing Systems (NIPS 2017). 2017.Christoph Molnar.Interpretable Machine Learning: A Guide for Making Black Box Models Explainable. 2021.Marco Tulio Ribeiro, Sameer Singh and Carlos Guestrin."Why Should I Trust You?": Explaining the Predictions of Any Classifier.Proceedings of the 2016 Conference of the North American Chapter of the Association for Computational Linguistics (NAACL 2016). 2016.Marco Tulio Ribeiro, Sameer Singh and Carlos Guestrin.Anchors: High-Precision Model-Agnostic Explanations.AAAI Conference on Artificial Intelligence (AAAI). 2018.Jasper van der Waa, Marcel Robeer, Jurriaan van Diggelen, Matthieu Brinkhuis and Mark Neerincx."Contrastive Explanations with Local Foil Trees".2018 Workshop on Human Interpretability in Machine Learning (WHI 2018). 2018.