{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### SHAP for sentiment analysis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An example of SHAP on sentiment analysis. The SHAP explainer for NLP tasks only supports `TextClassificationPipeline` in the `transformer` library. If using this explainer, please cite the original work: https://github.com/slundberg/shap." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import transformers\n", "from omnixai.data.text import Text\n", "from omnixai.explainers.nlp import ShapText" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# A transformer model for sentiment analysis\n", "model = transformers.pipeline(\n", " 'sentiment-analysis',\n", " model='distilbert-base-uncased-finetuned-sst-2-english',\n", " return_all_scores=True\n", ")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/156 [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "Partition explainer: 7it [00:17, 3.51s/it] \n" ] }, { "data": { "text/html": [ "