-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassifiers.py
50 lines (38 loc) · 1.16 KB
/
classifiers.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
import statistics
import collections
Anomaly = -1
Normal = 1
class MovingAverageClassifier:
def __init__(self, threshold=2):
self.threshold = threshold
def _update(self):
self.mean = statistics.mean(self.data)
self.stdev = statistics.stdev(self.data, xbar=self.mean)
def fit(self, X):
self.data = collections.deque(X, maxlen=len(X))
self._update()
return self
def fit_predict(self, X):
output = []
for sample in X:
self.data.append(sample)
self._update()
if self.stdev > 0:
zscore = (sample - self.mean) / self.stdev
if abs(zscore) > self.threshold:
output.append(Anomaly)
else:
output.append(Normal)
else:
if sample == self.data[0]:
output.append(Normal)
else:
output.append(Anomaly)
return output
class MovingMedianClassifier:
def __init__(self):
pass
def fit(X):
pass
def fit_predict(X):
pass