度々すみません
これの最後のshow_volonoi_with_metrics(dist.euclidean)でエラーが出てしまいます。
なにか良い方法はありますでしょうか
以下エラーです
RuntimeError Traceback (most recent call last)
<ipython-input-62-670a63d7ce7e> in <module>()
----> 1 show_volonoi_with_metrics(dist.euclidean)
<ipython-input-61-e1fd4304de1a> in show_volonoi_with_metrics(metrics)
15 xs = map(lambda p: p[0], cluster_points)
16 ys = map(lambda p: p[1], cluster_points)
---> 17 ax.scatter(xs, ys, color=cm.prism(i / float(len(c_means))), marker='.')
18
19 ax.scatter(map(lambda p: p[0], c_means), map(lambda p: p[1], c_means), color="g", marker='o')
~/anaconda3/envs/styletrans/lib/python3.6/site-packages/matplotlib/init.py in inner(ax, *args, **kwargs)
1708 warnings.warn(msg % (label_namer, func.name),
1709 RuntimeWarning, stacklevel=2)
-> 1710 return func(ax, *args, **kwargs)
1711 pre_doc = inner.doc
1712 if pre_doc is None:
~/anaconda3/envs/styletrans/lib/python3.6/site-packages/matplotlib/axes/_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
4007 edgecolors = 'face'
4008
-> 4009 self._process_unit_info(xdata=x, ydata=y, kwargs=kwargs)
4010 x = self.convert_xunits(x)
4011 y = self.convert_yunits(y)
~/anaconda3/envs/styletrans/lib/python3.6/site-packages/matplotlib/axes/_base.py in _process_unit_info(self, xdata, ydata, kwargs)
1967 # we only need to update if there is nothing set yet.
1968 if not self.xaxis.have_units():
-> 1969 self.xaxis.update_units(xdata)
1970
1971 if ydata is not None:
~/anaconda3/envs/styletrans/lib/python3.6/site-packages/matplotlib/axis.py in update_units(self, data)
1430 """
1431
-> 1432 converter = munits.registry.get_converter(data)
1433 if converter is None:
1434 return False
~/anaconda3/envs/styletrans/lib/python3.6/site-packages/matplotlib/units.py in get_converter(self, x)
160 if converter is None:
161 try:
--> 162 thisx = safe_first_element(x)
163 except (TypeError, StopIteration):
164 pass
~/anaconda3/envs/styletrans/lib/python3.6/site-packages/matplotlib/cbook/init.py in safe_first_element(obj)
2310 except TypeError:
2311 pass
-> 2312 raise RuntimeError("matplotlib does not support generators "
2313 "as input")
2314 return next(iter(obj))
RuntimeError: matplotlib does not support generators as input
以下コードです
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import scipy.spatial.distance as dist
%matplotlib inline
def label_cluster_num(means, mesh_points, metrics):
def label(point):
cluster_label = np.argmin(map(lambda mean: metrics(mean, point), means))
return point, cluster_label
return map(label, mesh_points)
c_means = np.array([[1, 2], [-3, 4], [-5, -6], [7, -8]])
xs = np.linspace(-10, 10, 100)
ys = np.linspace(-10, 10, 100)
xx, yy = np.meshgrid(xs, ys)
mesh_points = np.c_[xx.ravel(), yy.ravel()]
def show_volonoi_with_metrics(metrics):
labeled_mesh_points = label_cluster_num(c_means, mesh_points, metrics=metrics)
plt.figure()
fig, ax = plt.subplots()
ax.set_aspect('equal') ax.grid(True, which='both') ax.axhline(y=0, color='k') ax.axvline(x=0, color='k') ax.set_xlim([-10, 10]) ax.set_ylim([-10, 10]) for i in range(0, len(c_means)): cluster_points = map(lambda p, label: p, filter(lambda p, label: label == i, labeled_mesh_points)) xs = map(lambda p: p[0], cluster_points) ys = map(lambda p: p[1], cluster_points) ax.scatter(xs, ys, color=cm.prism(i / float(len(c_means))), marker='.') ax.scatter(map(lambda p: p[0], c_means), map(lambda p: p[1], c_means), color="g", marker='o') plt.show()
show_volonoi_with_metrics(dist.euclidean)
回答1件
あなたの回答
tips
プレビュー