Skip to main content
Import the public package as fpde.
This page documents the public API exported by fpde and fpde.core.

FPDEEngine

Use FPDEEngine for repeated explanations, batch explanations, Hyb-FPDE, grid search, and validation-based lambda selection.

FPDEEngine.fit

Fits reusable FPDE state from training data. Returns an FPDEEngine.

engine.explain_one

Explains one sample with fixed-lambda Hyb-FPDE. Returns (attributions, details). The details dictionary includes target_label, rival_label, target_probability, lambda_hyb, evidence, exactness_residual, positive_score, and negative_score.

engine.explain_batch

Explains many samples with fixed-lambda Hyb-FPDE. Returns (attribution_matrix, details). If include_details=False, details is an empty list.

engine.explain_matrix

Returns only the attribution matrix for a batch.
Searches Diff-FPDE, Cos-FPDE, and Hyb-FPDE candidate settings. Returns a HybFPDEGridSearchResult.

engine.select_lambda

Selects lambda_hyb by held-out deletion and insertion validation. Returns a HybFPDEValidationSelectionResult.

Prototype helpers

class_mean_prototypes

Builds one mean prototype per class. Returns (prototypes, labels).

select_prototype_pair

Selects the positive and negative prototype indices for a local contrast. Returns (positive_index, negative_index).

prepare_fpde_context

Precomputes reusable prototypes, anchors, baseline, and feature metadata. Returns an FPDEContext.

Explanation functions

Use these functions when you want direct control over prototypes and labels.

diff_fpde

Computes a Diff-FPDE explanation for one target/rival prototype pair.

cos_fpde

Computes a Cos-FPDE explanation for one target/rival prototype pair.

explain_with_selected_prototypes

Selects prototypes and computes a public Diff-FPDE or Cos-FPDE explanation. Use FPDEEngine for Hyb-FPDE.

Metrics and probability helpers

regularized_cosine

Returns cosine similarity with epsilon-regularized norms.

top_two_labels

Returns (target_label, rival_label, probability_vector) for one sample. model must implement predict_proba and expose classes_.

predict_proba_for_label

Returns the predict_proba(X) column for label.

perturbation_curves

Computes deletion and insertion curves for one attribution vector. Features are ranked by signed positive attribution in descending order.

Plotting helpers

The plotting helpers are exported from fpde and visualize FPDE attributions, prototype similarities, and perturbation curves with matplotlib. They require the optional plot extra:
Each function returns the matplotlib Axes it draws on and does not call plt.show() or save files. Pass an existing ax= to draw into a subplot, or omit it to create a new figure.

plot_attributions

Plots a signed horizontal bar chart for a 1D attribution vector or an FPDEExplanation. Positive values support the target class; negative values support the rival class. Use top_k to limit the number of features and normalize=True to L1-normalize the displayed values.

plot_attribution_waterfall

Plots a cumulative local explanation from base_value to base_value + sum(attributions). Hidden features (outside top_k) are grouped into one other features bar.

plot_attribution_summary

Plots a batch attribution matrix as a per-feature distribution, ordered by mean absolute attribution. Pass feature_values with the same shape as attributions to color points by the original feature value.

plot_attribution_image

Plots an attribution vector as a 2D heatmap. Pass shape=(height, width) for flat image vectors; shape is optional for already-2D arrays.

plot_perturbation_curves

Plots the deletion and insertion probability curves returned by perturbation_curves. curves must contain fractions, deletion_prob, and insertion_prob.

plot_local_contributions

Plots a signed local contribution bar chart from feature names and one attribution vector. Pass values to label each bar with the corresponding feature value.

prepare_local_contribution_data

Returns a list of row dictionaries (feature, display_name, contribution, abs_contribution, direction, direction_label) suitable for rendering FPDE local attributions in a custom UI.

plot_prototype_similarity_distribution

Plots the distribution of similarities from rows in X to the target prototype. Pass rival_prototype to overlay the rival distribution and x to mark the explained sample. metric is "cosine" or "negative_euclidean".

fpde.plots namespace

fpde.plots provides a compact matplotlib API for FPDE contribution arrays inspired by SHAP’s plotting layout. Pass show=False to receive an Axes without displaying, then save with ax.figure.savefig(...).

Result objects

Common errors