RuleTree.utils.shapelet_transform package#
Submodules#
RuleTree.utils.shapelet_transform.Shapelets module#
Shapelets: the shapelet transform of time series used by the shapelet stumps.
- class Shapelets(n_shapelets=100, n_shapelets_for_selection=inf, n_ts_for_selection_per_class=inf, sliding_window=50, selection='random', distance='euclidean', mi_n_neighbors=100, random_state=42, n_jobs=1)[source]#
Bases:
TransformerMixinShapelets transformer for time series classification.
This class implements the shapelet transform method which transforms time series data into a feature vector of distances between the time series and a set of shapelets. Shapelets are discriminative subsequences extracted from the time series.
The transformer follows scikit-learn’s TransformerMixin interface (fit, transform).
- Parameters:
n_shapelets (int, default=100) – Number of shapelets to be used in the transformation.
n_shapelets_for_selection (int or str, default=np.inf) – Number of candidate shapelets to generate before selection. If ‘stratified’, generates candidates based on class distribution. If np.inf, generates all possible candidates.
n_ts_for_selection_per_class (int, default=np.inf) – Number of time series to use per class for shapelet candidates.
sliding_window (int, default=50) – Length of the shapelets to extract.
selection (str, default='random') – Method used to select the final shapelets from candidates: - ‘random’: random selection - ‘mi_clf’: mutual information (classification) - ‘mi_reg’: mutual information (regression) - ‘cluster’: KMedoids clustering
distance (str or callable, default='euclidean') – Distance metric to use for computing distances between shapelets and time series. Options: ‘euclidean’, ‘sqeuclidean’, ‘cosine’, ‘cityblock’, or custom callable.
mi_n_neighbors (int, default=100) – Number of neighbors to use for mutual information calculation.
random_state (int, default=42) – Random seed for reproducibility.
n_jobs (int, default=1) – Number of parallel jobs. If -1, uses all available processors.
- fit(X, y=None)[source]#
Fit the Shapelets transformer on training data.
- Parameters:
X (array-like of shape (n_samples, n_signals, n_timepoints)) – Training time series data. Currently only supports n_signals=1 (univariate time series).
y (array-like of shape (n_samples,), default=None) – Target values for supervised learning.
- Returns:
self – Returns self.
- Return type:
- Raises:
NotImplementedError – If multivariate time series are provided (n_signals > 1).
- transform(X, y=None)[source]#
Transform time series data into shapelet distance features.
For each time series in X, computes the minimum distance to each shapelet, resulting in a feature vector of length n_shapelets.
- Parameters:
X (array-like of shape (n_samples, n_signals, n_timepoints)) – Time series data to transform.
y (array-like of shape (n_samples,), default=None) – Target values (unused in transformation).
- Returns:
Transformed data, where each feature is the minimum distance between a time series and a shapelet.
- Return type:
array-like of shape (n_samples, n_shapelets)
RuleTree.utils.shapelet_transform.matrix_to_vector_distances module#
Matrix to vector distance calculations for shapelet transforms.
This module provides optimized functions for computing various distance metrics between a matrix of vectors and a single vector. These functions are critical for efficient shapelet distance calculations in the RuleTree framework.
- euclidean(matrix, vector)[source]#
Calculate the Euclidean distance between each row in a matrix and a vector.
- Parameters:
matrix (numpy.ndarray) – A 2D numpy array where each row is a vector
vector (numpy.ndarray) – A 1D numpy array to compute distances against
- Returns:
A 1D array containing the Euclidean distance between each row of the matrix and the vector
- Return type:
- sqeuclidean(matrix, vector)[source]#
Calculate the squared Euclidean distance between each row in a matrix and a vector.
- Parameters:
matrix (numpy.ndarray) – A 2D numpy array where each row is a vector
vector (numpy.ndarray) – A 1D numpy array to compute distances against
- Returns:
A 1D array containing the squared Euclidean distance between each row of the matrix and the vector
- Return type:
- cityblock(matrix, vector)[source]#
Calculate the Manhattan (cityblock) distance between each row in a matrix and a vector.
- Parameters:
matrix (numpy.ndarray) – A 2D numpy array where each row is a vector
vector (numpy.ndarray) – A 1D numpy array to compute distances against
- Returns:
A 1D array containing the Manhattan distance between each row of the matrix and the vector
- Return type:
- cosine(matrix, vector)[source]#
Calculate the cosine distance between each row in a matrix and a vector.
Note: This function handles potential numerical issues by returning 0 for any NaN values that might occur during calculation.
- Parameters:
matrix (numpy.ndarray) – A 2D numpy array where each row is a vector
vector (numpy.ndarray) – A 1D numpy array to compute distances against
- Returns:
A 1D array containing the cosine distance between each row of the matrix and the vector
- Return type:
RuleTree.utils.shapelet_transform.drawing module#
Drawing of shapelets in the nodes of the Graphviz representation of a tree.
- graphviz_shapelets(label: str, shapes: list, colors: list = None) dict[source]#
Attributes of a Graphviz node with a label above a plot of one or more shapelets, drawn into a temporary PNG file (requires matplotlib).
- Parameters:
label (str) – Text of the node, e.g. the rule of the split.
shapes (list of numpy.ndarray) – The shapelets to draw, each of shape (length,).
colors (list of str, default=None) – Color of each shapelet. If None, the default colors of matplotlib.
- Returns:
attributes – Attributes of the Graphviz node.
- Return type:
- Raises:
MissingOptionalDependencyError – If matplotlib is not installed.