Skip to main content

Generate and visualize narrative sequences of counterfactual explanations

Project description

CounterfactualStoryboard

A Python library for generating and visualizing narrative sequences of counterfactual explanations.

CounterfactualStoryboard calculates the Shortest Path, Cheapest Path, and Most Actionable Path to reach a desired outcome, generating plain-language explanations and a beautiful decision storyboard.

Features

  • Abstract Engine: Built on dice-ml, but extensible to other counterfactual generation algorithms.
  • Narrative Generation: Uses Jinja2 templates to turn numeric feature changes into human-readable sentences.
  • Dual Visualization: Outputs interactive HTML/Plotly dashboards or static Matplotlib figures.
  • Model Agnostic: Works with scikit-learn, TensorFlow, PyTorch, or any custom model via a simple predict_fn.

Installation

pip install counterfactual_storyboard

Quickstart

import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from counterfactual_storyboard import StoryboardExplainer, FeatureMetadata

# 1. Train your model
df = pd.read_csv("data.csv")
X = df.drop(columns=["target"])
y = df["target"]
model = RandomForestClassifier().fit(X, y)

# 2. Define Feature Metadata (Costs and Mutability)
metadata = FeatureMetadata(
    continuous_features=["age", "balance", "credit_score"],
    actionable_features=["balance"], 
    feature_costs={"age": 100, "balance": 1, "credit_score": 10}
)

# 3. Initialize Explainer
explainer = StoryboardExplainer(
    predict_fn=model.predict_proba,
    training_data=df,
    target_column="target",
    feature_metadata=metadata
)

# 4. Generate Storyboard for an instance
instance = X.iloc[0:1]
storyboard = explainer.generate_storyboard(instance, target_class=1)

# 5. Visualize
storyboard.show()

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

counterfactual_storyboard-0.1.0.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

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

counterfactual_storyboard-0.1.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file counterfactual_storyboard-0.1.0.tar.gz.

File metadata

File hashes

Hashes for counterfactual_storyboard-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c60de3bf8aa6496fec5e4301fa6498aac4f7ae5be8b115ce143b29c36624659d
MD5 84d8a61cdc73ce7f9843e55eaab1b267
BLAKE2b-256 88b5c12d541fad3f26e6b7e618ef6c7f7f7e9501fd91ffebc32027a46d4d8d51

See more details on using hashes here.

File details

Details for the file counterfactual_storyboard-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for counterfactual_storyboard-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2b7c9e265905e57521d13248eba4e758c293c465a4b5042a69ccf44c0cd525ac
MD5 f4c016eb14b15b5d593707dcca185147
BLAKE2b-256 2997f8bdb4184a9f3b3970bcf1b8cfecbf619c60b7244aaf984275d25f0a53d7

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