lime_timeseries

Module Contents

Classes

LIMETimeseries

LIME implementation for timeseries.

class lime_timeseries.LIMETimeseries(kernel_width=25, verbose=False, preprocess_function=None, feature_selection='auto')[source]

LIME implementation for timeseries.

This implementation is inspired by the paper: Validation of XAI explanations for multivariate time series classification in the maritime domain. (https://doi.org/10.1016/j.jocs.2021.101539)

explain(model_or_function, input_timeseries, labels=(0,), class_names=None, num_features=1, num_samples=1, num_slices=1, batch_size=1, mask_type='mean', distance_method='cosine')[source]

Run the LIME explainer for timeseries.

Parameters:
  • model_or_function (callable or str) – The function that runs the model to be explained _or_ the path to a ONNX model on disk.

  • input_timeseries (np.ndarray) – The input time series data to be explained, with shape [batch_size, sequence_length, num_features].

  • labels (list) – The list of labels for different classes.

  • class_names (list) – The list of class names.

  • num_features (int) – The number of features to include in the explanation.

  • num_samples (int) – The number of samples to generate for the LIME explainer.

  • num_slices (int) – The number of slices to divide the time series data into.

  • batch_size (int) – The batch size to use for running the model.

  • mask_type (str) – The type of mask to apply to the time series data. Can be “mean” or “noise”.

  • distance_method (str) – The distance metric to use for LIME. Can be “cosine” or “euclidean”.

Returns:

An array (np.ndarray) containing the LIME explanations for each class.

Return type:

np.ndarray

_calculate_distance(masked_data, distance_method='cosine')[source]

Calcuate distance between perturbed data and the original samples.

Parameters:
  • masked_data (np.ndarray) – The perturbed time series data. *Note: The first instance is the original timeseries

  • distance_method (str) –

    The distance metric to use. Defaults to “cosine”. Supported options are: - ‘cosine’: Computes the cosine similarity between the two vectors. - ‘euclidean’: Computes the Euclidean distance between the two vectors. - ‘dtw’: Uses Dynamic Time Warping to calculate the distance between

    the two time series.

Returns:

A vector containing the distance between two timeseries.

Return type:

np.ndarray

Raises:

ValueError – If the given distance_method is not supported.

Notes

  • The cosine similarity is a measure of the similarity between two non-zero vectors

of an inner product space that measures the cosine of the angle between them. - The Euclidean distance is the straight-line distance between two points in

Euclidean space.

  • Dynamic Time Warping is an algorithm for measuring similarity between two time series sequences that may vary in speed or timing.

_dtw_distance(masked_data)[source]

Calculate distance based on dynamic time warping.

Parameters:

masked_data (np.ndarray) – An array of time series with some segments masked out. *Note: The first instance is the original timeseries

Returns:

DTW distances.

Return type:

np.ndarray