.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/language/plot_sentiment_analysis.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_language_plot_sentiment_analysis.py: Explaining Sentiment Analysis ============================== This example shows how to explain a sentiment classifier using ``shapiq``. Each token in the input text becomes a player in a cooperative game, and Shapley values quantify each token's contribution to the predicted sentiment. We use the :class:`~shapiq_games.benchmark.SentimentAnalysisLocalXAI` game from the ``shapiq_games`` package, which wraps a pretrained DistilBERT model fine-tuned on IMDb reviews. .. GENERATED FROM PYTHON SOURCE LINES 13-25 .. code-block:: Python from __future__ import annotations import os # Prevent OpenMP/MKL thread conflicts with PyTorch backend os.environ.setdefault("OMP_NUM_THREADS", "1") os.environ.setdefault("MKL_NUM_THREADS", "1") import shapiq from shapiq_games.benchmark import SentimentAnalysisLocalXAI .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/shapiq/checkouts/latest/examples/language/plot_sentiment_analysis.py:23: DeprecationWarning: This collection of games is deprecated and will be removed in a future version. from shapiq_games.benchmark import SentimentAnalysisLocalXAI .. GENERATED FROM PYTHON SOURCE LINES 26-32 Set Up the Sentiment Game -------------------------- Each word in the input becomes a player. The game value for a coalition is the model's sentiment score when only those tokens are visible (absent tokens are replaced with ``[MASK]``). The score is normalized so that the empty coalition maps to 0. .. GENERATED FROM PYTHON SOURCE LINES 32-43 .. code-block:: Python game = SentimentAnalysisLocalXAI( input_text="I really loved this amazing film", mask_strategy="mask", normalize=True, ) token_names = game.input_text.split() print(f"Tokens (players): {token_names}") print(f"Number of players: {game.n_players}") print(f"Grand coalition (full-text sentiment): {game.grand_coalition_value:.3f}") .. rst-class:: sphx-glr-script-out .. code-block:: none Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads. Loading weights: 0%| | 0/104 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_sentiment_analysis.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_sentiment_analysis.zip `