Skip to main content

A Python package for efficient causal SHAP computations

Project description

Fast Causal SHAP

PyPI version Docs   Python 3.9+ License: MIT Tests

Fast Causal SHAP is a Python package designed for efficient and interpretable SHAP value computation in causal inference tasks. It integrates seamlessly with various causal inference frameworks and enables feature attribution with awareness of causal dependencies.

Features

  • Fast computation of SHAP values for causal models
  • Support for multiple causal inference frameworks

Installation

Install the stable version via PyPI:

pip install fast-causal-shap

For Development

  Clone and install in editable mode with development dependencies:
  git clone https://github.com/woonyee28/CausalSHAP.git
  cd CausalSHAP
  pip install -e ".[dev]"
  pre-commit install

Quick Start

import json
import pandas as pd
from sklearn.ensemble import RandomForestRegressor
from fast_causal_shap import FastCausalSHAP

# Load your data
data = pd.DataFrame({
    'X1': [1, 2, 3, 4, 5],
    'X2': [2, 4, 6, 8, 10],
    'Y': [3, 6, 9, 12, 15]
})

# Train a model
model = RandomForestRegressor()
X = data[['X1', 'X2']]
y = data['Y']
model.fit(X, y)

# Causal effects JSON file
# This defines the causal relationships: X1 -> Y and X2 -> Y
causal_effects = [
    {"Pair": "X1->Y", "Mean_Causal_Effect": 0.8},
    {"Pair": "X2->Y", "Mean_Causal_Effect": 0.5}
]
with open('causal_effects.json', 'w') as f:
    json.dump(causal_effects, f)

# Initialize FastCausalSHAP
shap_explainer = FastCausalSHAP(data, model, target_variable='Y')

# Load causal graph
shap_explainer.load_causal_strengths('causal_effects.json')

# Compute SHAP values for a single instance
shap_values = shap_explainer.compute_modified_shap_proba(data.iloc[0])
print(shap_values)

Working with Different Causal Inference Algorithms

Fast Causal SHAP supports integration with structural algorithms such as:

  1. Peter-Clarke (PC) Algorithm
  2. IDA Algorithm
  3. Fast Causal Inference (FCI) Algorithm You can find example R code for these integrations here: FastCausalSHAP R code examples

Generate your causal graph using your preferred algorithm, then export to JSON format.

Format of the Causal_Effect.json:

[
  {
    "Pair": "Bacteroidia->Clostridia",
    "Mean_Causal_Effect": 0.71292
  },
  {
    "Pair": "Clostridia->Alphaproteobacteria",
    "Mean_Causal_Effect": 0.37652
  }, ......
]

Development and Contributions

Setup Development Environment

  # Clone repository
  git clone https://github.com/woonyee28/CausalSHAP.git
  cd CausalSHAP

  # Install with dev dependencies
  pip install -e ".[dev]"

  # Install pre-commit hooks
  pre-commit install

  # Running Tests
  pre-commit run --all-files

  # Run all tests
  pytest

  # Run with coverage
  pytest --cov=fast_causal_shap --cov-report=html

  # View coverage report
  open htmlcov/index.html

Code Quality

This project uses automated code quality tools:

  • Black: Code formatting
  • isort: Import sorting
  • Flake8: Linting
  • mypy: Type checking

Troubleshooting / FAQ

Q: I get "Must call load_causal_strengths() before computing SHAP values"

  • A: You need to load a causal graph before computing SHAP values. Call load_causal_strengths() first.

Q: "model must have 'feature_names_in_' attribute"

  • A: Ensure your model has been fitted before passing it to FastCausalSHAP.

Q: JSON file validation errors

  • A: Check that your JSON file follows the correct format (see Causal Graph Format section).

Contributing

Contributions are welcome through pull request!

Citation

If you use Fast Causal SHAP in your research, please cite:

@inproceedings{ng2025causal,
  title={Causal SHAP: Feature Attribution with Dependency Awareness through Causal Discovery},
  author={Ng, Woon Yee and Wang, Li Rong and Liu, Siyuan and Fan, Xiuyi},
  booktitle={Proceedings of the International Joint Conference on Neural Networks (IJCNN)},
  year={2025},
  organization={IEEE}
}

License

This project is licensed under the MIT License.

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

fast_causal_shap-0.3.0.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

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

fast_causal_shap-0.3.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file fast_causal_shap-0.3.0.tar.gz.

File metadata

  • Download URL: fast_causal_shap-0.3.0.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fast_causal_shap-0.3.0.tar.gz
Algorithm Hash digest
SHA256 a4b8d515a2ce6513da2f80b4e575dea9cbcd6d68aa25ee6dfa67bedf83da3d05
MD5 7ff575490a404d71406cb85c9b5a5af2
BLAKE2b-256 952b1f6b54490e89bee58da3b436beaa27f18ceb750691499fec470342cbf573

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_causal_shap-0.3.0.tar.gz:

Publisher: python-publish.yml on woonyee28/FastCausalSHAP

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

File details

Details for the file fast_causal_shap-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fast_causal_shap-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 02b20e8f82c9af865ccdb8de9b695ec4dbc1309c3b5822a0008065904b183383
MD5 77cb8e601b671ba798512bca797cd2d1
BLAKE2b-256 983cdef3174ad170c6c67514413a2f253cad7e2726812c448dbc6f6554638e78

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_causal_shap-0.3.0-py3-none-any.whl:

Publisher: python-publish.yml on woonyee28/FastCausalSHAP

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