-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
matrix_profile.py
88 lines (72 loc) · 2.88 KB
/
matrix_profile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python3 -u
# copyright: sktime developers, BSD-3-Clause License (see LICENSE file)
"""Implements matrix profile transformation."""
__author__ = ["mloning"]
__all__ = ["MatrixProfileTransformer"]
from sktime.transformations.base import BaseTransformer
class MatrixProfileTransformer(BaseTransformer):
"""Calculate the matrix profile of a time series.
Takes as input a single time series dataset and returns the matrix profile
for that time series dataset. The matrix profile is a vector that stores the
z-normalized Euclidean distance between any subsequence within a
time series and its nearest neighbor.
For more information on the matrix profile, see `stumpy's tutorial
<https://stumpy.readthedocs.io/en/latest/Tutorial_The_Matrix_Profile.html>`_
Parameters
----------
window_length : int
Notes
-----
Provides wrapper around functionality in `stumpy.stump
<https://stumpy.readthedocs.io/en/latest/api.html#stumpy.stump>`_
Examples
--------
>>> from sktime.transformations.series.matrix_profile import \
MatrixProfileTransformer
>>> from sktime.datasets import load_airline
>>> y = load_airline()
>>> transformer = MatrixProfileTransformer() # doctest: +SKIP
>>> y_hat = transformer.fit_transform(y) # doctest: +SKIP
"""
_tags = {
# packaging info
# --------------
"authors": ["mloning"],
"python_dependencies": "stumpy",
# estimator type
# --------------
"scitype:transform-input": "Series",
# what is the scitype of X: Series, or Panel
"scitype:transform-output": "Series",
# what scitype is returned: Primitives, Series, Panel
"scitype:instancewise": True, # is this an instance-wise transform?
"X_inner_mtype": ["np.ndarray"],
# which mtypes do _fit/_predict support for X?
"y_inner_mtype": "None", # which mtypes do _fit/_predict support for y?,
"univariate-only": True,
"fit_is_empty": True, # for unit test cases
}
def __init__(self, window_length=3):
self.window_length = window_length
super().__init__()
def _transform(self, X, y=None):
"""Transform X and return a transformed version.
private _transform containing the core logic, called from transform
Parameters
----------
X : 2D np.ndarray
Data to be transformed
y : ignored argument for interface compatibility
Additional data, e.g., labels for transformation
Returns
-------
Xt : 1D np.ndarray
transformed version of X
Matrix Profile of time series as output with length as
(n_timepoints-window_length+1)
"""
import stumpy
X = X.flatten()
Xt = stumpy.stump(X, self.window_length)
Xt = Xt[:, 0].astype("float")
return Xt