前提・実現したいこと
w空間における平均二乗誤差を表示したいのですが、エラーが出てしまいます。
「Pythonで動かして学ぶ!新しい機械学習の教科書」を使っていて、p150と同じコードです。
本のコードをそのまま打ち込んでいるため、全体的にまだ意味が分かっておらず、どこを直すべきなのかもよく分かりません、、
発生している問題・エラーメッセージ
TypeError Traceback (most recent call last) ~\Downloads\anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj) 339 pass 340 else: --> 341 return printer(obj) 342 # Finally look for special method names 343 method = get_real_method(obj, self.print_method) ~\Downloads\anaconda3\lib\site-packages\IPython\core\pylabtools.py in <lambda>(fig) 239 240 if 'png' in formats: --> 241 png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs)) 242 if 'retina' in formats or 'png2x' in formats: 243 png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs)) ~\Downloads\anaconda3\lib\site-packages\IPython\core\pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs) 123 124 bytes_io = BytesIO() --> 125 fig.canvas.print_figure(bytes_io, **kw) 126 data = bytes_io.getvalue() 127 if fmt == 'svg': ~\Downloads\anaconda3\lib\site-packages\matplotlib\backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs) 2210 orientation=orientation, 2211 dryrun=True, -> 2212 **kwargs) 2213 renderer = self.figure._cachedRenderer 2214 bbox_inches = self.figure.get_tightbbox(renderer) ~\Downloads\anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py in print_png(self, filename_or_obj, *args, **kwargs) 511 512 def print_png(self, filename_or_obj, *args, **kwargs): --> 513 FigureCanvasAgg.draw(self) 514 renderer = self.get_renderer() 515 original_dpi = renderer.dpi ~\Downloads\anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py in draw(self) 431 # if toolbar: 432 # toolbar.set_cursor(cursors.WAIT) --> 433 self.figure.draw(self.renderer) 434 # A GUI class may be need to update a window using this draw, so 435 # don't forget to call the superclass. ~\Downloads\anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs) 53 renderer.start_filter() 54 ---> 55 return draw(artist, renderer, *args, **kwargs) 56 finally: 57 if artist.get_agg_filter() is not None: ~\Downloads\anaconda3\lib\site-packages\matplotlib\figure.py in draw(self, renderer) 1473 1474 mimage._draw_list_compositing_images( -> 1475 renderer, self, artists, self.suppressComposite) 1476 1477 renderer.close_group('figure') ~\Downloads\anaconda3\lib\site-packages\matplotlib\image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite) 139 if not_composite or not has_images: 140 for a in artists: --> 141 a.draw(renderer) 142 else: 143 # Composite any adjacent images together ~\Downloads\anaconda3\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py in draw(self, renderer) 296 # Then axes 297 for ax in axes: --> 298 ax.draw(renderer) 299 300 # Then rest ~\Downloads\anaconda3\lib\site-packages\mpl_toolkits\mplot3d\axis3d.py in draw(self, renderer) 228 229 # code from XAxis --> 230 majorTicks = self.get_major_ticks() 231 majorLocs = self.major.locator() 232 ~\Downloads\anaconda3\lib\site-packages\mpl_toolkits\mplot3d\axis3d.py in get_major_ticks(self, numticks) 146 147 def get_major_ticks(self, numticks=None): --> 148 ticks = maxis.XAxis.get_major_ticks(self, numticks) 149 for t in ticks: 150 t.tick1line.set_transform(self.axes.transData) ~\Downloads\anaconda3\lib\site-packages\matplotlib\axis.py in get_major_ticks(self, numticks) 1394 'get the tick instances; grow as necessary' 1395 if numticks is None: -> 1396 numticks = len(self.get_major_locator()()) 1397 1398 while len(self.majorTicks) < numticks: TypeError: len() of unsized object <Figure size 684x288 with 2 Axes> ```ここに言語を入力 コード
該当のソースコード
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
np.random.seed(seed=1)
X_min = 4
X_max = 30
X_n = 16
X = 5 + 25*np.random.rand(X_n)
Prm_c = [170, 108, 0.2]
T = Prm_c[0] - Prm_c[1]*np.exp(-Prm_c[2]*X)\
- 4*np.random.randn(X_n)
np.savez('ch5_data.npz', X=X, X_min=X_min, X_max=X_max, X_n=X_n, T=T)
from mpl_toolkits.mplot3d import Axes3D
def mse_line(x, t, w):
y = w[0]*x + w[1]
mse = np.mean((y - t)**2)
return mse
xn = 100
w0_range = [-25, 25]
w1_range = [120, 170]
x0 = np.linspace(w0_range[0], w0_range[1], xn)
x1 = np.linspace(w1_range[0], w1_range[1], xn)
xx0, xx1 = np.meshgrid(x0, x1)
J = np.zeros((len(x0), len(x1)))
for i0 in range(xn):
for i1 in range(xn):
J[i1,i0] = mse_line(X, T, (x0[i0], x1[i1]))
plt.figure(figsize=(9.5, 4))
plt.subplots_adjust(wspace=0.5)
ax = plt.subplot(1, 2, 1, projection='3d')
ax.plot_surface(xx0, xx1, J, rstride=10, cstride=10, alpha=0.3,
color='blue', edgecolor='black')
ax.set_xticks([-20, 0, 20])
ax.set_yticks({120, 140, 160})
ax.view_init(20, -60)
plt.subplot(1,2,2)
cont = plt.contour(xx0, xx1, J, 30, colors='black',
levels=[100,1000,10000,100000])
cont.clabel(fmt='%1.0f', fontsize=8)
plt.grid(True)
plt.show()
コード
試行錯誤したこと
numpy,matplotlibがimportされているか確認しました。また、figsizeなどの数値を変えたりしてみましたが、駄目でした。
補足情報(FW/ツールのバージョンなど)
windows 8.1 です。
Jupyter Notebookを使っています。
初心者すぎて色々と申し訳ありません、、分かる方いらっしゃいましたらよろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー