wildboar.explain.counterfactual._proto#
Module Contents#
Classes#
Model agnostic approach for constructing counterfactual explanations |
|
Evaluate if a sample is a counterfactual |
|
Evaluate if a counterfactual is predicted as y |
|
Evaluate if the probability of a counterfactual is at least a given constant |
|
Helper class that provides a standard way to create an ABC using |
|
Sample a prototype uniformly at random from the initial prototype sample |
|
Sample a prototype among the samples closest to the current counterfactual |
|
Sample shapelet prototypes |
|
Combines the KNearestPrototypeSample and the ShapeletPrototypeSampler |
|
Move a time series towards a prototype |
|
Transform a sample by moving it closer in euclidean space |
|
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.BaseCounterfactualModel 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:
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.ABCEvaluate 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:
TargetEvaluatorEvaluate if a counterfactual is predicted as y
- class wildboar.explain.counterfactual._proto.ProbabilityEvaluator(estimator, probability=0.5)#
Bases:
TargetEvaluatorEvaluate 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.ABCHelper 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:
PrototypeSamplerSample 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:
PrototypeSamplerSample 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:
PrototypeSamplerSample 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:
PrototypeSamplerCombines 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.ABCMove 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:
MetricTransformTransform 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:
MetricTransformTransform 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