wildboar.explain.counterfactual._proto#

Module Contents#

Classes#

PrototypeCounterfactual

Model agnostic approach for constructing counterfactual explanations

TargetEvaluator

Evaluate if a sample is a counterfactual

PredictEvaluator

Evaluate if a counterfactual is predicted as y

ProbabilityEvaluator

Evaluate if the probability of a counterfactual is at least a given constant

PrototypeSampler

Helper class that provides a standard way to create an ABC using

UniformPrototypeSampler

Sample a prototype uniformly at random from the initial prototype sample

KNearestPrototypeSampler

Sample a prototype among the samples closest to the current counterfactual

ShapeletPrototypeSampler

Sample shapelet prototypes

KNearestShapeletPrototypeSampler

Combines the KNearestPrototypeSample and the ShapeletPrototypeSampler

MetricTransform

Move a time series towards a prototype

EuclideanTransform

Transform a sample by moving it closer in euclidean space

DynamicTimeWarpTransform

Transform a sample by moving it closer using the dtw alignment matrix

class wildboar.explain.counterfactual._proto.PrototypeCounterfactual(background_x, background_y, *, metric='euclidean', metric_params=None, max_iter=100, step_size=0.1, n_prototypes='auto', target='auto', method='sample', method_params=None, random_state=None)#

Bases: wildboar.explain.counterfactual.base.BaseCounterfactual

Model agnostic approach for constructing counterfactual explanations

estimator_#

The estimator for which counterfactuals are computed

Type:

object

classes_#

The classes

Type:

ndarray

partitions_#

Dictionary of classes and PrototypeSampler

Type:

dict

target_#

The target evaluator

Type:

TargetEvaluator

References

Samsten, Isak (2020).

Model agnostic time series counterfactuals

fit(estimator)#

Fit the counterfactual to a given estimator

Parameters:

estimator (object) – An estimator for which counterfactual explanations are produced

Return type:

self

transform(x, y)#

Transform the i:th sample in x to a sample that would be labeled as the i:th label in y

Parameters:
  • x (array-like of shape (n_samples, n_timestep) or (n_samples, n_dimension, n_timestep)) – The samples to generate counterfactual explanations for

  • y (array-like of shape (n_samples,)) – The desired label of the counterfactual sample

Returns:

  • counterfactuals (ndarray of same shape as x) – The counterfactual for each sample. If success[i] == False, then the value of counterfactuals[i] is undefined.

  • success (ndarray of shape (n_samples,)) – Boolean vector indicating successful transformations.

class wildboar.explain.counterfactual._proto.TargetEvaluator(estimator)#

Bases: abc.ABC

Evaluate if a sample is a counterfactual

is_counterfactual(x, y)#

Return true if x is a counterfactual of label y

Parameters:
  • x (ndarray of shape (n_samples, n_timestep) or (n_timestep,)) – The counterfactual sample

  • y (object) – The counterfactual label

Returns:

bool

Return type:

true if counterfactual

class wildboar.explain.counterfactual._proto.PredictEvaluator(estimator)#

Bases: TargetEvaluator

Evaluate if a counterfactual is predicted as y

class wildboar.explain.counterfactual._proto.ProbabilityEvaluator(estimator, probability=0.5)#

Bases: TargetEvaluator

Evaluate if the probability of a counterfactual is at least a given constant

class wildboar.explain.counterfactual._proto.PrototypeSampler(x, y, n_prototypes, metric_transform, random_state)#

Bases: abc.ABC

Helper class that provides a standard way to create an ABC using inheritance.

abstract sample(o)#

Sample an example

Parameters:

o (ndarray of shape (n_timestep,)) – The current counterfactual sample

Returns:

prototype – A prototype of the counterfactual label

Return type:

ndarray of shape (n_timestep,)

move(o, p)#

Move the current counterfactual toward the prototype

Parameters:
  • o (ndarray of shape (n_timestep,)) – The current counterfactual sample

  • p (ndarray of shape (n_timestep,)) – The prototype of the counterfactual label

Returns:

new_counterfactual – The new counterfactual moved towards the prototype

Return type:

ndarray of shape (n_timestep,)

sample_move(o)#

Sampla a prototype and move the counterfactual towards the prototype

Parameters:

o (ndarray of shape (n_timestep,)) – The current counterfactual sample

Returns:

new_counterfactual – The new counterfactual moved towards the prototype

Return type:

ndarray of shape (n_timestep,)

class wildboar.explain.counterfactual._proto.UniformPrototypeSampler(x, y, n_prototypes, metric_transform, random_state)#

Bases: PrototypeSampler

Sample a prototype uniformly at random from the initial prototype sample

sample(_o)#

Sample an example

Parameters:

o (ndarray of shape (n_timestep,)) – The current counterfactual sample

Returns:

prototype – A prototype of the counterfactual label

Return type:

ndarray of shape (n_timestep,)

class wildboar.explain.counterfactual._proto.KNearestPrototypeSampler(x, y, n_prototypes, metric_transform, random_state)#

Bases: PrototypeSampler

Sample a prototype among the samples closest to the current counterfactual

nearest_index(o)#

Return the index of the closest sample

Parameters:

o (ndarray of shape (n_timestep,)) – The current counterfactual sample

Returns:

int

Return type:

an index

sample(o)#

Sample an example

Parameters:

o (ndarray of shape (n_timestep,)) – The current counterfactual sample

Returns:

prototype – A prototype of the counterfactual label

Return type:

ndarray of shape (n_timestep,)

class wildboar.explain.counterfactual._proto.ShapeletPrototypeSampler(x, y, n_prototypes, metric_transform, random_state, min_shapelet_size=0, max_shapelet_size=1)#

Bases: PrototypeSampler

Sample shapelet prototypes

sample_shapelet(p)#

Sample a shapelet from x

Parameters:

p (ndarray of shape (n_timestep,)) – The prototype sample

Returns:

shapelet – A shapelet

Return type:

ndarray

sample(_o)#

Sample an example

Parameters:

o (ndarray of shape (n_timestep,)) – The current counterfactual sample

Returns:

prototype – A prototype of the counterfactual label

Return type:

ndarray of shape (n_timestep,)

move(o, p)#

Move the best matching shapelet of the counterfactual sample towards the shapelet prototype

Parameters:
  • o (ndarray of shape (n_timestep,)) – The counterfactual sample

  • p (ndarray) – The prototype shapelet

Returns:

new_counterfactual – The new counterfactual moved towards the prototype

Return type:

ndarray of shape (n_timestep,)

class wildboar.explain.counterfactual._proto.KNearestShapeletPrototypeSampler(x, y, n_prototypes, metric_transform, random_state, min_shapelet_size=0, max_shapelet_size=1)#

Bases: PrototypeSampler

Combines the KNearestPrototypeSample and the ShapeletPrototypeSampler such that prototype samples are sampled among the nearest neighbors of the counterfactual

sample(o)#

Sample an example

Parameters:

o (ndarray of shape (n_timestep,)) – The current counterfactual sample

Returns:

prototype – A prototype of the counterfactual label

Return type:

ndarray of shape (n_timestep,)

move(o, p)#

Move the current counterfactual toward the prototype

Parameters:
  • o (ndarray of shape (n_timestep,)) – The current counterfactual sample

  • p (ndarray of shape (n_timestep,)) – The prototype of the counterfactual label

Returns:

new_counterfactual – The new counterfactual moved towards the prototype

Return type:

ndarray of shape (n_timestep,)

class wildboar.explain.counterfactual._proto.MetricTransform(gamma)#

Bases: abc.ABC

Move a time series towards a prototype

abstract move(o, p)#

Move the sample o towards p

Parameters:
  • o (ndarray of shape (n_timestep,)) – An array

  • p (ndarray of shape (n_timestep,)) – An array

Returns:

ndarray

Return type:

an array

class wildboar.explain.counterfactual._proto.EuclideanTransform(gamma)#

Bases: MetricTransform

Transform a sample by moving it closer in euclidean space

move(o, p)#

Move the sample o towards p

Parameters:
  • o (ndarray of shape (n_timestep,)) – An array

  • p (ndarray of shape (n_timestep,)) – An array

Returns:

ndarray

Return type:

an array

class wildboar.explain.counterfactual._proto.DynamicTimeWarpTransform(gamma, r=1.0)#

Bases: MetricTransform

Transform a sample by moving it closer using the dtw alignment matrix

move(o, p)#

Move the sample o towards p

Parameters:
  • o (ndarray of shape (n_timestep,)) – An array

  • p (ndarray of shape (n_timestep,)) – An array

Returns:

ndarray

Return type:

an array