Skip to main content

Scalable Shapley Value Computation for Graph Data with Modular Architecture

Project description

ShapG: a fast and exactly approach to approximate the Shaple value on graph

Installation

pip install shapG

Features

  • Exact Shapley value computation for small to medium-sized graphs
  • Fast approximate Shapley value computation for large graphs using local search
  • Visualization tools for Shapley values
  • Utility functions for graph generation and analysis

Quick Start (New Modular API)

import networkx as nx
from shapG import ShapGExplainer, GraphBuilder, FeatureImportanceVisualizer

# Build a graph from tabular data
builder = GraphBuilder()
G = builder.from_correlation(data, threshold=0.3)

# Compute approximate Shapley values
explainer = ShapGExplainer(depth=1, n_samples=15)
shapley_values = explainer.fit_explain(G)

# Visualize the results
viz = FeatureImportanceVisualizer()
viz.plot_importance(shapley_values)

Advanced Usage

Custom Characteristic Function

You can define a custom characteristic function:

from shapG import ShapGExplainer, CustomFunction

def my_coalition_function(coalition, graph):
    subgraph = graph.subgraph(coalition)
    return nx.density(subgraph) * len(coalition)

custom_func = CustomFunction(my_coalition_function)
explainer = ShapGExplainer(characteristic_function=custom_func)
shapley_values = explainer.fit_explain(G)

Customizing the Plot

from shapG import FeatureImportanceVisualizer

viz = FeatureImportanceVisualizer()
fig, ax = viz.plot_importance(
    shapley_values,
    top_n=5,                    # Show only top 5 values
    style='seaborn-v0_8',       # Matplotlib style
    file_name="shapley.eps",    # Save to file
    title="Node Importance",    # Custom title
    figsize=(10, 6),            # Figure size (width, height)
    color="#2E86C1",            # Bar color
    show_values=True,           # Show values next to bars
    value_format="{:.4f}",      # Format for displayed values
    show_plot=False             # Show plot
)

# Further customize the plot using matplotlib objects
ax.set_xlabel("Contribution Score", fontsize=14)

Legacy API (Deprecated)

The legacy procedural APIs (shapG, shapley_value, graph_generator, plot) are deprecated and planned for removal in v0.15.0. Please migrate to the modular API (ShapGExplainer, ExactExplainer, GraphBuilder, FeatureImportanceVisualizer).

Performance Notes

Exact Shapley computation scales as O(2^n). For graphs with more than ~20 nodes, prefer approximate explainers such as ShapGExplainer, QRCSExplainer, or BlockQRCSExplainer.

License

MIT License

Citation

If you find this code useful in your research, please consider citing:

  1. For centralities measures:
    @article{zhao2024centralitymeasuresopiniondynamics,
          title={Centrality measures and opinion dynamics in two-layer networks with replica nodes},
          author={Chi Zhao and Elena Parilina},
          year={2024},
          eprint={2406.18780},
          archivePrefix={arXiv},
          primaryClass={physics.soc-ph},
          journal={arXiv preprint arXiv:2406.18780},
          url={https://arxiv.org/abs/2406.18780},
    }
    
  2. For the new method for explanable ai:
    @article{ZHAO2025110409,
          title = {ShapG: New feature importance method based on the Shapley value},
          journal = {Engineering Applications of Artificial Intelligence},
          volume = {148},
          pages = {110409},
          year = {2025},
          issn = {0952-1976},
          doi = {https://doi.org/10.1016/j.engappai.2025.110409},
          url = {https://www.sciencedirect.com/science/article/pii/S0952197625004099},
          author = {Chi Zhao and Jing Liu and Elena Parilina},
    }
    

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shapg-0.14.1.tar.gz (1.2 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

shapg-0.14.1-py3-none-any.whl (112.1 kB view details)

Uploaded Python 3

File details

Details for the file shapg-0.14.1.tar.gz.

File metadata

  • Download URL: shapg-0.14.1.tar.gz
  • Upload date:
  • Size: 1.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for shapg-0.14.1.tar.gz
Algorithm Hash digest
SHA256 6555043da04699421b93667156f3feeca241c0f8340bcdcc1fd1777b3a790c18
MD5 135ca9024aaccf89c599fda1d39e2392
BLAKE2b-256 dbd069855f43cfbec65fae1ae7b029b77eef1b084a4302fa643a97b29c064161

See more details on using hashes here.

Provenance

The following attestation bundles were made for shapg-0.14.1.tar.gz:

Publisher: publish.yml on vectorsss/shapG

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file shapg-0.14.1-py3-none-any.whl.

File metadata

  • Download URL: shapg-0.14.1-py3-none-any.whl
  • Upload date:
  • Size: 112.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for shapg-0.14.1-py3-none-any.whl
Algorithm Hash digest
SHA256 76168ea62a0bfe0b56bd5792bb6cfc64f42ea6c7b759c0195ebc24bedb50eb35
MD5 0c6ee952fcec389efddd4f09d18fd139
BLAKE2b-256 740dacace4a7d62232e2c885b42cbe1de1dc987ce373eeaeb7a0d72831a4b253

See more details on using hashes here.

Provenance

The following attestation bundles were made for shapg-0.14.1-py3-none-any.whl:

Publisher: publish.yml on vectorsss/shapG

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page