teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

追記

2017/11/24 06:38

投稿

mkgrei
mkgrei

スコア8562

answer CHANGED
@@ -1,4 +1,98 @@
1
1
  仮説ですが、オリジナルのコードはPython2.xでmapがlistを返していたのです。
2
2
  そして今、Python3.xを使っているせいで、mapがイテレータを返してきているので、matplotlibは困惑している状況です。
3
3
 
4
- すべてのmap(...)をlist(map(...))のようにlistで囲むと解決する可能性が高いです。
4
+ すべてのmap(...)をlist(map(...))のようにlistで囲むと解決する可能性が高いです。
5
+
6
+ ---
7
+ 追記
8
+ 古いlambda関数の挙動を初めて知りました。
9
+ 簡潔な解決法はpython2.xを使用することでした。
10
+
11
+ 以下にpython3.xで動くコードを載せます。
12
+ ```python
13
+ import numpy as np
14
+ import matplotlib.pyplot as plt
15
+ import matplotlib.cm as cm
16
+ import scipy.spatial.distance as dist
17
+ %matplotlib inline
18
+
19
+ def label_cluster_num(means, mesh_points, metrics):
20
+ def label(point):
21
+ cluster_label = np.argmin(list(map(lambda mean: metrics(mean, point), means)))
22
+ return point, cluster_label
23
+ return list(map(label, mesh_points))
24
+
25
+ c_means = np.array([[1, 2], [-3, 4], [-5, -6], [7, -8]])
26
+ xs = np.linspace(-10, 10, 100)
27
+ ys = np.linspace(-10, 10, 100)
28
+ xx, yy = np.meshgrid(xs, ys)
29
+ mesh_points = np.c_[xx.ravel(), yy.ravel()]
30
+
31
+ def show_volonoi_with_metrics(metrics):
32
+ labeled_mesh_points = label_cluster_num(c_means, mesh_points, metrics=metrics)
33
+ plt.figure()
34
+ fig, ax = plt.subplots()
35
+
36
+ ax.set_aspect('equal')
37
+ ax.grid(True, which='both')
38
+ ax.axhline(y=0, color='k')
39
+ ax.axvline(x=0, color='k')
40
+ ax.set_xlim([-10, 10])
41
+ ax.set_ylim([-10, 10])
42
+
43
+ for i in range(0, len(c_means)):
44
+ cluster_points = list(map(lambda p: p[0], filter(lambda p: p[1] == i, labeled_mesh_points)))
45
+ xs = list(map(lambda p: p[0], cluster_points))
46
+ ys = list(map(lambda p: p[1], cluster_points))
47
+ ax.scatter(xs, ys, color=cm.prism(i / float(len(c_means))), marker='.')
48
+
49
+ ax.scatter(list(map(lambda p: p[0], c_means)), list(map(lambda p: p[1], c_means)), color="g", marker='o')
50
+
51
+ plt.show()
52
+
53
+ show_volonoi_with_metrics(dist.euclidean)
54
+ ```
55
+
56
+ 読みやすいバージョン
57
+ ```python
58
+ import numpy as np
59
+ import matplotlib.pyplot as plt
60
+ import matplotlib.cm as cm
61
+ import scipy.spatial.distance as dist
62
+ %matplotlib inline
63
+
64
+ def label_cluster_num(means, mesh_points, metrics):
65
+ def label(point):
66
+ cluster_label = np.argmin([metrics(p, point) for p in means])
67
+ return cluster_label
68
+ return np.array([label(p) for p in mesh_points])
69
+
70
+ c_means = np.array([[1, 2], [-3, 4], [-5, -6], [7, -8]])
71
+ xs = np.linspace(-10, 10, 100)
72
+ ys = np.linspace(-10, 10, 100)
73
+ xx, yy = np.meshgrid(xs, ys)
74
+ mesh_points = np.c_[xx.ravel(), yy.ravel()]
75
+
76
+ def show_volonoi_with_metrics(metrics):
77
+ labels = label_cluster_num(c_means, mesh_points, metrics=metrics)
78
+ fig, ax = plt.subplots()
79
+
80
+ ax.set_aspect('equal')
81
+ ax.grid(True, which='both')
82
+ ax.axhline(y=0, color='k')
83
+ ax.axvline(x=0, color='k')
84
+ ax.set_xlim([-10, 10])
85
+ ax.set_ylim([-10, 10])
86
+
87
+ for i in range(0, len(c_means)):
88
+ cluster_points = mesh_points[labels==i]
89
+ xs = cluster_points[:, 0]
90
+ ys = cluster_points[:, 1]
91
+ ax.scatter(xs, ys, color=cm.prism(i / float(len(c_means))), marker='.')
92
+
93
+ ax.scatter(c_means[:, 0], c_means[:, 0], color="g", marker='o')
94
+
95
+ plt.show()
96
+
97
+ show_volonoi_with_metrics(dist.euclidean)
98
+ ```