shapiq.plot.sentence_plot

shapiq.plot.sentence_plot(interaction_values, words, *, connected_words=None, chars_per_line=35, font_family='sans-serif', show=False, max_score=None)[source]

Plots the first order effects (attributions) of a sentence or paragraph.

An example of the plot is shown below.

../_images/sentence_plot_example.png
Parameters:
  • interaction_values (InteractionValues) – The interaction values as an interaction object.

  • words (Sequence[str]) – The words of the sentence or a paragraph of text.

  • connected_words (Sequence[tuple[str, str]] | None) – A list of tuples with connected words. Defaults to None. If two ‘words’ are connected, the plot will not add a space between them (e.g., the parts “enjoy” and “able” would be connected to “enjoyable” with potentially different attributions for each part).

  • chars_per_line (int) – The maximum number of characters per line. Defaults to 35 after which the text will be wrapped to the next line. Connected words receive a ‘-’ in front of them.

  • font_family (str) – The font family used for the plot. Defaults to sans-serif. For a list of available font families, see the matplotlib documentation of matplotlib.font_manager.FontProperties. Note the plot is optimized for sans-serif.

  • max_score (float | None) – The maximum score for the attributions to scale the colors and alpha values. This is useful if you want to compare the attributions of different sentences and both plots should have the same color scale. Defaults to None.

  • show (bool) – Whether to show the plot. Defaults to False.

Return type:

tuple[Figure, Axes] | None

Returns:

If show is True, the function returns None. Otherwise, it returns a tuple with the figure and the axis of the plot.

Example

>>> import numpy as np
>>> from shapiq.plot import sentence_plot
>>> iv = InteractionValues(
...    values=np.array([0.45, 0.01, 0.67, -0.2, -0.05, 0.7, 0.1, -0.04, 0.56, 0.7]),
...    index="SV",
...    n_players=10,
...    min_order=1,
...    max_order=1,
...    estimated=False,
...    baseline_value=0.0,
... )
>>> words = ["I", "really", "enjoy", "working", "with", "Shapley", "values", "in", "Python", "!"]
>>> connected_words = [("Shapley", "values")]
>>> fig, ax = sentence_plot(iv, words, connected_words, show=False, chars_per_line=100)
>>> plt.show()
../_images/sentence_plot_connected_example.png