-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemestral_work_02.py
380 lines (310 loc) · 14.6 KB
/
semestral_work_02.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
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
import random
import csv
import os
import timeit
from pprint import pprint
import clustering as cl
from xypoint import XYPoint
from xypoint import euclidean_distance, manhattan_distance, cosine_similarity
import numpy as np
from sklearn.metrics import silhouette_score
from copy import deepcopy
# for charting
import matplotlib.pyplot as plt
def plot_dataset(dataset, label="Points"):
# plot the dataset
fig, ax = plt.subplots(figsize=(10, 6))
# Plot points
ax.plot([p.x for p in dataset], [p.y for p in dataset], 'ro', label=label) # 'ro' means red dots
# Add a legend
ax.legend()
# show the plot
plt.show()
def plot_results(clusters, init_points=None, label="NA"):
# now with colors
colors = ['red', 'green', 'blue', 'yellow', 'orange']
fig, ax = plt.subplots(figsize=(10, 6))
# Plot points
i = 0
for _, points in clusters.items():
ax.plot([p.x for p in points], [p.y for p in points], 'o', label=f'Body {i + 1}', color=colors[i])
i += 1
ax.plot([p.x for p in clusters.keys()], [p.y for p in clusters.keys()], 'x', label=f'{label}', color='black')
if init_points:
ax.plot([p.x for p in init_points], [p.y for p in init_points], 's', label=f'Initial {label}', color='purple')
ax.legend()
plt.title(label)
plt.show()
def generate_sklearn_compatible_data(clusters_dataset, include_keys_as_data):
# Flatten the dataset and generate labels
mapping = {}
i = 0
for center, points in clusters_dataset.items():
for point in points:
mapping[point] = i
if include_keys_as_data:
mapping[center] = i
i += 1
# Generate dataset
X = []
labels = []
for point, label in mapping.items():
X.append([point.x, point.y])
labels.append(label)
return X, labels
def example():
k = 4
n = 100
clusters_dataset = cl.create_dataset(n, k, None, XYPoint(-1000000000, -1000000000), XYPoint(1000000000, 1000000000))
while not clusters_dataset:
clusters_dataset = cl.create_dataset(n, k, None, XYPoint(-1000000000, -1000000000), XYPoint(1000000000, 1000000000))
# start each function and plot it's results
clustering = cl.Clustering(euclidean_distance)
dataset = []
for center, points in clusters_dataset.items():
dataset.append(center)
dataset.extend(points)
init_points = random.sample(dataset, k=k)
# plot dataset
plot_dataset(dataset)
# plot with actual clusters
# append centers to dataset
c_dataset = deepcopy(clusters_dataset)
for center, points in c_dataset.items():
c_dataset[center].append(center)
plot_results(c_dataset, None, 'Origin point')
# k-means
c_clusters, debug_data = clustering.k_means(dataset, k, init_points)
plot_results(c_clusters, init_points, 'k-means')
# k-medoids
c_clusters, debug_data = clustering.k_medoids(dataset, k, init_points)
plot_results(c_clusters, init_points, 'k-medoids')
# k-medoids alternative
c_clusters, debug_data = clustering.k_medoids_alternative(dataset, k)
plot_results(c_clusters, [], 'k-medoids alternative')
def meassuring():
# ensure csv files (k_means.csv, k_medoids.csv and k_medoids_alt.csv) are present
if not os.path.isfile('k_means.csv'):
with open('k_means.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
writer.writerow(['dissimilarity_used', 'k', 'n', 'time_results', 'iterations', 'assignments', 'cost_calculations', 'cluster_calculations'])
if not os.path.isfile('k_medoids.csv'):
with open('k_medoids.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
writer.writerow(['dissimilarity_used', 'k', 'n', 'time_results', 'iterations', 'assignments', 'cost_calculations', 'cluster_calculations'])
if not os.path.isfile('k_medoids_alt.csv'):
with open('k_medoids_alt.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
writer.writerow(['dissimilarity_used', 'k', 'n', 'time_results', 'iterations', 'assignments', 'cost_calculations', 'cluster_calculations', 'vj_calculations'])
distance_functions = [euclidean_distance, manhattan_distance, cosine_similarity]
for i in range(len(distance_functions)):
dissimilarity_used = ''
if i == 0:
dissimilarity_used = 'euclidean_distance'
elif i == 1:
dissimilarity_used = 'manhattan_distance'
elif i == 2:
dissimilarity_used = 'cosine_similarity'
clustering = cl.Clustering(distance_functions[i])
# for k-means
print(f'k-means, {dissimilarity_used}')
k = 2
while k <= 7:
n = 2500000
time_result = 0
# while duration < 2 min
while time_result < 120:
print(f'k={k}, n={n}')
clusters_dataset = cl.create_dataset(n, k, None, XYPoint(-1000000000, -1000000000), XYPoint(1000000000, 1000000000))
while not clusters_dataset:
clusters_dataset = cl.create_dataset(n, k, None, XYPoint(-1000000000, -1000000000), XYPoint(1000000000, 1000000000))
dataset = []
for center, points in clusters_dataset.items():
dataset.append(center)
dataset.extend(points)
init_points = random.sample(dataset, k)
# k-means
timeit_start = timeit.default_timer()
c_clusters, debug_data = clustering.k_means(dataset, k, init_points)
timeit_stop = timeit.default_timer()
time_result = timeit_stop - timeit_start
# write to csv
with open('k_means.csv', 'a', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
res = [dissimilarity_used, k, n, time_result]
res.extend(list(debug_data.values()))
writer.writerow(res)
print(f'time_result={time_result}')
n += 2500000
k += 1
# for k-medoids
print(f'k-medoids, {dissimilarity_used}')
k = 2
while k <= 7:
n = 100
time_result = 0
# while duration < 2 min
while time_result < 120:
print(f'k={k}, n={n}')
clusters_dataset = cl.create_dataset(n, k, None, XYPoint(-1000000000, -1000000000), XYPoint(1000000000, 1000000000))
while not clusters_dataset:
clusters_dataset = cl.create_dataset(n, k, None, XYPoint(-1000000000, -1000000000), XYPoint(1000000000, 1000000000))
dataset = []
for center, points in clusters_dataset.items():
dataset.append(center)
dataset.extend(points)
init_points = random.sample(dataset, k)
# k-medoids
timeit_start = timeit.default_timer()
c_clusters, debug_data = clustering.k_medoids(dataset, k, init_points)
timeit_stop = timeit.default_timer()
time_result = timeit_stop - timeit_start
# write to csv
with open('k_medoids.csv', 'a', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
res = [dissimilarity_used, k, n, time_result]
res.extend(list(debug_data.values()))
writer.writerow(res)
print(f'time_result={time_result}')
n *= 1.1
n = int(n)
k += 1
# for k-medoids alternative
print(f'k-medoids alternative, {dissimilarity_used}')
k = 2
while k <= 7:
n = 100
time_result = 0
# while duration < 2 min
while time_result < 120:
clusters_dataset = cl.create_dataset(n, k, None, XYPoint(-1000000000, -1000000000), XYPoint(1000000000, 1000000000))
while not clusters_dataset:
clusters_dataset = cl.create_dataset(n, k, None, XYPoint(-1000000000, -1000000000), XYPoint(1000000000, 1000000000))
dataset = []
for center, points in clusters_dataset.items():
dataset.append(center)
dataset.extend(points)
# k-medoids alternative
timeit_start = timeit.default_timer()
c_clusters, debug_data = clustering.k_medoids_alternative(dataset, k)
timeit_stop = timeit.default_timer()
time_result = timeit_stop - timeit_start
# write to csv
with open('k_medoids_alt.csv', 'a', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
res = [dissimilarity_used, k, n, time_result]
res.extend(list(debug_data.values()))
writer.writerow(res)
print(f'time_result={time_result}')
n *= 1.1
n = int(n)
k += 1
def comparison():
dissimilarity_used = 'euclidean_distance'
clustering = cl.Clustering(euclidean_distance)
k = 5
n = 100
print(f'k={k}')
print(f'n, k_means_time, k_medoids_time, k_medoids_alt_time')
while n <= 1000:
clusters_dataset = cl.create_dataset(n, k, None, XYPoint(-1000000000, -1000000000), XYPoint(1000000000, 1000000000))
while not clusters_dataset:
clusters_dataset = cl.create_dataset(n, k, None, XYPoint(-1000000000, -1000000000), XYPoint(1000000000, 1000000000))
dataset = []
for center, points in clusters_dataset.items():
dataset.append(center)
dataset.extend(points)
init_points = random.sample(dataset, k=k)
# k-means
timeit_start = timeit.default_timer()
c_clusters, debug_data = clustering.k_means(dataset, k, init_points)
timeit_stop = timeit.default_timer()
k_means_time = timeit_stop - timeit_start
# k-medoids
timeit_start = timeit.default_timer()
c_clusters, debug_data = clustering.k_medoids(dataset, k, init_points)
timeit_stop = timeit.default_timer()
k_medoids_time = timeit_stop - timeit_start
# k-medoids alternative
timeit_start = timeit.default_timer()
c_clusters, debug_data = clustering.k_medoids_alternative(dataset, k)
timeit_stop = timeit.default_timer()
k_medoids_alt_time = timeit_stop - timeit_start
print(f'{n},{k_means_time},{k_medoids_time},{k_medoids_alt_time}')
n += 100
def comparison_selective():
dissimilarity_used = 'euclidean_distance'
clustering = cl.Clustering(euclidean_distance)
k = 5
n = 100
print(f'k={k}')
print(f'n, (init_points_time,k_means_time),(init_points_time,k_medoids_time),(dissimilarity_matrix_time,points_vj_time,k_medoids_time)')
while n <= 1000:
clusters_dataset = cl.create_dataset(n, k, None, XYPoint(-1000000000, -1000000000), XYPoint(1000000000, 1000000000))
while not clusters_dataset:
clusters_dataset = cl.create_dataset(n, k, None, XYPoint(-1000000000, -1000000000), XYPoint(1000000000, 1000000000))
dataset = []
for center, points in clusters_dataset.items():
dataset.append(center)
dataset.extend(points)
timeit_start = timeit.default_timer()
init_points = random.sample(dataset, k=k)
timeit_stop = timeit.default_timer()
init_points_time = timeit_stop - timeit_start
# k-means
timeit_start = timeit.default_timer()
c_clusters, debug_data = clustering.k_means(dataset, k, init_points)
timeit_stop = timeit.default_timer()
k_means_time = timeit_stop - timeit_start
# k-medoids
timeit_start = timeit.default_timer()
c_clusters, debug_data = clustering.k_medoids(dataset, k, init_points)
timeit_stop = timeit.default_timer()
k_medoids_time = timeit_stop - timeit_start
# k-medoids alternative
c_clusters, debug_data, times = clustering.k_medoids_alternative_timed(dataset, k)
print(f'{n},({init_points_time},{k_means_time}),({init_points_time},{k_medoids_time}),({times["dissimilarity_matrix"]},{times["points_vj"]},{times["k_medoids"]})')
n += 100
def success_rate_test():
dissimilarity_used = 'euclidean_distance'
clustering = cl.Clustering(euclidean_distance)
k = 5
n = 100
print(f'k={k}')
print(f'n, k_means_score, k_medoids_score, k_medoids_alt_score')
while n <= 1000:
clusters_dataset = cl.create_dataset(n, k, None, XYPoint(-1000000000, -1000000000), XYPoint(1000000000, 1000000000))
while not clusters_dataset:
clusters_dataset = cl.create_dataset(n, k, None, XYPoint(-1000000000, -1000000000), XYPoint(1000000000, 1000000000))
# Original dataset for silhouette evaluation
sk_dataset, _ = generate_sklearn_compatible_data(clusters_dataset, True)
dataset = []
for center, points in clusters_dataset.items():
dataset.append(center)
dataset.extend(points)
init_points = random.sample(dataset, k=k)
# k-means
c_clusters, debug_data = clustering.k_means(dataset, k, init_points)
# convert to sklearn format (for k-means, centroids are not part of the clusters)
_, sk_means = generate_sklearn_compatible_data(c_clusters, False)
# k-medoids
c_clusters, debug_data = clustering.k_medoids(dataset, k, init_points)
# convert to sklearn format (for k-medoids, medoids are part of the clusters)
_, sk_medoids = generate_sklearn_compatible_data(c_clusters, False)
# k-medoids alternative
c_clusters, debug_data = clustering.k_medoids_alternative(dataset, k)
# convert to sklearn format (for k-medoids alternative, medoids are part of the clusters)
_, sk_medoids_alt = generate_sklearn_compatible_data(c_clusters, False)
# Calculate silhouette scores
sk_means_score = silhouette_score(sk_dataset, sk_means)
sk_medoids_score = silhouette_score(sk_dataset, sk_medoids)
sk_medoids_alt_score = silhouette_score(sk_dataset, sk_medoids_alt)
print(f'{n},{sk_means_score},{sk_medoids_score},{sk_medoids_alt_score}')
n += 100
if __name__ == "__main__":
random.seed(hash('KIV/PRO'))
example()
comparison()
comparison_selective()
success_rate_test()
meassuring()