Skip to main content

PACE

Proposal Assembly for Counterfactual Explanations — a counterfactual explainer for tabular classifiers.

PACE generates counterfactual explanations — the smallest change to a data point that flips the model's prediction. It works with any binary classifier that exposes a predict_proba method, handles mixed numeric/categorical features natively, and supports actionability constraints.

Install

pip install pacex

Quick start

from sklearn.datasets import fetch_openml
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from pace import PACE, FeatureInfo
from pace.preprocessing import make_preprocessor, feature_info_from_preprocessor

# 1. Load the German credit dataset (1 000 rows, mixed features)
data = fetch_openml("credit-g", version=1, as_frame=True)
df = data.frame
y = (df["class"] == "good").astype(int)

num_cols = ["age", "duration", "credit_amount"]
cat_cols = ["checking_status", "credit_history", "purpose", "personal_status"]

X_train_df, X_test_df, y_train, y_test = train_test_split(
    df[num_cols + cat_cols], y, test_size=0.2, random_state=42
)

# 2. Preprocess
pre = make_preprocessor(num_cols, cat_cols)
X_tr_sc = pre.fit_transform(X_train_df)   # numpy array, model space
X_te_sc = pre.transform(X_test_df)
feature_info = feature_info_from_preprocessor(pre, num_cols, cat_cols)

# 3. Train your model (any sklearn-compatible classifier)
model = RandomForestClassifier(random_state=42).fit(X_tr_sc, y_train)

# 4. Fit PACE once per (dataset, model) pair
explainer = PACE(
    X_train=X_tr_sc,
    predict_proba=model.predict_proba,
    feature_info=feature_info,
    pre=pre,
    num_cols=num_cols,
    cat_cols=cat_cols,
)

# 5. Explain a single instance — with constraints and immutability
x_f = X_te_sc[0]
x_cf, report = explainer.explain(
    x_f,
    y_desired=1,                           # flip to "good credit"
    n_candidates=1200,                     # more candidates → better quality
    max_changed_features=2,               # change at most 2 features
    immutable=["age", "personal_status"], # these must not change
    constraints={
        "duration":      (None, 36),       # loan term ≤ 36 months
        "credit_amount": (0,    None),     # amount must stay non-negative
    },
)

print(report)
# {'status': 'ok', 'l0': 2, 'l2': 0.74, 'p_cf': 0.68, ...}

# 6. Decode back to human-readable values
print(explainer.decode(x_cf))
# {'age': 34.0, 'duration': 24.0, 'credit_amount': 2500.0, ...}

Constraints are specified in the original feature space (before scaling).

Purely numeric data

If your data has no categorical columns, skip feature_info:

explainer = PACE(X_train=X_tr_sc, predict_proba=model.predict_proba)
x_cf, report = explainer.explain(x_f, y_desired=1)

License

Apache 2.0

Metadata

Release files for pacex 0.1.2

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

Source distribution (sdist)

Source distribution for pacex 0.1.2
File Size Uploaded Provenance Actions
pacex-0.1.2.tar.gz 18.9 kB Without provenance Details for pacex-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pacex 0.1.2
File Interpreter ABI Platform Actions
pacex-0.1.2-py3-none-any.whl Python 3 none any Details for pacex-0.1.2-py3-none-any.whl

Total release size: 37.2 kB

Release files / pacex-0.1.2.tar.gz

Download URL pacex-0.1.2.tar.gz
Size 18.9 kB
Tags Source
SHA-256 checksum
How to use checksums
0c517e947ccc15af18021566e85c03cf98198570e2f777c7d50cb035e2182ed1
BLAKE2b-256 checksum
How to use checksums
05a00aed95a27de84cb8231d3bfa6ad71306334e718ebbc0e835b57dd582cbaa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.5

Release files / pacex-0.1.2-py3-none-any.whl

Download URL pacex-0.1.2-py3-none-any.whl
Size 18.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6d529c3b61a0c1f490f428645f6adec020ee0dc8435d9a56fcb8ac733e39548e
BLAKE2b-256 checksum
How to use checksums
8f90af70df7b9b924982d9ccd9eeac4d33a053201514550e4348beeb87acf66b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.5

Security for pacex 0.1.2

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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