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

質問編集履歴

4

ソースコードのシンタックスハイライト

2018/09/09 14:16

投稿

cdyuna
cdyuna

スコア9

title CHANGED
File without changes
body CHANGED
@@ -5,7 +5,7 @@
5
5
  本のコードをそのまま打ち込んでいるため、全体的にまだ意味が分かっておらず、どこを直すべきなのかもよく分かりません、、
6
6
 
7
7
  ### 発生している問題・エラーメッセージ
8
- ---------------------------------------------------------------------------
8
+ ```---------------------------------------------------------------------------
9
9
  TypeError Traceback (most recent call last)
10
10
  ~\Downloads\anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
11
11
  339 pass
@@ -101,8 +101,12 @@
101
101
  TypeError: len() of unsized object
102
102
 
103
103
  <Figure size 684x288 with 2 Axes>
104
+ ```ここに言語を入力
105
+ コード
106
+ ```
104
107
 
105
108
  ### 該当のソースコード
109
+
106
110
  import numpy as np
107
111
  import matplotlib.pyplot as plt
108
112
  %matplotlib inline
@@ -114,18 +118,16 @@
114
118
  X = 5 + 25*np.random.rand(X_n)
115
119
  Prm_c = [170, 108, 0.2]
116
120
  T = Prm_c[0] - Prm_c[1]*np.exp(-Prm_c[2]*X)\
117
- + 4*np.random.randn(X_n)
121
+ + 4*np.random.randn(X_n)
118
122
  np.savez('ch5_data.npz', X=X, X_min=X_min, X_max=X_max, X_n=X_n, T=T)
119
123
 
120
-
121
124
  from mpl_toolkits.mplot3d import Axes3D
122
125
 
123
126
  def mse_line(x, t, w):
124
- y = w[0]*x + w[1]
127
+ y = w[0]*x + w[1]
125
- mse = np.mean((y - t)**2)
128
+ mse = np.mean((y - t)**2)
126
- return mse
129
+ return mse
127
130
 
128
-
129
131
  xn = 100
130
132
  w0_range = [-25, 25]
131
133
  w1_range = [120, 170]
@@ -134,26 +136,28 @@
134
136
  xx0, xx1 = np.meshgrid(x0, x1)
135
137
  J = np.zeros((len(x0), len(x1)))
136
138
  for i0 in range(xn):
137
- for i1 in range(xn):
139
+ for i1 in range(xn):
138
- J[i1,i0] = mse_line(X, T, (x0[i0], x1[i1]))
140
+ J[i1,i0] = mse_line(X, T, (x0[i0], x1[i1]))
139
-
141
+
140
142
  plt.figure(figsize=(9.5, 4))
141
143
  plt.subplots_adjust(wspace=0.5)
142
144
 
143
145
  ax = plt.subplot(1, 2, 1, projection='3d')
144
146
  ax.plot_surface(xx0, xx1, J, rstride=10, cstride=10, alpha=0.3,
145
- color='blue', edgecolor='black')
147
+ color='blue', edgecolor='black')
146
148
  ax.set_xticks([-20, 0, 20])
147
149
  ax.set_yticks({120, 140, 160})
148
150
  ax.view_init(20, -60)
149
151
 
150
152
  plt.subplot(1,2,2)
151
153
  cont = plt.contour(xx0, xx1, J, 30, colors='black',
152
- levels=[100,1000,10000,100000])
154
+ levels=[100,1000,10000,100000])
153
155
  cont.clabel(fmt='%1.0f', fontsize=8)
154
156
  plt.grid(True)
155
157
  plt.show()
156
-
158
+ ```ここに言語を入力
159
+ コード
160
+ ```
157
161
  ### 試行錯誤したこと
158
162
    numpy,matplotlibがimportされているか確認しました。また、figsizeなどの数値を変えたりしてみましたが、駄目でした。
159
163
 

3

エラーメッセージ、ソースコードの補足

2018/09/09 14:16

投稿

cdyuna
cdyuna

スコア9

title CHANGED
File without changes
body CHANGED
@@ -3,12 +3,129 @@
3
3
  「Pythonで動かして学ぶ!新しい機械学習の教科書」を使っていて、p150と同じコードです。
4
4
 
5
5
  本のコードをそのまま打ち込んでいるため、全体的にまだ意味が分かっておらず、どこを直すべきなのかもよく分かりません、、
6
+
6
7
  ### 発生している問題・エラーメッセージ
8
+ ---------------------------------------------------------------------------
9
+ TypeError Traceback (most recent call last)
10
+ ~\Downloads\anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
11
+ 339 pass
12
+ 340 else:
13
+ --> 341 return printer(obj)
14
+ 342 # Finally look for special method names
15
+ 343 method = get_real_method(obj, self.print_method)
16
+
17
+ ~\Downloads\anaconda3\lib\site-packages\IPython\core\pylabtools.py in <lambda>(fig)
18
+ 239
19
+ 240 if 'png' in formats:
20
+ --> 241 png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
21
+ 242 if 'retina' in formats or 'png2x' in formats:
22
+ 243 png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))
23
+
24
+ ~\Downloads\anaconda3\lib\site-packages\IPython\core\pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
25
+ 123
26
+ 124 bytes_io = BytesIO()
27
+ --> 125 fig.canvas.print_figure(bytes_io, **kw)
28
+ 126 data = bytes_io.getvalue()
29
+ 127 if fmt == 'svg':
30
+
31
+ ~\Downloads\anaconda3\lib\site-packages\matplotlib\backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
32
+ 2210 orientation=orientation,
33
+ 2211 dryrun=True,
34
+ -> 2212 **kwargs)
35
+ 2213 renderer = self.figure._cachedRenderer
36
+ 2214 bbox_inches = self.figure.get_tightbbox(renderer)
37
+
38
+ ~\Downloads\anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py in print_png(self, filename_or_obj, *args, **kwargs)
39
+ 511
40
+ 512 def print_png(self, filename_or_obj, *args, **kwargs):
41
+ --> 513 FigureCanvasAgg.draw(self)
42
+ 514 renderer = self.get_renderer()
43
+ 515 original_dpi = renderer.dpi
44
+
45
+ ~\Downloads\anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py in draw(self)
46
+ 431 # if toolbar:
47
+ 432 # toolbar.set_cursor(cursors.WAIT)
48
+ --> 433 self.figure.draw(self.renderer)
49
+ 434 # A GUI class may be need to update a window using this draw, so
50
+ 435 # don't forget to call the superclass.
51
+
52
+ ~\Downloads\anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
53
+ 53 renderer.start_filter()
54
+ 54
55
+ ---> 55 return draw(artist, renderer, *args, **kwargs)
56
+ 56 finally:
57
+ 57 if artist.get_agg_filter() is not None:
58
+
59
+ ~\Downloads\anaconda3\lib\site-packages\matplotlib\figure.py in draw(self, renderer)
60
+ 1473
61
+ 1474 mimage._draw_list_compositing_images(
62
+ -> 1475 renderer, self, artists, self.suppressComposite)
63
+ 1476
64
+ 1477 renderer.close_group('figure')
65
+
66
+ ~\Downloads\anaconda3\lib\site-packages\matplotlib\image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
67
+ 139 if not_composite or not has_images:
68
+ 140 for a in artists:
69
+ --> 141 a.draw(renderer)
70
+ 142 else:
71
+ 143 # Composite any adjacent images together
72
+
73
+ ~\Downloads\anaconda3\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py in draw(self, renderer)
74
+ 296 # Then axes
75
+ 297 for ax in axes:
76
+ --> 298 ax.draw(renderer)
77
+ 299
78
+ 300 # Then rest
79
+
80
+ ~\Downloads\anaconda3\lib\site-packages\mpl_toolkits\mplot3d\axis3d.py in draw(self, renderer)
81
+ 228
82
+ 229 # code from XAxis
83
+ --> 230 majorTicks = self.get_major_ticks()
84
+ 231 majorLocs = self.major.locator()
85
+ 232
86
+
87
+ ~\Downloads\anaconda3\lib\site-packages\mpl_toolkits\mplot3d\axis3d.py in get_major_ticks(self, numticks)
88
+ 146
89
+ 147 def get_major_ticks(self, numticks=None):
90
+ --> 148 ticks = maxis.XAxis.get_major_ticks(self, numticks)
91
+ 149 for t in ticks:
92
+ 150 t.tick1line.set_transform(self.axes.transData)
93
+
94
+ ~\Downloads\anaconda3\lib\site-packages\matplotlib\axis.py in get_major_ticks(self, numticks)
95
+ 1394 'get the tick instances; grow as necessary'
96
+ 1395 if numticks is None:
97
+ -> 1396 numticks = len(self.get_major_locator()())
98
+ 1397
99
+ 1398 while len(self.majorTicks) < numticks:
100
+
7
101
  TypeError: len() of unsized object
8
102
 
9
103
  <Figure size 684x288 with 2 Axes>
10
104
 
11
105
  ### 該当のソースコード
106
+ import numpy as np
107
+ import matplotlib.pyplot as plt
108
+ %matplotlib inline
109
+
110
+ np.random.seed(seed=1)
111
+ X_min = 4
112
+ X_max = 30
113
+ X_n = 16
114
+ X = 5 + 25*np.random.rand(X_n)
115
+ Prm_c = [170, 108, 0.2]
116
+ T = Prm_c[0] - Prm_c[1]*np.exp(-Prm_c[2]*X)\
117
+ + 4*np.random.randn(X_n)
118
+ np.savez('ch5_data.npz', X=X, X_min=X_min, X_max=X_max, X_n=X_n, T=T)
119
+
120
+
121
+ from mpl_toolkits.mplot3d import Axes3D
122
+
123
+ def mse_line(x, t, w):
124
+ y = w[0]*x + w[1]
125
+ mse = np.mean((y - t)**2)
126
+ return mse
127
+
128
+
12
129
  xn = 100
13
130
  w0_range = [-25, 25]
14
131
  w1_range = [120, 170]
@@ -30,6 +147,13 @@
30
147
  ax.set_yticks({120, 140, 160})
31
148
  ax.view_init(20, -60)
32
149
 
150
+ plt.subplot(1,2,2)
151
+ cont = plt.contour(xx0, xx1, J, 30, colors='black',
152
+ levels=[100,1000,10000,100000])
153
+ cont.clabel(fmt='%1.0f', fontsize=8)
154
+ plt.grid(True)
155
+ plt.show()
156
+
33
157
  ### 試行錯誤したこと
34
158
    numpy,matplotlibがimportされているか確認しました。また、figsizeなどの数値を変えたりしてみましたが、駄目でした。
35
159
 

2

試行錯誤したことの追加

2018/09/09 11:06

投稿

cdyuna
cdyuna

スコア9

title CHANGED
File without changes
body CHANGED
@@ -30,13 +30,12 @@
30
30
  ax.set_yticks({120, 140, 160})
31
31
  ax.view_init(20, -60)
32
32
 
33
- ```こに言語名を入力
33
+ ### 試行錯誤した
34
- ソースコード
34
+   numpy,matplotlibがimportされているか確認しました。また、figsizeなどの数値を変えたりしてみましたが、駄目でした。
35
- ```
36
35
 
37
-
38
36
  ### 補足情報(FW/ツールのバージョンなど)
39
37
 
40
38
  windows 8.1 です。
39
+ Jupyter Notebookを使っています。
41
40
 
42
41
  初心者すぎて色々と申し訳ありません、、分かる方いらっしゃいましたらよろしくお願いいたします。

1

質問の題名、内容を変更しました。

2018/09/08 04:01

投稿

cdyuna
cdyuna

スコア9

title CHANGED
@@ -1,1 +1,1 @@
1
- 「Pythonで動かして学ぶ!新しい機械学習教科書」p150が分かりません、、
1
+ TypeError: len() of unsized object エラー正し方が分かりません
body CHANGED
@@ -1,5 +1,6 @@
1
1
  ### 前提・実現したいこと
2
- w空間における平均二乗誤差を表示したい
2
+ w空間における平均二乗誤差を表示したいのですが、エラーが出てしまいます。
3
+ 「Pythonで動かして学ぶ!新しい機械学習の教科書」を使っていて、p150と同じコードです。
3
4
 
4
5
  本のコードをそのまま打ち込んでいるため、全体的にまだ意味が分かっておらず、どこを直すべきなのかもよく分かりません、、
5
6
  ### 発生している問題・エラーメッセージ