wildboar.explain.counterfactual._proto#
Module Contents#
Classes#
Transform a sample by moving it closer using the dtw alignment matrix |
|
Transform a sample by moving it closer in euclidean space |
|
Sample a prototype among the samples closest to the current counterfactual |
|
Combines the KNearestPrototypeSample and the ShapeletPrototypeSampler |
|
Move a time series towards a prototype |
|
Evaluate if a counterfactual is predicted as y |
|
Evaluate if the probability of a counterfactual is at least a given constant |
|
Model agnostic approach for constructing counterfactual explanations |
|
Helper class that provides a standard way to create an ABC using |
|
Sample shapelet prototypes |
|
Evaluate if a sample is a counterfactual |
|
Sample a prototype uniformly at random from the initial prototype sample |
|
Transform a sample by moving it closer using the dtw alignment matrix |
- class wildboar.explain.counterfactual._proto.DynamicTimeWarpTransform(gamma, r=1.0)[source]#
Bases:
MetricTransformTransform a sample by moving it closer using the dtw alignment matrix
Construct a new transformer
- Parameters:
gamma (float) – The strength of the move, where values close to 0 means that the sample is moved less and values closer to 1 mean that the sample is moved more.
- class wildboar.explain.counterfactual._proto.EuclideanTransform(gamma)[source]#
Bases:
MetricTransformTransform a sample by moving it closer in euclidean space
Construct a new transformer
- Parameters:
gamma (float) – The strength of the move, where values close to 0 means that the sample is moved less and values closer to 1 mean that the sample is moved more.
- class wildboar.explain.counterfactual._proto.KNearestPrototypeSampler(x, y, prototype_indicies, metric_transform)[source]#
Bases:
PrototypeSamplerSample a prototype among the samples closest to the current counterfactual
Sample and refine counterfactuals
- Parameters:
x (ndarray of shape (n_samples, n_timestep)) – The data samples labeled as y
y (object) – The label of the samples in x
n_prototypes (int) – The number of prototypes in the initial sample
metric_transform (MetricTransform) – The metric transformer.
random_state (RandomState) – The random number generator.
- class wildboar.explain.counterfactual._proto.KNearestShapeletPrototypeSampler(x, y, prototype_indicies, metric_transform, min_shapelet_size=0, max_shapelet_size=1)[source]#
Bases:
PrototypeSamplerCombines the KNearestPrototypeSample and the ShapeletPrototypeSampler such that prototype samples are sampled among the nearest neighbors of the counterfactual
Sample and refine counterfactuals
- Parameters:
x (ndarray of shape (n_samples, n_timestep)) – The data samples labeled as y
y (object) – The label of the samples in x
n_prototypes (int) – The number of prototypes in the initial sample
metric_transform (MetricTransform) – The metric transformer.
random_state (RandomState) – The random number generator.
- move(o, p)[source]#
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)[source]#
Bases:
abc.ABCMove a time series towards a prototype
Construct a new transformer
- Parameters:
gamma (float) – The strength of the move, where values close to 0 means that the sample is moved less and values closer to 1 mean that the sample is moved more.
- class wildboar.explain.counterfactual._proto.PredictEvaluator(estimator)[source]#
Bases:
TargetEvaluatorEvaluate if a counterfactual is predicted as y
Construct a new evaluator
- Parameters:
estimator (object) – The estimator
- class wildboar.explain.counterfactual._proto.ProbabilityEvaluator(estimator, probability=0.5)[source]#
Bases:
TargetEvaluatorEvaluate if the probability of a counterfactual is at least a given constant
Construct a new evaluator
- Parameters:
estimator (object) – The estimator
probability (float) – The minimum probability of the predicted label
- class wildboar.explain.counterfactual._proto.PrototypeCounterfactual(metric='euclidean', *, r=1.0, g=0.05, max_iter=100, step_size=0.1, n_prototypes='auto', target='auto', method='sample', min_shapelet_size=0.0, max_shapelet_size=1.0, random_state=None, verbose=False)[source]#
Bases:
wildboar.base.CounterfactualMixin,wildboar.base.ExplainerMixin,wildboar.base.BaseEstimatorModel agnostic approach for constructing counterfactual explanations
References
- Samsten, Isak (2020).
Model agnostic time series counterfactuals
Crate a new model agnostic counterfactual explainer.
- Parameters:
metric ({'euclidean', 'dtw', 'wdtw'}, optional) – The metric used to move the samples
r (float, optional) – The warping window size, if metric=’dtw’ or metric=’wdtw’
g (float, optional) – Penalty control for weighted DTW, if metric=’wdtw’
max_iter (int, optional) – The maximum number of iterations
step_size (float, optional) – The step size when moving samples toward class prototypes
n_prototypes (int, float or str, optional) – The number of initial prototypes to sample from
target (float or str, optional) –
The target evaluation of counterfactuals:
if ‘auto’ the counterfactual prediction must return the correct label
if float, the counterfactual prediction probability must exceed target value
method ({'sample', 'shapelet', 'nearest', 'nearest_shapelet'}, optional) –
Method for selecting prototypes
if ‘sample’ a prototype is sampled among the initial prototypes
if ‘shapelet’ a prototype shapelet is sampled among the initial prototypes
if ‘nearest’ a prototype is sampled from the closest n prototypes
if ‘nearest_shapelet’ a prototype shapelet is sampled from the closest n prototypes
min_shapelet_size (float, optional) – Minimum shapelet size, if method=’shapelet’ or ‘nearest_shapelet’
max_shapelet_size (float, optional) – Maximum shapelet size, if method=’shapelet’ or ‘nearest_shapelet’
random_state (RandomState or int, optional) – Pseudo-random number for consistency between different runs
- class wildboar.explain.counterfactual._proto.PrototypeSampler(x, y, prototype_indices, metric_transform)[source]#
Bases:
abc.ABCHelper class that provides a standard way to create an ABC using inheritance.
Sample and refine counterfactuals
- Parameters:
x (ndarray of shape (n_samples, n_timestep)) – The data samples labeled as y
y (object) – The label of the samples in x
n_prototypes (int) – The number of prototypes in the initial sample
metric_transform (MetricTransform) – The metric transformer.
random_state (RandomState) – The random number generator.
- move(o, p)[source]#
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,)
- abstract sample(o, random_state)[source]#
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,)
- sample_move(o, random_state)[source]#
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.ShapeletPrototypeSampler(x, y, prototype_indicies, metric_transform, min_shapelet_size=0, max_shapelet_size=1)[source]#
Bases:
PrototypeSamplerSample shapelet prototypes
Sample shapelet
- Parameters:
x (ndarray of shape (n_samples, n_timestep)) – The data samples
y (object) – The label of the samples in x
metric_transform (MetricTransform) – The metric transformer.
random_state (RandomState) – The random number generator.
min_shapelet_size (float) – The minimum shapelet size
max_shapelet_size (float) – The maximum shapelet size
- move(o, p)[source]#
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.TargetEvaluator(estimator)[source]#
Bases:
abc.ABCEvaluate if a sample is a counterfactual
Construct a new evaluator
- Parameters:
estimator (object) – The estimator
- class wildboar.explain.counterfactual._proto.UniformPrototypeSampler(x, y, prototype_indices, metric_transform)[source]#
Bases:
PrototypeSamplerSample a prototype uniformly at random from the initial prototype sample
Sample and refine counterfactuals
- Parameters:
x (ndarray of shape (n_samples, n_timestep)) – The data samples labeled as y
y (object) – The label of the samples in x
n_prototypes (int) – The number of prototypes in the initial sample
metric_transform (MetricTransform) – The metric transformer.
random_state (RandomState) – The random number generator.
- class wildboar.explain.counterfactual._proto.WeightedDynamicTimeWarpTransform(gamma, r=1, g=0.05)[source]#
Bases:
DynamicTimeWarpTransformTransform a sample by moving it closer using the dtw alignment matrix
Construct a new transformer
- Parameters:
gamma (float) – The strength of the move, where values close to 0 means that the sample is moved less and values closer to 1 mean that the sample is moved more.