ACFX (Actionable Counterfactual eXplainer) is a model-agnostic Explainable AI (XAI) framework for generating actionable counterfactual explanations for machine learning models.
It answers the question:
What minimal and feasible changes to the input would lead to a desired prediction outcome?
- Model-agnostic counterfactual explanations
- Actionability and feasibility constraints
- Support for causal structures and expert knowledge
- Built-in benchmarking framework
- Python API and graphical user interface (GUI)
conda create --name acfx_env python=3.11
conda activate acfx_env
conda install pip
pip install acfxgit clone https://github.com/sbobek/acfx
cd acfx
pip install .Note: The requirements.txt file includes additional packages that are not required for standard use. It is provided for development purposes and to support reproducibility of the complete development environment. Direct runtime dependencies are specified in pyproject.toml.
from acfx import AcfxEBM
from interpret.glassbox import ExplainableBoostingClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import numpy as np
# Initialize model and explainer
model = ExplainableBoostingClassifier()
explainer = AcfxEBM(model)
# Load sample data
data = load_iris(as_frame=True)
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
# Define feature bounds
pbounds = {col: (X_train[col].min(), X_train[col].max()) for col in X_train.columns}
# Example adjacency matrix and causal order
adjacency_matrix = np.array([
[0.0, 0.0, 0.0, 0.0],
[0.8, 0.0, 0.0, 0.0],
[0.0, 0.6, 0.0, 0.0],
[0.5, 0.0, 0.7, 0.0]
])
causal_order = [0, 1, 2, 3]
# Fit explainer
explainer.fit(
X=X_train,
adjacency_matrix=adjacency_matrix,
causal_order=causal_order,
pbounds=pbounds,
y=y_train,
features_order=X_train.columns.tolist()
)
# Generate counterfactual
query_instance = X_test.iloc[0].values
original_class = model.predict([query_instance])[0]
desired_class = next(cls for cls in model.classes_ if cls != original_class)
counterfactuals = explainer.counterfactual(desired_class=desired_class, query_instance=query_instance)
# Step 9: Inspect the results
print(f"Original instance: {query_instance}")
print(f"\nGenerated counterfactuals: {counterfactuals}")
⚠️ The adjacency matrix above is a simple example. In practice, it can be provided by expert knowledge or learned using tools like DirectLiNGAM.
Learn more and explore advanced examples:
- Documentation: ACFX Docs
- Python API (Colab Notebooks): Interactive Examples
- Graphical User Interface (GUI): ACFX GUI
The library and the Streamlit GUI are covered by an automated pytest suite with code coverage measurement:
git clone https://github.com/sbobek/acfx
cd acfx
pip install -e ".[test,streamlit-app]"
pytest --covSee TESTING.md for details (fast mode, HTML coverage report, what is excluded from coverage and why).
pip install acfx[benchmark]git clone https://github.com/sbobek/acfx
cd acfx/src
python -m acfx.benchmark.main
⚠️ Always run as a module fromacfx/srcusingpython -m. Running directly or from another folder may cause import or path errors.
TBD