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

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.2.0.tar.gz (11.9 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.2.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fast_causal_shap-0.2.0.tar.gz
  • Upload date:
  • Size: 11.9 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.2.0.tar.gz
Algorithm Hash digest
SHA256 2ab4ae49ff186b7344957bb47cc4c98fac5bc67ab1aa28766db313bcffd5aea2
MD5 d00efd21b25383fbfe30ec5ddb29bb20
BLAKE2b-256 6e00c8fe145f0b53da3fd20e0727575e6942dece78fb2f8ab606c8ec3e9aba15

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_causal_shap-0.2.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.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fast_causal_shap-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bbfff18c5a4f4dbbeaba6504902e0321764062cee05e6bb3fd1a05f7ab2d0884
MD5 b27fecb60b5cd140145225a2240ff27b
BLAKE2b-256 a97ff13df6a7e9578a3ad284b2dcfea14dac05802675fdf4f5927fa159064e19

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_causal_shap-0.2.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