ShapeletTransformer#
- class ShapeletTransformer(n_shapelets=8, n_shapelets_for_selection=500, n_ts_for_selection_per_class=100, sliding_window=50, selection='mi_clf', distance='euclidean', mi_n_neighbors=100, random_state=42, n_jobs=1, ml_task='clf')[source]#
Bases:
RuleTreeBaseTransformerTransformer of time series into their distances from shapelets, subsequences selected among the ones of the time series in the node.
The distance of a time series from a shapelet is the smallest distance between the shapelet and the subsequences of the series of the same length. After the splitter has chosen a column, only the chosen shapelet is kept (
select).- Parameters:
n_shapelets (int, default=8) – Number of shapelets selected at each node.
n_shapelets_for_selection (int, numpy.inf or 'stratified', default=500) – Number of candidate subsequences extracted from the time series, among which the shapelets are selected. ‘stratified’ draws them according to the class distribution.
n_ts_for_selection_per_class (int or numpy.inf, default=100) – Number of time series of each class from which the candidates are extracted.
sliding_window (int, default=50) – Length of the shapelets.
selection ({'random', 'mi_clf', 'mi_reg', 'cluster'}, default='mi_clf') – How the shapelets are selected among the candidates: at random, by mutual information with the target, or as the medoids of a k-medoids clustering (requires scikit-learn-extra).
distance ({'euclidean', 'sqeuclidean', 'cosine', 'cityblock'} or callable, default='euclidean') – Distance between a shapelet and a subsequence; the distance of a time series from a shapelet is the minimum over the positions of the shapelet.
mi_n_neighbors (int, default=100) – Number of neighbors used to estimate the mutual information.
random_state (int, default=42) – Seed of the random choices of the candidates and of the shapelets.
n_jobs (int, default=1) – Number of threads used to compute the distances (-1: all the CPUs).
ml_task ({MODEL_TYPE_CLF, MODEL_TYPE_REG}, default=MODEL_TYPE_CLF) – Task of the stump: for classification, the candidates are drawn from the time series of each class; for regression (also with several targets), from all the time series.
- Variables:
See also
ShapeletTreeStumpClassifier,ShapeletTreeStumpRegressorExamples
>>> import numpy as np >>> from RuleTree.stumps.transformers import ShapeletTransformer >>> rng = np.random.default_rng(0) >>> X_ts, y = rng.normal(size=(20, 1, 30)), rng.integers(0, 2, size=20) >>> transformer = ShapeletTransformer(n_shapelets=3, sliding_window=5, selection="random") >>> transformer = transformer.fit(X_ts, y, context=None, node_id=None, cache=None) >>> transformer.transform(X_ts).shape (20, 3)
- supported_data_types = ('ts',)#
- fit(X, y, idx=None, data_type=None, sample_weight=None, *, context, node_id, cache)[source]#
Select the shapelets among the subsequences of the time series in the node.
- Parameters:
X (numpy.ndarray) – Data of the transformer’s type, all the instances.
y (numpy.ndarray) – Target, all the instances.
idx (numpy.ndarray, default=None) – Indices of the instances in the node. If None, all the instances.
data_type (str, default=None) – Type of X, used as key of the cached values.
sample_weight (numpy.ndarray, default=None) – Sample weights, all the instances.
context (RuleTree or None) – Tree being built, None outside a tree.
node_id (str or None) – Identifier of the node, e.g.
"Rl"(context.get_node_by_id(node_id)), None outside a tree.cache (dict or None) – Cache of the tree (namespace -> dictionary of values), None outside a tree: the component decides whether and how to use it (the ones of the library use
self._get_cache(cache)).
- Returns:
self
- Return type:
- Raises:
ValueError – If sample_weight is given, or y has several targets with a mutual information selection.
- transform(X, context=None, node_id=None, cache=None)[source]#
Distance of each time series from each shapelet.
- Parameters:
X (numpy.ndarray) – Time series, of shape (n_samples, 1, length).
context (RuleTree, default=None) – The tree, None outside a tree.
node_id (str, default=None) – Identifier of the node of the stump, None outside a tree.
cache (dict, default=None) – Cache of the tree, when called by it; None otherwise.
- Returns:
X_transformed
- Return type:
numpy.ndarray of shape (n_samples, n_shapelets)
- get_state()[source]#
Fitted state: the shapelets.
- Returns:
state – JSON-friendly dictionary, restored by set_state.
- Return type: