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.2.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.2-py3-none-any.whl (113.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: shapg-0.14.2.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.2.tar.gz
Algorithm Hash digest
SHA256 6c55d71db8e5c41e5bfe6a43ac0f5a42bdbeb64d52328a216ecfdabd09c136bd
MD5 ae9ee00d77fe00b8a69408cafb4ae8e9
BLAKE2b-256 830f23b263ddfa513640f3d3aa65df3df176e07f60e5fb28ff823b5b115b2bc2

See more details on using hashes here.

Provenance

The following attestation bundles were made for shapg-0.14.2.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.2-py3-none-any.whl.

File metadata

  • Download URL: shapg-0.14.2-py3-none-any.whl
  • Upload date:
  • Size: 113.4 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 dc02cd40131dd456d07755dc6c8278f7dd4fbbe6a0e0d7d388df7cc4066f6385
MD5 2533983451984aecc79bbc404db1a925
BLAKE2b-256 630e43f7caca3b6f407edb93aaa2cd29e9e9990cbe76fb9c17b6ee54bf5ae494

See more details on using hashes here.

Provenance

The following attestation bundles were made for shapg-0.14.2-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