shapiq: Shapley Interaction Quantification

repository·main·Indexed 20 days ago

https://github.com/mmschlk/shapiq

A Python package for approximating any-order Shapley interactions to explain feature interactions in machine learning models and benchmark game-theoretic algorithms. It extends standard Shapley values by quantifying synergy effects between features, data points, or weak learners. Key features include TabularExplainer for k-SII indices, ProxySPEX for large-scale interactions, TabPFNExplainer for TabPFN models, and various visualization tools such as network, force, and beeswarm plots.

Tokens
8.4K
Snippets
30
Records
40
Agent score
73%

What's inside shapiq

  1. Overview of shapiq: Shapley Interaction Quantification

    main

    shapiq is a Python package designed for approximating any-order Shapley interactions, benchmarking game-theoretical algorithms for machine learning, and explaining feature interactions in model predictions.

    It extends the shap package by quantifying the synergy effect between entities (referred to as players in game theory), such as explanatory features, data points, or weak learners in ensemble models. This provides a more comprehensive view of machine learning models compared to standard individual Shapley values.

  2. Explore shapiq visualization capabilities

    main

    shapiq provides several built-in visualization tools to interpret Shapley Interaction (SI) values and model behavior. Available visualization types include:

    • Force plots: To visualize how different features push the prediction towards or away from a base value.
    • Beeswarm plots: To show the distribution of interaction values across a dataset.
    • SI graph plots: To visualize Shapley Interactions as a graph structure.
    • UpSet plots: To visualize intersections and overlaps in feature interactions.

    Refer to the specific example files in the examples/visualization/ directory to see implementation details for each plot type.

  3. Understand the core capabilities of shapiq

    main

    shapiq is a library designed for explaining machine learning models using Shapley values and Shapley interactions. It provides several key advantages over standard SHAP implementations:

    • Shapley Interactions: Beyond standard Shapley values, shapiq enables the computation of any-order feature interactions, allowing for more detailed model explanations.
    • Unified Interface for Game Theory and ML: shapiq treats machine learning problems as cooperative games. It provides a unified interface that allows you to switch between an explanation perspective (transforming ML models into games) and a game theory perspective (computing concepts like Shapley values, Shapley interactions, or the Banzhaf value on a general game object).
    • High-Dimensional Support: Many algorithms available in shap are implemented in shapiq, often optimized for cases involving a higher number of features.
    • Benchmarking Platform: The library serves as a platform to benchmark state-of-the-art algorithms for Shapley values and interactions, providing tools to evaluate performance on pre-computed benchmark tasks.
  4. Use the ConfoundingXAI game for confounding-bias attribution

    main
    The ConfoundingXAI game is used for confounding-bias attribution in Causal XAI. It utilizes Shapley values to identify which covariates are responsible for the gap between an estimated treatment effect and the observed outcome difference. This allows developers to attribute confounding bias to specific features in a causal model.
  5. What are Shapley Interactions and how do they differ from Shapley values?

    main

    Traditional Shapley values distribute a model's prediction influence among individual features. However, they merge individual effects and interaction effects into a single number, making it impossible to distinguish if a feature's influence is independent or dependent on another feature.

    Shapley interactions solve this by decomposing the prediction's influence into:

    1. Individual contributions: The effect of a feature acting alone.
    2. Interactions: The effect of combinations of features (e.g., how the combination of longitude and latitude identifies a specific location).

    By setting a maximum interaction order, you can control the granularity of the analysis. For example, a second-order decomposition allows you to see how pairs of features interact, whereas traditional Shapley values only provide first-order (individual) effects.

  6. Install development dependencies for shapiq

    main

    If you are contributing to shapiq and need additional packages for documentation or testing, use the optional installation extras:

    • shapiq[docs]: Installs dependencies required for building documentation.
    • shapiq[dev]: Installs all development dependencies, including those for documentation.
    pip install shapiq[docs]
    pip install shapiq[dev]
  7. Explain image classification models using shapiq

    main
    The vision examples in shapiq demonstrate how to explain image classification models by treating image patches as players in a cooperative game. This approach allows for computing Shapley Interaction Indices (SII) to understand how different spatial regions of an image contribute to or interact in a model's prediction.
  8. Define a custom game by inheriting from Game

    main

    The Game class is the base class for all cooperative games in shapiq. To use it with your own data or model, you must create a subclass and implement the value_function method. The value_function defines the worth of a coalition of players and is the core logic of your game.

    When initializing your subclass, you can specify n_players, whether to normalize values (centering them so the empty coalition is zero), and an optional list of player_names to allow using string identifiers for coalitions.

    from shapiq.game import Game
    import numpy as np
    
    class MyCustomGame(Game):
        def value_function(self, coalitions: np.ndarray) -> np.ndarray:
            # Implement your logic here
            # coalitions is a one-hot encoded matrix
            return np.sum(coalitions, axis=1)
    
    # Usage
    game = MyCustomGame(n_players=3)
  9. Data structures for coalitions and interactions

    main

    The following types define how players and their groupings are represented in shapiq:

    • CoalitionTuple: A sorted tuple[int, ...] of player indices.
    • InteractionTuple: A sorted tuple[int, ...] of player indices representing an interaction.
    • CoalitionsTuples: A Collection[CoalitionTuple] representing a set of coalitions.
    • CoalitionMatrix: A 2D NDArray[np.bool_] of shape (n_coalitions, n_players) where 1 indicates player presence.
    • InteractionScores: A dict[InteractionTuple, float] mapping interactions to their scores.
    • GameScores: A dict[CoalitionTuple, float] mapping coalitions to their game values.