acfx.AcfxLinear

class acfx.AcfxLinear(blackbox: LinearClassifierMixin)

Bases: ACFX

AcfxLinear: A Counterfactual Explanation Model (using linear additive model as blackbox)

__init__(blackbox: LinearClassifierMixin)

Parameters

blackbox:

Linear blackbox explainer

counterfactual(query_instance: ndarray, desired_class: int, num_counterfactuals: int = 1, proximity_weight: float = 1, sparsity_weight: float = 1, plausibility_weight: float = 0, diversity_weight: float = 1, init_points: int = 10, n_iter: int = 1000, sampling_from_model: bool = True) ndarray

Generates counterfactuals

Parameters

query_instance:

The instance to generate counterfactuals for.

desired_class:

The target class for the counterfactuals.

num_counterfactuals:

The number of counterfactual instances to generate.

proximity_weight:

Weight for proximity loss component

sparsity_weight:

Weight for sparsity loss component

plausibility_weight:

Weight for plausibility loss component

diversity_weight:

Weight for diversity loss component

init_points:

Number of initial points for Bayesian Optimization.

n_iter:

Number of iterations for Bayesian Optimization.

sampling_from_model:

true if you want to generate samples from model after sampling from data

Returns

np.ndarray:

The generated counterfactuals that minimize the loss function.

fit(X: DataFrame, pbounds: Dict[str, Tuple[float, float]], causal_order: Sequence[int] | None = None, adjacency_matrix: ndarray | None = None, y=None, masked_features: List[str] | None = None, categorical_indicator: List[bool] | None = None, features_order: List[str] | None = None, bayesian_causality: bool = False, bayesian_model: DiscreteBayesianNetwork | None = None, num_bins: int | None = None) Self

Fits explainer to the sampled data and blackbox model provided in the constructor

Returns

self

Fitted estimator.

Parameters

X{sparse matrix} of shape (n_samples, n_features)

Dataset used for counterfactuals generation

pbounds:

The bounds for each feature to search over (dict with feature names as keys and tuple (min, max) as values).

causal_order:

The order of variables in the causal graph.

adjacency_matrix:

The adjacency matrix representing the causal structure.

yarray-like of shape (n_samples,).

Dataset target values used for blackbox model fitting only. You can provide fitted blackbox to constructor or fit it in this method by providing this parameter

masked_features:

List of interchangeable features

categorical_indicator:

True at the index where the variable should be treated as categorical

features_order:

order of features in query instance

bayesian_causality:

calculate discrete bayesian network for causal loss. If bayesian_causality is True, then adjacency_matrix and causal_order are not used. Instead, a discrete bayesian network will be used to calculate causal loss component

bayesian_model:

optionally, provide pre-fitted discrete bayesian model, if bayesian_causality is True. If it is not provided and bayesian_causality is True, then it will be initialized internally and fitted with the num_bins param

num_bins:

Number of bins to use for discretizing continuous features (bayesian causality only)

predict(X)

Predicts using blackbox model

Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

Dataset to predict

Returns

Prediction class labels for samples in X by blackbox model