Skip to main content

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.

Metadata

Release files for fast-causal-shap 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fast-causal-shap 0.3.0
File Size Uploaded Provenance Actions
fast_causal_shap-0.3.0.tar.gz 12.0 kB With provenance Details for fast_causal_shap-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fast-causal-shap 0.3.0
File Interpreter ABI Platform Actions
fast_causal_shap-0.3.0-py3-none-any.whl Python 3 none any Details for fast_causal_shap-0.3.0-py3-none-any.whl

Total release size: 21.3 kB

Release files / fast_causal_shap-0.3.0.tar.gz

Download URL fast_causal_shap-0.3.0.tar.gz
Size 12.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a4b8d515a2ce6513da2f80b4e575dea9cbcd6d68aa25ee6dfa67bedf83da3d05
BLAKE2b-256 checksum
How to use checksums
952b1f6b54490e89bee58da3b436beaa27f18ceb750691499fec470342cbf573
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 19, 2026.

Transparency log

Release files / fast_causal_shap-0.3.0-py3-none-any.whl

Download URL fast_causal_shap-0.3.0-py3-none-any.whl
Size 9.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
02b20e8f82c9af865ccdb8de9b695ec4dbc1309c3b5822a0008065904b183383
BLAKE2b-256 checksum
How to use checksums
983cdef3174ad170c6c67514413a2f253cad7e2726812c448dbc6f6554638e78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 19, 2026.

Transparency log

Security for fast-causal-shap 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for fast_causal_shap-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 19, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page