-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathother_models.py
88 lines (67 loc) · 2.73 KB
/
other_models.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
from sklearn.svm import OneClassSVM
import numpy as np
from kenchi.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
class WrapOneClassSVM:
def __init__(self, *args, **kwargs):
self.OneClassSVM_inst = OneClassSVM(*args, **kwargs)
def fit(self, X, y= None):
X_concat = [x.T for x in X]
X_concat = np.concatenate(X_concat)
self.OneClassSVM_inst.fit(X_concat)
def predict(self, X, y= None):
y_pred = []
for bi in range(len(X)):
y_pred_e = np.mean(self.OneClassSVM_inst.predict(X[bi].T))
y_pred_e = 1 if y_pred_e > 0 else -1
y_pred.append(y_pred_e)
return y_pred
class WrapKenchi:
def __init__(self, base, *args, **kwargs):
self.base_inst = base(*args, **kwargs)
self.scaler = StandardScaler()
def fit(self, X, y= None):
X_concat = [x.T for x in X]
X_concat = np.concatenate(X_concat)
# self.base_inst._fit(X_concat)
self.pipeline = make_pipeline(self.scaler, self.base_inst).fit(X_concat)
self.threshold = self.base_inst._get_threshold()
def predict(self, X, y= None):
y_pred = []
for bi in range(len(X)):
anomaly_score_mean = np.mean(self.pipeline.anomaly_score(X[bi].T))
y_pred_e = -1 if self.threshold < anomaly_score_mean else 1
y_pred.append(y_pred_e)
return y_pred
class WrapPyod:
def __init__(self, base, *args, **kwargs):
self.base_inst = base(*args, **kwargs)
def fit(self, X, y= None):
X_concat = [x.T for x in X]
X_concat = np.concatenate(X_concat)
self.base_inst.fit(X_concat)
def predict(self, X, y= None):
y_pred = []
for bi in range(len(X)):
anomaly_score_mean = np.mean(self.base_inst.decision_function(X[bi].T))
y_pred_e = -1 if self.base_inst.threshold_ < anomaly_score_mean else 1
y_pred.append(y_pred_e)
return y_pred
class WrapPulearn:
def __init__(self, base, *args, **kwargs):
self.base_inst = base(*args, **kwargs)
def fit(self, X, is_PL, y= None,):
is_PL_loc = []
for bi in range(len(X)):
for ii in range(X[bi].shape[1]):
is_PL_loc.append(-1 if is_PL[bi] == 0 else 1)
X_concat = [x.T for x in X]
X_concat = np.concatenate(X_concat)
self.base_inst.fit(X_concat, np.array(is_PL_loc))
def predict(self, X, y= None):
y_pred = []
for bi in range(len(X)):
y_preds_mean = np.mean(self.base_inst.predict(X[bi].T))
y_pred_e = 1 if y_preds_mean > 0.5 else -1
y_pred.append(y_pred_e)
return y_pred