File size: 1,328 Bytes
d03866e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import stumpy
import numpy as np

class MatrixProfile():
    """
    Wrapper of the stympy implementation of the MatrixProfile algorithm

    Parameters
    ----------
    window : int,
        target subsequence length.
    
    Attributes
    ----------
    decision_scores_ : numpy array of shape (n_samples - m,)
        The anomaly score.
        The higher, the more abnormal. Anomalies tend to have higher
        scores. This value is available once the detector is
        fitted.
    """

    def __init__(self, window):
        self.window = window
        self.model_name = 'MatrixProfile'

    def fit(self, X, y=None):
        """Fit detector. y is ignored in unsupervised methods.
        
        Parameters
        ----------
        X : numpy array of shape (n_samples, )
            The input samples.
        y : Ignored
            Not used, present for API consistency by convention.
        
        Returns
        -------
        self : object
            Fitted estimator.
        """
        self.profile = stumpy.stump(X.ravel(),m=self.window)
        #self.profile = mp.compute(X, windows=self.window)
        res = np.zeros(len(X))
        res.fill(self.profile[:, 0].min())
        res[self.window//2:-self.window//2+1] = self.profile[:, 0]
        self.decision_scores_ = res
        return self