-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclassifiers.py
More file actions
414 lines (327 loc) · 13.9 KB
/
classifiers.py
File metadata and controls
414 lines (327 loc) · 13.9 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import numpy as np
import sys
import time
from math import sqrt
class classifier:
@staticmethod
def get_micd_dist(obj, coord):
return sqrt(np.matmul(np.matmul(np.subtract(coord, obj.mean), np.linalg.inv(obj.covariance)),
np.subtract(coord, obj.mean).T))
@staticmethod
def get_euclidean_dist(px1, py1, px0, py0):
return sqrt((px0 - px1) ** 2 + (py0 - py1) ** 2)
@staticmethod
def create_med2(a, b):
start_time = time.time()
num_steps = 1000
# Create Mesh grid
x_grid = np.linspace(min(*a.cluster[:, 0], *b.cluster[:, 0]) - 1, max(*a.cluster[:, 0], *b.cluster[:, 0]) + 1,
num_steps)
y_grid = np.linspace(min(*a.cluster[:, 1], *b.cluster[:, 1]) - 1, max(*a.cluster[:, 1], *b.cluster[:, 1]) + 1,
num_steps)
x0, y0 = np.meshgrid(x_grid, y_grid)
boundary = [[0 for _ in range(len(x_grid))] for _ in range(len(y_grid))]
for i in range(num_steps):
for j in range(num_steps):
a_dist = classifier.get_euclidean_dist(a.mean[0], a.mean[1], x0[i][j], y0[i][j])
b_dist = classifier.get_euclidean_dist(b.mean[0], b.mean[1], x0[i][j], y0[i][j])
boundary[i][j] = a_dist - b_dist
# Print progress
sys.stdout.write('\r')
sys.stdout.write('Calculating MED2... Row: {0:4}/{1:4}'.format(i + 1, num_steps))
end_time = time.time()
print('... completed ({:9.4f} seconds).'.format(end_time - start_time))
return [boundary, x_grid, y_grid]
@staticmethod
def create_med3(c, d, e):
start_time = time.time()
num_steps = 1000
# Create Mesh grid
x_grid = np.linspace(min(*c.cluster[:, 0], *d.cluster[:, 0], *e.cluster[:, 0]) - 1,
max(*c.cluster[:, 0], *d.cluster[:, 0], *e.cluster[:, 0]) + 1, num_steps)
y_grid = np.linspace(min(*c.cluster[:, 1], *d.cluster[:, 1], *e.cluster[:, 1]) - 1,
max(*c.cluster[:, 1], *d.cluster[:, 1], *e.cluster[:, 1]) + 1, num_steps)
x0, y0 = np.meshgrid(x_grid, y_grid)
boundary = [[0 for _ in range(len(x_grid))] for _ in range(len(y_grid))]
for i in range(num_steps):
for j in range(num_steps):
c_dist = classifier.get_euclidean_dist(*c.mean, x0[i][j], y0[i][j])
d_dist = classifier.get_euclidean_dist(*d.mean, x0[i][j], y0[i][j])
e_dist = classifier.get_euclidean_dist(*e.mean, x0[i][j], y0[i][j])
if min(c_dist, d_dist, e_dist) == c_dist:
boundary[i][j] = 1
elif min(c_dist, d_dist, e_dist) == d_dist:
boundary[i][j] = 2
else:
boundary[i][j] = 3
# Print progress
sys.stdout.write('\r')
sys.stdout.write('Calculating MED3... Row: {0:4}/{1:4}'.format(i + 1, num_steps))
end_time = time.time()
print('... completed ({:9.4f} seconds).'.format(end_time - start_time))
return [boundary, x_grid, y_grid]
@staticmethod
def create_ged2(a, b):
start_time = time.time()
num_steps = 1000
x_grid = np.linspace(min(*a.cluster[:, 0], *b.cluster[:, 0]) - 1, max(*a.cluster[:, 0], *b.cluster[:, 0]) + 1,
num_steps)
y_grid = np.linspace(min(*a.cluster[:, 1], *b.cluster[:, 1]) - 1, max(*a.cluster[:, 1], *b.cluster[:, 1]) + 1,
num_steps)
x, y = np.meshgrid(x_grid, y_grid)
boundary = [[0 for _ in range(len(x_grid))] for _ in range(len(y_grid))]
for i in range(num_steps):
for j in range(num_steps):
coord = [x[i][j], y[i][j]]
a_dist = classifier.get_micd_dist(a, coord)
b_dist = classifier.get_micd_dist(b, coord)
boundary[i][j] = (a_dist - b_dist)
# Print progress
sys.stdout.write('\r')
sys.stdout.write('Calculating GED2... Row: {0:4}/{1:4}'.format(i + 1, num_steps))
end_time = time.time()
print('... completed ({:9.4f} seconds).'.format(end_time - start_time))
return [boundary, x_grid, y_grid]
@staticmethod
def create_ged3(c, d, e):
start_time = time.time()
num_steps = 1000
x_grid = np.linspace(min(*c.cluster[:, 0], *d.cluster[:, 0], *e.cluster[:, 0]) - 1,
max(*c.cluster[:, 0], *d.cluster[:, 0], *e.cluster[:, 0]) + 1, num_steps)
y_grid = np.linspace(min(*c.cluster[:, 1], *d.cluster[:, 1], *e.cluster[:, 1]) - 1,
max(*c.cluster[:, 1], *d.cluster[:, 1], *e.cluster[:, 1]) + 1, num_steps)
x, y = np.meshgrid(x_grid, y_grid)
boundary = [[0 for _ in range(len(x_grid))] for _ in range(len(y_grid))]
for i in range(num_steps):
for j in range(num_steps):
coord = [x[i][j], y[i][j]]
c_dist = classifier.get_micd_dist(c, coord)
d_dist = classifier.get_micd_dist(d, coord)
e_dist = classifier.get_micd_dist(e, coord)
if min(c_dist, d_dist, e_dist) == c_dist:
boundary[i][j] = 1
elif min(c_dist, d_dist, e_dist) == d_dist:
boundary[i][j] = 2
else:
boundary[i][j] = 3
# Print progress
sys.stdout.write('\r')
sys.stdout.write('Calculating GED3... Row: {0:4}/{1:4}'.format(i + 1, num_steps))
end_time = time.time()
print('... completed ({:9.4f} seconds).'.format(end_time - start_time))
return [boundary, x_grid, y_grid]
@staticmethod
def get_map2(a, b, num_steps=1000):
# Create Mesh grid
x_grid = np.linspace(min(*a.cluster[:, 0], *b.cluster[:, 0]) - 1, max(*a.cluster[:, 0], *b.cluster[:, 0]) + 1,
num_steps)
y_grid = np.linspace(min(*a.cluster[:, 1], *b.cluster[:, 1]) - 1, max(*a.cluster[:, 1], *b.cluster[:, 1]) + 1,
num_steps)
x0, y0 = np.meshgrid(x_grid, y_grid)
boundary = [[0 for _ in range(len(x_grid))] for _ in range(len(y_grid))]
inv_cov_a = np.linalg.inv(a.covariance)
inv_cov_b = np.linalg.inv(b.covariance)
mean_a = np.array(a.mean)
mean_b = np.array(b.mean)
Q0 = np.subtract(inv_cov_a, inv_cov_b)
Q1 = 2 * (np.dot(mean_b, inv_cov_b) - np.dot(mean_a, inv_cov_a))
Q2 = np.dot(np.dot(mean_a, inv_cov_a), mean_a.T) - np.dot(np.dot(mean_b, inv_cov_b), mean_b.T)
Q3 = np.log((b.n / a.n))
Q4 = np.log(np.linalg.det(a.covariance) / np.linalg.det(b.covariance))
for i in range(num_steps):
for j in range(num_steps):
coord = [x0[i][j], y0[i][j]]
dist = np.matmul(np.matmul(coord, Q0), np.array(coord).T) + np.matmul(Q1, np.array(
coord).T) + Q2 + 2 * Q3 + Q4
boundary[i][j] = dist
return [boundary, x_grid, y_grid]
@staticmethod
def create_map2(a, b):
start_time = time.time()
num_steps = 1000
boundary, x_grid, y_grid = classifier.get_map2(a, b, num_steps)
for i in range(num_steps):
for j in range(num_steps):
boundary[i][j] = 1 if boundary[i][j] < 0 else 2
# Print progress
sys.stdout.write('\r')
sys.stdout.write('Calculating MAP2... Row: {0:4}/{1:4}'.format(i + 1, num_steps))
end_time = time.time()
print('... completed ({:9.4f} seconds).'.format(end_time - start_time))
return [boundary, x_grid, y_grid]
@staticmethod
def create_map3(c, d, e):
start_time = time.time()
num_steps = 1000
# Create Mesh grid
x_grid = np.linspace(min(*c.cluster[:, 0], *d.cluster[:, 0], *e.cluster[:, 0]) - 1,
max(*c.cluster[:, 0], *d.cluster[:, 0], *e.cluster[:, 0]) + 1, num_steps)
y_grid = np.linspace(min(*c.cluster[:, 1], *d.cluster[:, 1], *e.cluster[:, 1]) - 1,
max(*c.cluster[:, 1], *d.cluster[:, 1], *e.cluster[:, 1]) + 1, num_steps)
boundary = [[0 for _ in range(len(x_grid))] for _ in range(len(y_grid))]
boundary_cd = classifier.get_map2(c, d, num_steps)[0]
boundary_ce = classifier.get_map2(c, e, num_steps)[0]
boundary_de = classifier.get_map2(d, e, num_steps)[0]
for i in range(num_steps):
for j in range(num_steps):
if boundary_cd[i][j] >= 0 and boundary_de[i][j] <= 0:
boundary[i][j] = 2
elif boundary_de[i][j] >= 0 and boundary_ce[i][j] >= 0:
boundary[i][j] = 3
elif boundary_ce[i][j] <= 0 and boundary_cd[i][j] <= 0:
boundary[i][j] = 1
# Print progress
sys.stdout.write('\r')
sys.stdout.write('Calculating MAP3... Row: {0:4}/{1:4}'.format(i + 1, num_steps))
end_time = time.time()
print('... completed ({:9.4f} seconds).'.format(end_time - start_time))
return [boundary, x_grid, y_grid]
@staticmethod
def create_nn2(a, b):
start_time = time.time()
num_steps = 1000
# Create Mesh grid
x_grid = np.linspace(min(*a.cluster[:, 0], *b.cluster[:, 0]) - 1, max(*a.cluster[:, 0], *b.cluster[:, 0]) + 1,
num_steps)
y_grid = np.linspace(min(*a.cluster[:, 1], *b.cluster[:, 1]) - 1, max(*a.cluster[:, 1], *b.cluster[:, 1]) + 1,
num_steps)
x0, y0 = np.meshgrid(x_grid, y_grid)
boundary = [[0 for _ in range(len(x_grid))] for _ in range(len(y_grid))]
for i in range(num_steps):
for j in range(num_steps):
# Find nearest neighbours
a_dist = float('inf')
for coord in a.cluster:
temp_dist = classifier.get_euclidean_dist(coord[0], coord[1], x0[i][j], y0[i][j])
if temp_dist < a_dist:
a_dist = temp_dist
b_dist = float('inf')
for coord in b.cluster:
temp_dist = classifier.get_euclidean_dist(coord[0], coord[1], x0[i][j], y0[i][j])
if temp_dist < b_dist:
b_dist = temp_dist
boundary[i][j] = a_dist - b_dist
# Print progress
sys.stdout.write('\r')
sys.stdout.write('Calculating NN2... Row: {0:4}/{1:4}'.format(i + 1, num_steps))
end_time = time.time()
print('... completed ({:9.4f} seconds).'.format(end_time - start_time))
return [boundary, x_grid, y_grid]
@staticmethod
def create_nn3(c, d, e):
start_time = time.time()
num_steps = 1000
# Create Mesh grid
x_grid = np.linspace(min(*c.cluster[:, 0], *d.cluster[:, 0], *e.cluster[:, 0]) - 1,
max(*c.cluster[:, 0], *d.cluster[:, 0], *e.cluster[:, 0]) + 1, num_steps)
y_grid = np.linspace(min(*c.cluster[:, 1], *d.cluster[:, 1], *e.cluster[:, 1]) - 1,
max(*c.cluster[:, 1], *d.cluster[:, 1], *e.cluster[:, 1]) + 1, num_steps)
x0, y0 = np.meshgrid(x_grid, y_grid)
boundary = [[0 for _ in range(len(x_grid))] for _ in range(len(y_grid))]
for i in range(num_steps):
for j in range(num_steps):
# Find nearest neighbours
c_dist = float('inf')
for coord in c.cluster:
temp_dist = classifier.get_euclidean_dist(coord[0], coord[1], x0[i][j], y0[i][j])
if temp_dist < c_dist:
c_dist = temp_dist
d_dist = float('inf')
for coord in d.cluster:
temp_dist = classifier.get_euclidean_dist(coord[0], coord[1], x0[i][j], y0[i][j])
if temp_dist < d_dist:
d_dist = temp_dist
e_dist = float('inf')
for coord in e.cluster:
temp_dist = classifier.get_euclidean_dist(coord[0], coord[1], x0[i][j], y0[i][j])
if temp_dist < e_dist:
e_dist = temp_dist
if min(c_dist, d_dist, e_dist) == c_dist:
boundary[i][j] = 1
elif min(c_dist, d_dist, e_dist) == d_dist:
boundary[i][j] = 2
else:
boundary[i][j] = 3
# Print progress
sys.stdout.write('\r')
sys.stdout.write('Calculating NN3... Row: {0:4}/{1:4}'.format(i + 1, num_steps))
end_time = time.time()
print('... completed ({:9.4f} seconds).'.format(end_time - start_time))
return [boundary, x_grid, y_grid]
@staticmethod
def create_knn2(a, b):
start_time = time.time()
num_steps = 1000
# Create Mesh grid
x_grid = np.linspace(min(*a.cluster[:, 0], *b.cluster[:, 0]) - 1, max(*a.cluster[:, 0], *b.cluster[:, 0]) + 1,
num_steps)
y_grid = np.linspace(min(*a.cluster[:, 1], *b.cluster[:, 1]) - 1, max(*a.cluster[:, 1], *b.cluster[:, 1]) + 1,
num_steps)
x0, y0 = np.meshgrid(x_grid, y_grid)
boundary = [[0 for _ in range(len(x_grid))] for _ in range(len(y_grid))]
for i in range(num_steps):
for j in range(num_steps):
# Find nearest neighbours
a_group = [float('inf') for _ in range(4)]
for coord in a.cluster:
temp_dist = classifier.get_euclidean_dist(coord[0], coord[1], x0[i][j], y0[i][j])
if temp_dist < max(a_group):
a_group[a_group.index(max(a_group))] = temp_dist
a_dist = np.mean(a_group)
b_group = [float('inf') for _ in range(4)]
for coord in b.cluster:
temp_dist = classifier.get_euclidean_dist(coord[0], coord[1], x0[i][j], y0[i][j])
if temp_dist < max(b_group):
b_group[b_group.index(max(b_group))] = temp_dist
b_dist = np.mean(b_group)
boundary[i][j] = a_dist - b_dist
# Print progress
sys.stdout.write('\r')
sys.stdout.write('Calculating KNN2... Row: {0:4}/{1:4}'.format(i + 1, num_steps))
end_time = time.time()
print('... completed ({:9.4f} seconds).'.format(end_time - start_time))
return [boundary, x_grid, y_grid]
@staticmethod
def create_knn3(c, d, e):
start_time = time.time()
num_steps = 1000
# Create Mesh grid
x_grid = np.linspace(min(*c.cluster[:, 0], *d.cluster[:, 0], *e.cluster[:, 0]) - 1,
max(*c.cluster[:, 0], *d.cluster[:, 0], *e.cluster[:, 0]) + 1, num_steps)
y_grid = np.linspace(min(*c.cluster[:, 1], *d.cluster[:, 1], *e.cluster[:, 1]) - 1,
max(*c.cluster[:, 1], *d.cluster[:, 1], *e.cluster[:, 1]) + 1, num_steps)
x0, y0 = np.meshgrid(x_grid, y_grid)
boundary = [[0 for _ in range(len(x_grid))] for _ in range(len(y_grid))]
for i in range(num_steps):
for j in range(num_steps):
# Find nearest neighbours
c_group = [float('inf') for _ in range(4)]
for coord in c.cluster:
temp_dist = classifier.get_euclidean_dist(coord[0], coord[1], x0[i][j], y0[i][j])
if temp_dist < max(c_group):
c_group[c_group.index(max(c_group))] = temp_dist
c_dist = np.mean(c_group)
d_group = [float('inf') for _ in range(4)]
for coord in d.cluster:
temp_dist = classifier.get_euclidean_dist(coord[0], coord[1], x0[i][j], y0[i][j])
if temp_dist < max(d_group):
d_group[d_group.index(max(d_group))] = temp_dist
d_dist = np.mean(d_group)
e_group = [float('inf') for _ in range(4)]
for coord in e.cluster:
temp_dist = classifier.get_euclidean_dist(coord[0], coord[1], x0[i][j], y0[i][j])
if temp_dist < max(e_group):
e_group[e_group.index(max(e_group))] = temp_dist
e_dist = np.mean(e_group)
if min(c_dist, d_dist, e_dist) == c_dist:
boundary[i][j] = 1
elif min(c_dist, d_dist, e_dist) == d_dist:
boundary[i][j] = 2
else:
boundary[i][j] = 3
# Print progress
sys.stdout.write('\r')
sys.stdout.write('Calculating KNN3... Row: {0:4}/{1:4}'.format(i + 1, num_steps))
end_time = time.time()
print('... completed ({:9.4f} seconds).'.format(end_time - start_time))
return [boundary, x_grid, y_grid]