Skip to main content

Feature Prototype Direction Explainer for prototype-based feature attribution

Project description

Feature Prototype Direction Explainer (FPDE)

PyPI version Python versions License

Feature Prototype Direction Explainer (FPDE) is a Python package for prototype-contrast feature attribution. It explains a classification result by comparing an input with a prototype for the target class and a prototype for a rival class, then decomposing that contrast into per-feature contributions.

Use FPDE when you want a lightweight, post-hoc explanation method for tabular-style feature vectors and black-box classifiers that expose class probabilities.

What You Can Do

  • Build class-mean prototypes from training data.
  • Explain one sample with Diff-FPDE, Cos-FPDE, or a fixed Hyb-FPDE mixture.
  • Explain batches while reusing fitted prototype state.
  • Search Diff, Cos, and Hyb-FPDE candidate settings.
  • Select lambda_hyb with held-out deletion and insertion validation.
  • Compute deletion and insertion perturbation curves for an attribution vector.

Install FPDE

FPDE requires Python 3.12 or newer.

python -m pip install fpde

For local development, clone the repository and install it in editable mode:

python -m pip install -e .

The PyPI distribution name and Python import package are both fpde.

Quick Start

This example trains a scikit-learn classifier, fits an FPDE engine on the training data, and explains one test sample.

import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

from fpde import FPDEEngine

data = load_breast_cancer()
X_train, X_test, y_train, _ = train_test_split(
    data.data,
    data.target,
    test_size=0.25,
    random_state=7,
    stratify=data.target,
)

scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

model = LogisticRegression(max_iter=2000, random_state=7)
model.fit(X_train, y_train)

engine = FPDEEngine.fit(X_train, y_train, model=model)
attributions, details = engine.explain_one(X_test[0], lambda_hyb=0.5)

print(np.asarray(attributions))
print(details["target_label"], details["rival_label"], details["evidence"])

Positive attribution values support the target class relative to the rival class. Negative values support the rival class relative to the target class.

Run The Example

python examples/minimal_fpde_example.py

The script prints the predicted class and the largest positive and negative feature contributions for one sample from the breast cancer dataset bundled with scikit-learn.

Documentation

Test The Package

Install the development dependencies, then run the test suite:

python -m pip install -e .
python -m pytest

You can also run the repository in Docker:

docker build -t fpde .
docker run --rm fpde

The Docker command runs the test suite and the minimal example.

Cite FPDE

If you use FPDE in academic work, cite the software or method as appropriate. Citation metadata is available in CITATION.cff.

License

FPDE is distributed under a dual license: MIT OR Apache-2.0. You may choose either license. See LICENSE, LICENSE-MIT, and LICENSE-APACHE.

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

fpde-0.1.0.tar.gz (37.2 kB view details)

Uploaded Source

Built Distribution

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

fpde-0.1.0-py3-none-any.whl (36.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fpde-0.1.0.tar.gz
  • Upload date:
  • Size: 37.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.17 {"installer":{"name":"uv","version":"0.11.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fpde-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4c0a2258c4af82899662712abe55652cdf708e85168ffa76104273e0c2254f08
MD5 8d142a358059453ff29ed34b07f618a6
BLAKE2b-256 f1bab8fca2029b1f67701a3942507ad39dc27dfb370e9e6b0c8afd3f32392327

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fpde-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 36.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.17 {"installer":{"name":"uv","version":"0.11.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fpde-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5973e0bb5a2fabcb8a090fddb1a43338c6546db457c28cc954de7aeccb66d8e7
MD5 627bbd7d824d8e5183b4ed6bbc3c7846
BLAKE2b-256 b3a2afadddfb14b5be127f85e9f2a954eb4884d74b09a307ab2aed1030b05125

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