-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspect_agglomerative_clustering.py
60 lines (50 loc) · 1.77 KB
/
inspect_agglomerative_clustering.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
from agglomerative_clustering import AgglomerativeClustering
from l_method import agglomerative_l_method
import numpy as np
from util import get_cmap
import matplotlib.pyplot as plt
from itertools import product
def uniform(count):
return np.random.uniform(0, 1, (count, 2))
def normal(center, size, count):
x, y = center
X = np.random.normal(x, size, count)
Y = np.random.normal(y, size, count)
return np.array(list(zip(X, Y)))
def plot(ax, X, **kwargs):
x1, x2 = list(zip(*X))
ax.scatter(x1, x2, **kwargs)
distributions = {
'uniform': uniform(1000),
'normal': normal((0.5, 0.5), 0.15, 1000),
'many_normal': np.concatenate((
normal((0.2, 0.8), 0.05, 100),
normal((0.8, 0.8), 0.05, 100),
normal((0.2, 0.2), 0.05, 100),
normal((0.8, 0.2), 0.05, 100),
normal((0.35, 0.55), 0.05, 100),
normal((0.45, 0.45), 0.05, 100),
normal((0.55, 0.55), 0.05, 100),
normal((0.65, 0.45), 0.05, 100),
))
}
methods = [
'ward', 'average', 'complete', 'single'
]
def l_method(ax, X, method):
l_method = agglomerative_l_method(X, method=method)
suggest_n = len(l_method.cluster_centers_)
cmap = get_cmap(suggest_n + 1)
for label in range(suggest_n):
XX = list(map(lambda xy: xy[0],
filter(lambda xy: xy[1] == label,
zip(X, l_method.labels_))))
plot(ax, XX, c=cmap(label), edgecolors='none')
fig, axes = plt.subplots(len(methods), len(distributions))
for (i, method), (j, (dist_name, X)) in product(enumerate(methods),
enumerate(distributions.items())):
plt.sca(axes[i][j])
plt.title(dist_name + ' ' + method)
plt.axis('off')
l_method(axes[i][j], X, method)
plt.show()