-
Notifications
You must be signed in to change notification settings - Fork 1
/
algos.py
355 lines (258 loc) · 10.4 KB
/
algos.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
def OCSVM(X_train, X_test, Y_test):
from sklearn.svm import OneClassSVM
ocSVM = OneClassSVM()
ocSVM.fit(X_train)
pred = ocSVM.predict(X_test)
pred[pred==1] = 0
pred[pred==-1] = 1
acc = np.sum(pred == Y_test)/X_test.shape[0]
print("ocSVM:" + str(acc))
return (acc*100)
def elm(X_train, X_test, Y_train, Y_test, ds_anom, ds_norm):
# CHECK : Constants
omega = 1.
class ELM(object):
def __init__(self, sess, batch_size, input_len, hidden_num, output_len, W, b):
'''
Args:
sess : TensorFlow session.
batch_size : The batch size (N)
input_len : The length of input. (L)
hidden_num : The number of hidden node. (K)
output_len : The length of output. (O)
W : randomly initialized weights
b : randomly initialized bias
'''
self._sess = sess
self._batch_size = batch_size
self._input_len = input_len
self._hidden_num = hidden_num
self._output_len = output_len
# for train
self._x0 = tf.placeholder(tf.float32, [self._batch_size, self._input_len])
self._t0 = tf.placeholder(tf.float32, [self._batch_size, self._output_len])
# for test
self._x1 = tf.placeholder(tf.float32, [None, self._input_len])
self._t1 = tf.placeholder(tf.float32, [None, self._output_len])
# self._W = tf.Variable(
# tf.random_normal([self._input_len, self._hidden_num]),
# trainable=False, dtype=tf.float32)
# self._b = tf.Variable(
# tf.random_normal([self._hidden_num]),
# trainable=False, dtype=tf.float32)
## Wts initialisation
self._W = W
self._b = b
self._beta = tf.Variable(
tf.zeros([self._hidden_num, self._output_len]),
trainable=False, dtype=tf.float32)
self._var_list = [self._W, self._b, self._beta]
self.H0 = tf.matmul(self._x0, self._W) + self._b # N x L
self.H0_T = tf.transpose(self.H0)
self.H1 = tf.matmul(self._x1, self._W) + self._b # N x L
self.H1_T = tf.transpose(self.H1)
# beta analytic solution : self._beta_s (K x O)
if self._input_len < self._hidden_num: # L < K
identity = tf.constant(np.identity(self._hidden_num), dtype=tf.float32)
self._beta_s = tf.matmul(tf.matmul(tf.matrix_inverse(
tf.matmul(self.H0_T, self.H0) + identity/omega),
self.H0_T), self._t0)
# _beta_s = (H_T*H + I/om)^(-1)*H_T*T
else:
identity = tf.constant(np.identity(self._batch_size), dtype=tf.float32)
self._beta_s = tf.matmul(tf.matmul(self.H0_T, tf.matrix_inverse(
tf.matmul(self.H0, self.H0_T)+identity/omega)), self._t0)
# _beta_s = H_T*(H*H_T + I/om)^(-1)*T
self._assign_beta = self._beta.assign(self._beta_s)
self._fx0 = tf.matmul(self.H0, self._beta)
self._fx1 = tf.matmul(self.H1, self._beta)
self._cost = tf.reduce_mean(tf.cast(tf.losses.mean_squared_error(labels=self._t0, predictions=self._fx0), tf.float32))
self._init = False
self._feed = False
# Cost for every sample point
# self._correct_prediction = tf.equal(tf.argmax(self._fx1,1), tf.argmax(self._t1,1))
# self._accuracy = tf.reduce_mean(tf.cast(self._correct_prediction, tf.float32))
self._testcost = tf.cast(tf.losses.mean_squared_error(labels=self._t1, predictions=self._fx1), tf.float32)
def feed(self, x, t):
'''
Args :
x : input array (N x L)
t : label array (N x O)
'''
if not self._init : self.init()
self._sess.run(self._assign_beta, {self._x0:x, self._t0:t})
# print(self._sess.run(self._cost, {self._x0:x, self._t0:t}))
self._feed = True
def init(self):
self._sess.run(tf.initialize_variables(self._var_list))
self._init = True
def test(self, x, t=None):
if not self._feed : exit("Not feed-forward trained")
if t is not None :
# print("Accuracy: {:.9f}".format(self._sess.run(self._accuracy, {self._x1:x, self._t1:t})))
return self._sess.run(self._testcost, {self._x1:x, self._t1:t})
else :
return self._sess.run(self._fx1, {self._x1:x})
# ## Initializing Parameters
# In[55]:
import tensorflow as tf
# In[56]:
sess = tf.Session()
batch_size = X_train.shape[0]
hidden_num = 150
input_len = X_train.shape[1]
print("batch_size : {}".format(batch_size))
print("hidden_num : {}".format(hidden_num))
print(input_len)
W = tf.Variable(
tf.random_normal([input_len, hidden_num]),
trainable=False, dtype=tf.float32)
b = tf.Variable(
tf.random_normal([hidden_num]),
trainable=False, dtype=tf.float32)
# ## Initializing list of W and b
# In[57]:
init_list = []
for i in range(10):
init_list.append((tf.Variable(tf.random_normal([input_len, hidden_num],seed=i),trainable=False, dtype=tf.float32),tf.Variable(tf.random_normal([hidden_num], seed=i),trainable=False, dtype=tf.float32)))
# ## Accuracy Function
# In[58]:
def accuracy(anom_pred):
cnt = 0
for pt in anom_pred:
if pt[1]>=ds_norm.shape[0]-X_train.shape[0] and pt[1]<X_test.shape[0]:
cnt+=1
return (cnt/float(ds_anom.shape[0]))*100
# ## Evaluation
# In[ ]:
results = {}
itr = 0
best_W = tf.Variable(tf.zeros([input_len,hidden_num]))
best_b = tf.Variable(tf.zeros([hidden_num]))
best_acc = 0.0
best_acc_idx = 0
for W,b in init_list:
## feed W,b from list and evaluate error and accuracy corresponding to them
elm = ELM(sess, batch_size, input_len, hidden_num, input_len, W, b)
train_x, train_y = (X_train[:batch_size], X_train[:batch_size])
elm.feed(train_x, train_y)
## error list
err = []
for idx,test_pt in enumerate(X_test):
x = test_pt.reshape(1,-1)
err.append((elm.test(x, x), idx))
err.sort(reverse=True)
anom_pred = err[:ds_anom.shape[0]]
acc = accuracy(anom_pred)
results[itr] = [(err,acc)]
itr += 1
if acc>best_acc:
best_W = W
best_b = b
best_acc = acc
best_acc_idx = itr-1
err = results[best_acc_idx][0][0]
err_array = np.array(err)
W_final = best_W.eval(session=sess)
b_final = best_b.eval(session=sess)
print("ELM: "+ str(best_acc))
return best_acc
def knn(X_train, X_test, Y_train, Y_test):
from pyod.models.knn import KNN
model = KNN()
model.fit(X_train)
pred = model.predict(X_test)
acc = np.sum(pred == Y_test)/X_test.shape[0]
print(acc)
return (acc*100)
def mean_knn(X_train, X_test, Y_train, Y_test):
from pyod.models.knn import KNN
model = KNN(method='mean')
model.fit(X_train)
pred = model.predict(X_test)
acc = np.sum(pred == Y_test)/X_test.shape[0]
print(acc)
return (acc*100)
def median_knn(X_train, X_test, Y_train, Y_test):
from pyod.models.knn import KNN
model = KNN(method='median')
model.fit(X_train)
pred = model.predict(X_test)
acc = np.sum(pred == Y_test)/X_test.shape[0]
print(acc)
return (acc*100)
def pca(X_train, X_test, Y_train, Y_test):
from pyod.models.pca import PCA
model = PCA()
model.fit(X_train)
pred = model.predict(X_test)
acc = np.sum(pred == Y_test)/X_test.shape[0]
print(acc)
return (acc*100)
def iforest(X_train, X_test, Y_train, Y_test):
from pyod.models.iforest import IForest
model = IForest(random_state=0)
model.fit(X_train)
pred = model.predict(X_test)
acc = np.sum(pred == Y_test)/X_test.shape[0]
print(acc)
return (acc*100)
def feature_bagging(X_train, X_test, Y_train, Y_test):
from pyod.models.feature_bagging import FeatureBagging
model = FeatureBagging(random_state=1)
model.fit(X_train)
pred = model.predict(X_test)
acc = np.sum(pred == Y_test)/X_test.shape[0]
print(acc)
return (acc*100)
## Data preparation for clustering algorithms
# ## DBSCAN
def DBSCAN(X_train, X_test, Y_train, Y_test, dsStatus):
samples = X_train.shape[0]+X_test.shape[0]
X = np.zeros((samples,X_train.shape[1]))
X[:X_train.shape[0], :] = X_train[:, :]
X[X_train.shape[0]:, :] = X_test[:, :]
X.shape
Y = np.zeros(samples,)
Y[:Y_train.shape[0]] = Y_train
Y[Y_train.shape[0]:] = Y_test
Y.shape
from sklearn.cluster import DBSCAN
if dsStatus.get() == 'page_blocks':
e = 50.0
elif dsStatus.get() == 'cancer':
e = 1.0
elif dsStatus.get() == 'lymphography':
e=20.0
else:
e=3.0
dbscan = DBSCAN(eps=e)
pred = dbscan.fit_predict(X)
pred
acc = np.sum(pred==Y)/Y.shape[0]
print("DBSCAN:" + str(acc))
return (acc*100)
# ## LOF
def Lof(X_train, X_test, Y_train, Y_test):
samples = X_train.shape[0]+X_test.shape[0]
X = np.zeros((samples,X_train.shape[1]))
X[:X_train.shape[0], :] = X_train[:, :]
X[X_train.shape[0]:, :] = X_test[:, :]
X.shape
Y = np.zeros(samples,)
Y[:Y_train.shape[0]] = Y_train
Y[Y_train.shape[0]:] = Y_test
Y.shape
from sklearn.neighbors import LocalOutlierFactor
lof = LocalOutlierFactor()
pred = lof.fit_predict(X)
np.unique(pred, return_counts=True)
pred[pred==1] = 0
pred[pred == -1] = 1
acc = np.sum(pred==Y)/Y.shape[0]
print("LOF:" + str(acc))
return (acc*100)