Skip to main content

PACE: Proposal Assembly for Counterfactual Explanations

Project description

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

import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from pace import PACE, FeatureInfo
from pace.preprocessing import make_preprocessor, feature_info_from_preprocessor

# 1. Prepare data
num_cols = ["age", "hours_per_week", "capital_gain"]
cat_cols = ["workclass", "education", "occupation"]

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)

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

# 3. 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,
)

# 4. Explain a single instance
x_f = X_te_sc[0]
x_cf, report = explainer.explain(x_f, y_desired=1)

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

# 5. Decode back to human-readable values
print(explainer.decode(x_cf))
# {'age': 34.0, 'hours_per_week': 45.0, 'workclass': 'Private', ...}

Constraints and immutability

x_cf, report = explainer.explain(
    x_f,
    y_desired=1,
    n_candidates=1200,          # more candidates → better quality
    max_changed_features=3,     # change at most 3 features
    immutable=["age", "sex"],   # these must not change
    constraints={
        "hours_per_week": (None, 60),   # hours ≤ 60
        "capital_gain":   (0, None),    # capital_gain ≥ 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

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

pacex-0.1.0.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

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

pacex-0.1.0-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pacex-0.1.0.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.13

File hashes

Hashes for pacex-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c0ade4c920f8fcad92b905b05aba837e7a6fbb8f9fac44ee6cdbf6751a6c3907
MD5 6f1a794ef484bcee419694be40301c33
BLAKE2b-256 e89dbf8e7d3dab776df63d6a400a399104bd6d187484f948e8fb207dafe5d55d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pacex-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.13

File hashes

Hashes for pacex-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4b3cedf96360d3125700ec39281bdb296feab77350985e5ff7093c7b7ed66bbf
MD5 5727eb3b24bddc60f823412043187bd0
BLAKE2b-256 4308120a1995cc756d81d4f8626ccd234c7910c8a5aeb46f5e94d4e6d51d4952

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