回答編集履歴
1
追記
test
CHANGED
@@ -5,3 +5,191 @@
|
|
5
5
|
|
6
6
|
|
7
7
|
すべてのmap(...)をlist(map(...))のようにlistで囲むと解決する可能性が高いです。
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
---
|
12
|
+
|
13
|
+
追記
|
14
|
+
|
15
|
+
古いlambda関数の挙動を初めて知りました。
|
16
|
+
|
17
|
+
簡潔な解決法はpython2.xを使用することでした。
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
以下にpython3.xで動くコードを載せます。
|
22
|
+
|
23
|
+
```python
|
24
|
+
|
25
|
+
import numpy as np
|
26
|
+
|
27
|
+
import matplotlib.pyplot as plt
|
28
|
+
|
29
|
+
import matplotlib.cm as cm
|
30
|
+
|
31
|
+
import scipy.spatial.distance as dist
|
32
|
+
|
33
|
+
%matplotlib inline
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
def label_cluster_num(means, mesh_points, metrics):
|
38
|
+
|
39
|
+
def label(point):
|
40
|
+
|
41
|
+
cluster_label = np.argmin(list(map(lambda mean: metrics(mean, point), means)))
|
42
|
+
|
43
|
+
return point, cluster_label
|
44
|
+
|
45
|
+
return list(map(label, mesh_points))
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
c_means = np.array([[1, 2], [-3, 4], [-5, -6], [7, -8]])
|
50
|
+
|
51
|
+
xs = np.linspace(-10, 10, 100)
|
52
|
+
|
53
|
+
ys = np.linspace(-10, 10, 100)
|
54
|
+
|
55
|
+
xx, yy = np.meshgrid(xs, ys)
|
56
|
+
|
57
|
+
mesh_points = np.c_[xx.ravel(), yy.ravel()]
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
def show_volonoi_with_metrics(metrics):
|
62
|
+
|
63
|
+
labeled_mesh_points = label_cluster_num(c_means, mesh_points, metrics=metrics)
|
64
|
+
|
65
|
+
plt.figure()
|
66
|
+
|
67
|
+
fig, ax = plt.subplots()
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
ax.set_aspect('equal')
|
72
|
+
|
73
|
+
ax.grid(True, which='both')
|
74
|
+
|
75
|
+
ax.axhline(y=0, color='k')
|
76
|
+
|
77
|
+
ax.axvline(x=0, color='k')
|
78
|
+
|
79
|
+
ax.set_xlim([-10, 10])
|
80
|
+
|
81
|
+
ax.set_ylim([-10, 10])
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
for i in range(0, len(c_means)):
|
86
|
+
|
87
|
+
cluster_points = list(map(lambda p: p[0], filter(lambda p: p[1] == i, labeled_mesh_points)))
|
88
|
+
|
89
|
+
xs = list(map(lambda p: p[0], cluster_points))
|
90
|
+
|
91
|
+
ys = list(map(lambda p: p[1], cluster_points))
|
92
|
+
|
93
|
+
ax.scatter(xs, ys, color=cm.prism(i / float(len(c_means))), marker='.')
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
ax.scatter(list(map(lambda p: p[0], c_means)), list(map(lambda p: p[1], c_means)), color="g", marker='o')
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
plt.show()
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
show_volonoi_with_metrics(dist.euclidean)
|
106
|
+
|
107
|
+
```
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
読みやすいバージョン
|
112
|
+
|
113
|
+
```python
|
114
|
+
|
115
|
+
import numpy as np
|
116
|
+
|
117
|
+
import matplotlib.pyplot as plt
|
118
|
+
|
119
|
+
import matplotlib.cm as cm
|
120
|
+
|
121
|
+
import scipy.spatial.distance as dist
|
122
|
+
|
123
|
+
%matplotlib inline
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
def label_cluster_num(means, mesh_points, metrics):
|
128
|
+
|
129
|
+
def label(point):
|
130
|
+
|
131
|
+
cluster_label = np.argmin([metrics(p, point) for p in means])
|
132
|
+
|
133
|
+
return cluster_label
|
134
|
+
|
135
|
+
return np.array([label(p) for p in mesh_points])
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
c_means = np.array([[1, 2], [-3, 4], [-5, -6], [7, -8]])
|
140
|
+
|
141
|
+
xs = np.linspace(-10, 10, 100)
|
142
|
+
|
143
|
+
ys = np.linspace(-10, 10, 100)
|
144
|
+
|
145
|
+
xx, yy = np.meshgrid(xs, ys)
|
146
|
+
|
147
|
+
mesh_points = np.c_[xx.ravel(), yy.ravel()]
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
def show_volonoi_with_metrics(metrics):
|
152
|
+
|
153
|
+
labels = label_cluster_num(c_means, mesh_points, metrics=metrics)
|
154
|
+
|
155
|
+
fig, ax = plt.subplots()
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
ax.set_aspect('equal')
|
160
|
+
|
161
|
+
ax.grid(True, which='both')
|
162
|
+
|
163
|
+
ax.axhline(y=0, color='k')
|
164
|
+
|
165
|
+
ax.axvline(x=0, color='k')
|
166
|
+
|
167
|
+
ax.set_xlim([-10, 10])
|
168
|
+
|
169
|
+
ax.set_ylim([-10, 10])
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
for i in range(0, len(c_means)):
|
174
|
+
|
175
|
+
cluster_points = mesh_points[labels==i]
|
176
|
+
|
177
|
+
xs = cluster_points[:, 0]
|
178
|
+
|
179
|
+
ys = cluster_points[:, 1]
|
180
|
+
|
181
|
+
ax.scatter(xs, ys, color=cm.prism(i / float(len(c_means))), marker='.')
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
ax.scatter(c_means[:, 0], c_means[:, 0], color="g", marker='o')
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
plt.show()
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
show_volonoi_with_metrics(dist.euclidean)
|
194
|
+
|
195
|
+
```
|