-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw.py
More file actions
369 lines (292 loc) · 12.3 KB
/
hw.py
File metadata and controls
369 lines (292 loc) · 12.3 KB
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
from math import log, sqrt, exp
from operator import add
import numpy as np
from functools import reduce
import sys
#consider expand abstract attribute size, class size
ATTRIBUTE_SIZE = 13
CLS_SIZE = 2
logging = False
class Machine:
def is_valid(self, data):
if len(data) > 13:
return True
else:
return False
def predict_file(self, data_file, with_roc):
roc_file = open("roc.txt", "w")
data_lines = data_file.readlines()
original = self.predict_data_lines(data_lines)
original.print()
original.print_roc_point()
if with_roc:
print()
print("Give threshold for draw roc curve....")
print()
for i in range(200):
threshold = -10 + (i / 10)
print("threshold : " + str(threshold))
result = self.predict_data_lines(data_lines, threshold)
if result.is_eer():
EER = result
result.print_roc_point()
roc_file.write(str(result.fp_rate()) + "\t" + str(result.tp_rate()) + "\n")
print()
roc_file.close()
try:
print("Equal error rate")
EER.print_roc_point()
except:
print("Program can't find EER")
def predict_data_lines(self, data_lines, threshold = 0):
predictResult = PredictResult()
for event in data_lines:
data_line = event.split()
if self.is_valid(data_line):
actual_cls = int(data_line.pop())
predict = self.predict(np.array([float(i) for i in data_line]), threshold)
predictResult.add_data(predict, actual_cls)
return predictResult
class BayesMachine(Machine):
def learn_file(self, file):
training_data, sum, cls_size, trans = self.file_to_data(file)
mean = self.calculate_mean(sum, cls_size)
cov_mat = self.calculate_covariance_matrix(mean, training_data, cls_size, trans)
prior = [cls_size[cls] / (len(training_data) * 1.0) for cls in range(CLS_SIZE)]
self.discriminant = self.make_discriminant(cov_mat, mean, prior)
def file_to_data(self, file):
training_data = []
cls_size = [0] * CLS_SIZE
data_lines = file.readlines()
sum = np.full((CLS_SIZE, ATTRIBUTE_SIZE), 0.0)
trans = [np.full((ATTRIBUTE_SIZE, ATTRIBUTE_SIZE), 0.0)] * CLS_SIZE
for event in data_lines:
data_line = event.split()
if self.is_valid(data_line):
data = {
'cls': int(data_line.pop()),
'data': np.array([float(i) for i in data_line]),
}
sum[data['cls']] = np.add(sum[data['cls']], data['data'])
trans = np.add(trans[data['cls']], np.mat(data['data']).T * np.mat(data['data']))
cls_size[data['cls']] += 1
training_data.append(data)
return training_data, sum, cls_size, trans
def calculate_mean(self, sum, cls_size):
means = []
for cls, cls_sum in enumerate(sum):
mean = cls_sum / cls_size[cls]
means.append(np.mat(mean).T)
return means
def calculate_covariance_matrix(self, mean, training_data, cls_size, trans):
cov_mat = [np.full((ATTRIBUTE_SIZE, ATTRIBUTE_SIZE), 0.0)] * CLS_SIZE
for data in training_data:
cov_mat[data['cls']] = np.add(cov_mat[data['cls']], (np.mat(data['data']).T - mean[data['cls']]) * (np.mat(data['data']).T - mean[data['cls']]).T)
for i in range(CLS_SIZE):
cov_mat[i] = cov_mat[i] / cls_size[i]
return cov_mat
def make_discriminant(self, cov_mat, mean, prior):
def g(x, cls):
w_1 = -0.5 * cov_mat[cls].I
w_2 = cov_mat[cls].I * mean[cls]
w_3 = -0.5 * mean[cls].T * cov_mat[cls].I * mean[cls] - (0.5 * log(np.linalg.det(cov_mat[cls]))) + log(prior[cls])
result = np.mat(x) * w_1 * np.mat(x).T + w_2.T * np.mat(x).T + w_3
return result[0]
return g
def predict(self, data, threshold):
positive = self.discriminant(data, 1)
negative = self.discriminant(data, 0)
if positive + threshold > negative:
return 1
else:
return 0
def sigmoid(weight, values):
return 1.0 / (1 + exp(-(weight.T @ values)))
class Perceptrons():
def __init__(self, nodes):
self.learning_rate = 0.001
self.layers = [None] * len(nodes)
self.weights = []
self.augmented_layers = [None] * len(nodes)
for index, node_length in enumerate(nodes):
self.change_layer(index, np.full((node_length, 1), 0.0))
for i in range(len(nodes) - 1):
self.weights.append(self.beginning_weight(nodes[i] + 1, nodes[i + 1]))
def last_layer(self):
return self.layers[len(self.layers) - 1]
def augmented_layer(self, index):
return self.augmented_layers[index]
def change_layer(self, index, layer):
self.layers[index] = layer
augmented_shape = (self.layers[index].shape[0] + 1, 1)
self.augmented_layers[index] = np.append([1.0], self.layers[index]).reshape(augmented_shape)
def weight(self, index):
return self.weights[index]
def beginning_weight(self, row, col):
return np.random.uniform(-0.01, 0.01, (row, col))
def calculate(self, step):
results = []
for cls in range(len(self.layers[step + 1])):
results.append(sigmoid(self.weight(step)[:, [cls]], self.augmented_layer(step)))
# TODO refactoring
return np.array([results]).T
def calculate_all(self):
for step in range(len(self.weights)):
self.change_layer(step + 1, self.calculate(step))
def err(self, weight_index):
if weight_index == len(self.weights) - 1:
return lambda output_node: self.actual_class - self.last_layer()[0][0]
else:
above_err = self.err(weight_index + 1)
above_layer = self.augmented_layer(weight_index + 1)
#err_sum = reduce(lambda x, y: x + above_err(i) * y, above_layer, 0)
err_sum = 0.0
for i in range(len(above_layer)):
#TODO should refactoring append [0] behind column vector
err_sum += (above_err(i) * above_layer[i][0])
#make code looks good
return lambda output_node: err_sum * (self.augmented_layer(weight_index + 1)[output_node] @ (1 - self.augmented_layer(weight_index + 1)[output_node]))
def delta(self, step, above_node_index, below_node_index):
result = self.learning_rate * self.err(step)(above_node_index) * self.augmented_layer(step)[below_node_index]
if isinstance(result, float):
return result
else:
return result[0]
def delta_matrix(self, step):
yop = []
for below_node_index in range(len(self.augmented_layer(step))):
results = []
for above_node_index in range(len(self.layers[step + 1])):
results.append(self.delta(step, above_node_index, below_node_index))
yop.append(results)
return np.array(yop)
def update_weight(self, step):
result = self.delta_matrix(step)
self.weights[step] += result
def update_weight_all(self):
for i in range(len(self.weights)):
self.update_weight(i)
def back_propogation(self, data):
self.change_layer(0, data['data'])
self.actual_class = data['cls']
self.calculate_all()
self.update_weight_all()
def info(self):
information = ""
information += "Layer depth : " + str(len(self.layers)) + "\n"
information += "Weight number : " + str(len(self.weights)) + "\n"
information += "Layers" + "\n"
information += "===========================" + "\n"
information += str(self.layers) + "\n"
information += "Weights" + "\n"
information += "===========================" + "\n"
information += str(self.weights) + "\n"
return information
class DeepLearningMachine(Machine):
def __init__(self, perceptrons):
self.epoch = 0
self.layers = []
self.weights = []
self.training_data = []
self.perceptrons = perceptrons
def predict(self, data, threshold=0):
result = self.discriminant(data)
if result + threshold > 0.5:
return 1
else:
return 0
def predict_data_lines(self, data_lines, threshold=0):
predictResult = PredictResult()
for event in data_lines:
data_line = event.split()
if self.is_valid(data_line):
data = self.raw_str_to_data(data_line)
actual_cls = data['cls']
predict = self.predict(data['data'], threshold)
predictResult.add_data(predict, actual_cls)
return predictResult
def learn_file(self, file):
self.read_file(file)
#print(self.perceptrons.info())
while self.converge():
print("EPOCH : " + str(self.epoch))
for data in self.training_data:
self.perceptrons.back_propogation(data)
#print(self.perceptrons.info())
def g(data):
self.perceptrons.change_layer(0, data)
self.perceptrons.calculate_all()
return self.perceptrons.last_layer().item(0, 0)
self.discriminant = g
def raw_str_to_data(self, data_line):
return {
'cls': int(data_line.pop()),
'data': np.array([float(i) for i in data_line]).reshape((13, 1)),
}
def read_file(self, data_file):
data_lines = data_file.readlines()
for event in data_lines:
data_line = event.split()
if self.is_valid(data_line):
self.training_data.append(self.raw_str_to_data(data_line))
def converge(self, delta = 0):
self.epoch += 1
if self.epoch >= 3:
return False
else:
return True
class PredictResult:
def __init__(self):
self.true_positive = self.true_negative = self.false_positive = self.false_negative = 0
def add_data(self, predict, actual_cls):
if predict == actual_cls:
if predict == 1:
self.true_positive += 1
else:
self.true_negative += 1
else:
if predict == 1:
self.false_positive += 1
else:
self.false_negative += 1
def empirical_error(self):
return self.false_negative + self.false_positive
def size(self):
return self.true_positive + self.true_negative + self.false_positive + self.false_negative
def fp_rate(self):
if self.false_positive + self.true_negative <= 0:
return 0
return self.false_positive / (self.false_positive + self.true_negative * 1.0)
def tp_rate(self):
if self.true_positive + self.false_negative <= 0:
return 0
return self.true_positive / (self.true_positive + self.false_negative * 1.0)
def is_eer(self):
if 0.99 < self.tp_rate() + self.fp_rate() < 1.01:
return True
else:
return False
def print_roc_point(self):
print("--------------------------------------------")
print("FPR : " + str(self.fp_rate()))
print("TPR : " + str(self.tp_rate()))
def print(self):
print()
print("Result")
print("--------------------------------------------")
print("Empirical error : " + str(self.empirical_error()))
print("Empirical error : " + str(self.empirical_error() / (self.size() * 1.0)))
print()
print("Confusion Matrix")
print("--------------------------------------------")
print("True positive : " + str(self.true_positive))
print("True negative : " + str(self.true_negative))
print("False positive : " + str(self.false_positive))
print("False negative : " + str(self.false_negative))
if __name__ == "__main__":
machine = DeepLearningMachine(Perceptrons([13, 2, 2, 1]))
with open('data/trn.txt') as file:
machine.learn_file(file)
test_datas = []
with open('data/tst.txt') as file:
machine.predict_file(file, True)