-
Notifications
You must be signed in to change notification settings - Fork 19
/
k_medoids.py
128 lines (104 loc) · 3.44 KB
/
k_medoids.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
import random
import numpy as np
from tqdm import tqdm
from functools import partial
from Levenshtein import distance
from multiprocessing import Pool, cpu_count
def calculate_medoid(items):
return min(items, key=partial(max_distance, items))
def total_distance(items, medoid):
return sum(distance(medoid, o) for o in items)
def max_distance(items, medoid):
return max(distance(medoid, o) for o in items)
def associate_(args_):
o, medoids = args_
medoid = min(medoids, key=lambda m: distance(m, o))
return medoid
def associate(pool, medoids, items):
assigned = list(
pool.imap(associate_, [(x, medoids) for x in items])
)
clusters = {medoid: [] for medoid in medoids}
for x, ci in zip(items, assigned):
clusters[ci].append(x)
return assigned, clusters
def cost(solution):
return sum(
max_distance(items, medoid)
for medoid, items in solution.items()
)
def clustered(items, n_clusters, iterations):
"""
Implements K-Medoids clustering
"""
with Pool(cpu_count()) as pool:
# start off clustering around random medoids
medoids = random.sample(items, n_clusters)
assigned, clusters = associate(pool, medoids, items)
for i in tqdm(range(iterations)):
previous = clusters
# update medoids
medoids = list(
pool.imap(calculate_medoid, clusters.values())
)
# recompute clusters with new medoids
assigned, clusters = associate(pool, medoids, items)
# has the solution converged?
if cost(clusters) == cost(previous):
break
radius = {
medoid: max_distance(items, medoid)
for medoid, items in clusters.items()
}
distortion = {
x: distance(x, ci) for x, ci in zip(items, assigned)
}
return clusters, radius, distortion
def search_radius(args_):
q, clusters, radius, T = args_
num_evaluated = 0
for k, v in clusters.items():
dist = distance(k, q)
if dist - radius[k] < T:
num_evaluated += len(v)
return num_evaluated
def search_distortion(args_):
q, clusters, distortion, T = args_
num_evaluated = 0
for k, xs in clusters.items():
dist = distance(k, q)
for x in xs:
if dist - distortion[x] < T:
num_evaluated += 1
return num_evaluated
def k_medoids_embedding(args, h):
clusters, radius, distortion = clustered(h.string_b, 1024 * 8, 10)
radius_list = list(radius.values())
for p in [0, 10, 20, 30, 50, 60, 70, 80, 90, 100]:
print("p =", p, "% : ", np.percentile(radius_list, p))
for T in [1, 2, 5, 10]:
with Pool(cpu_count()) as pool:
search_res = list(
pool.imap(
search_radius,
((q, clusters, radius, T) for q in h.string_q),
)
)
print(
"T =",
T,
" search_radius evaluated rate :",
np.mean(search_res) / h.nb,
)
search_res = list(
pool.imap(
search_distortion,
((q, clusters, distortion, T) for q in h.string_q),
)
)
print(
"T =",
T,
" search_distortion evaluated rate :",
np.mean(search_res) / h.nb,
)