Skip to main content

A library to compute Shapley values in graphs.

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

import networkx as nx
from shapG.shapley import shapG, graph_generator
from shapG.plot import plot

# Generate a random graph
G = graph_generator(n_nodes=10, density=0.5)

# Compute approximate Shapley values
shapley_values = shapG(G, depth=1, m=15)

# Visualize the results
plot(shapley_values, top_n=10)

Advanced Usage

Custom Characteristic Function

You can define a custom characteristic function:

def my_coalition_function(G, S):
    # Your custom characteristic function
    subgraph = G.subgraph(S)
    return nx.density(subgraph) * len(S)

# Use your custom function
shapley_values = shapG(G, f=my_coalition_function)

More example, see ./examples.

Customizing the Plot

fig, ax = plot(
    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: bool = False      # Show plot
)

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

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.13.3.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

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

shapg-0.13.3-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: shapg-0.13.3.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for shapg-0.13.3.tar.gz
Algorithm Hash digest
SHA256 9616240f484fc6f72a4a01acbcb71e73184340b61bf805c9de3788c2ac4ada6c
MD5 da285e2f62c83505b4925da7bc276117
BLAKE2b-256 8cfe688b700da38f29b7024918e129bc0ee99e4c78dc4e971a7a39513c88eafb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: shapg-0.13.3-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for shapg-0.13.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b091b9b939986caead8364615d89ad47f55d2e18aaffc6502e5ca2c9f57431e3
MD5 34b3fa6a0e70a0dfb76cb22f4b3a3055
BLAKE2b-256 e56c41155412dbea9fe6e62aa5b53df9b06be25192b87dcbab17657b0fdb3446

See more details on using hashes here.

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