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:
- 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}, } - 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6c55d71db8e5c41e5bfe6a43ac0f5a42bdbeb64d52328a216ecfdabd09c136bd
|
|
| MD5 |
ae9ee00d77fe00b8a69408cafb4ae8e9
|
|
| BLAKE2b-256 |
830f23b263ddfa513640f3d3aa65df3df176e07f60e5fb28ff823b5b115b2bc2
|
Provenance
The following attestation bundles were made for shapg-0.14.2.tar.gz:
Publisher:
publish.yml on vectorsss/shapG
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
shapg-0.14.2.tar.gz -
Subject digest:
6c55d71db8e5c41e5bfe6a43ac0f5a42bdbeb64d52328a216ecfdabd09c136bd - Sigstore transparency entry: 1924260768
- Sigstore integration time:
-
Permalink:
vectorsss/shapG@0228dd3e38fde7d9d6b11d54007b73611da78916 -
Branch / Tag:
refs/tags/v0.14.2 - Owner: https://github.com/vectorsss
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@0228dd3e38fde7d9d6b11d54007b73611da78916 -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dc02cd40131dd456d07755dc6c8278f7dd4fbbe6a0e0d7d388df7cc4066f6385
|
|
| MD5 |
2533983451984aecc79bbc404db1a925
|
|
| BLAKE2b-256 |
630e43f7caca3b6f407edb93aaa2cd29e9e9990cbe76fb9c17b6ee54bf5ae494
|
Provenance
The following attestation bundles were made for shapg-0.14.2-py3-none-any.whl:
Publisher:
publish.yml on vectorsss/shapG
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
shapg-0.14.2-py3-none-any.whl -
Subject digest:
dc02cd40131dd456d07755dc6c8278f7dd4fbbe6a0e0d7d388df7cc4066f6385 - Sigstore transparency entry: 1924260880
- Sigstore integration time:
-
Permalink:
vectorsss/shapG@0228dd3e38fde7d9d6b11d54007b73611da78916 -
Branch / Tag:
refs/tags/v0.14.2 - Owner: https://github.com/vectorsss
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@0228dd3e38fde7d9d6b11d54007b73611da78916 -
Trigger Event:
push
-
Statement type: