⬅ tadkit/base/tadlearner.py source

1 from typing import Sequence, Optional, Protocol, runtime_checkable
2  
3 from tadkit.base.typing import ParamsDescription, Array
4  
5  
6 @runtime_checkable
7 class TADLearner(Protocol):
8 """Abstract class of Time Anomaly Detection Learner (model).
9  
10 Avoid explicit inheritance from this class. Better to simply do it implicitly.
11  
12 Methods:
13 fit: Fit the learner on input data.
14 score_samples: The measure of normality of an observation according to the fitted model.
15 The lower, the more abnormal.
16 predict: Predict if a particular sample is an outlier or not. For each observation, tells
17 whether or not (+1 or -1) it should be considered as an inlier according to the fitted model.
18  
19 Class attributes:
20 params_description: Description of the arguments of the __init__ method. See examples in the catalog.
21 required_properties: Get the properties that the input data must satisfies. See examples in the catalog.
22  
23 Example:
24 >>> assert isinstance(MyLearner, TADLearner)
25 >>> MyLearner.required_properties # The required property of input data
26 >>> MyLearner.params_description # The description of the params
27 >>> params = ... # Params to initiate learner
28 >>> learner = MyLearner(**params)
29 >>> learner.fit(X) # X, y must satisfy MyLearner.required_properties
30 >>> score_sample_pred = learner.score_samples(X_test)
31 """
32  
33 required_properties: Sequence[str] = []
34 params_description: ParamsDescription = {}
35  
  • E704 Multiple statements on one line (def)
36 def fit(self, X: Array, y: Optional[Array] = None) -> "TADLearner": ...
37  
38 def score_samples(self, X: Array) -> Array:
39 """
40 The measure of normality of an observation according to the fitted model.
41 Scikit-learn compatible.
42  
43 Parameters
44 ----------
45 X : {array-like, sparse matrix} of shape (n_samples, n_features)
46 The input samples.
47  
48 Returns
49 -------
50 scores : ndarray of shape (n_samples,)
51 The anomaly score of the input samples.
52 The lower, the more abnormal.
53 """
54 ...
55  
56 def predict(self, X: Array) -> Array:
57 """
58 Predict if a particular sample is an outlier or not.
59 Scikit-learn compatible.
60  
61 Parameters
62 ----------
63 X : {array-like, sparse matrix} of shape (n_samples, n_features)
64 The input samples.
65  
66 Returns
67 -------
68 is_inlier : ndarray of shape (n_samples,)
69 For each observation, tells whether or not (+1 or -1) it should
70 be considered as an inlier according to the fitted model.
71 """
72 ...