質問編集履歴
1
途中送信部分を訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -143,3 +143,191 @@
|
|
143
143
|
plt.show()
|
144
144
|
|
145
145
|
```
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
//追記
|
150
|
+
|
151
|
+
申し訳ございません途中で送信してしまいました.
|
152
|
+
|
153
|
+
続きを追記させていただきます.
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
上記のプログラムのteachers部分に図のようなコレクションを挿入したく下記のようなプログラムに書き直しました.
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
```python
|
164
|
+
|
165
|
+
import numpy as np
|
166
|
+
|
167
|
+
import csv
|
168
|
+
|
169
|
+
data = []
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
with open("data.csv","rb") as f:
|
174
|
+
|
175
|
+
reader = csv.reader(f)
|
176
|
+
|
177
|
+
header = next(reader)
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
for row in reader:
|
182
|
+
|
183
|
+
data.append(row)
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
from matplotlib import pyplot as plt
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
class SOM():
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
def __init__(self, teachers, N, seed=None):
|
196
|
+
|
197
|
+
self.teachers = np.array(teachers)
|
198
|
+
|
199
|
+
self.n_teacher = self.teachers.shape[0]
|
200
|
+
|
201
|
+
self.N = N
|
202
|
+
|
203
|
+
if not seed is None:
|
204
|
+
|
205
|
+
np.random.seed(seed)
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
x, y = np.meshgrid(range(self.N), range(self.N))
|
210
|
+
|
211
|
+
self.c = np.hstack((y.flatten()[:, np.newaxis],
|
212
|
+
|
213
|
+
x.flatten()[:, np.newaxis]))
|
214
|
+
|
215
|
+
self.nodes = np.random.rand(self.N*self.N,
|
216
|
+
|
217
|
+
self.teachers.shape[1])
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
def train(self):
|
222
|
+
|
223
|
+
for i, teacher in enumerate(self.teachers):
|
224
|
+
|
225
|
+
bmu = self._best_matching_unit(teacher)
|
226
|
+
|
227
|
+
d = np.linalg.norm(self.c - bmu, axis=1)
|
228
|
+
|
229
|
+
L = self._learning_ratio(i)
|
230
|
+
|
231
|
+
S = self._learning_radius(i, d)
|
232
|
+
|
233
|
+
self.nodes += L * S[:, np.newaxis] * (teacher - self.nodes)
|
234
|
+
|
235
|
+
return self.nodes
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
def _best_matching_unit(self, teacher):
|
240
|
+
|
241
|
+
#compute all norms (square)
|
242
|
+
|
243
|
+
norms = np.linalg.norm(self.nodes - teacher, axis=1)
|
244
|
+
|
245
|
+
bmu = np.argmin(norms) #argment with minimum element
|
246
|
+
|
247
|
+
return np.unravel_index(bmu,(self.N, self.N))
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
def _neighbourhood(self, t):#neighbourhood radious
|
252
|
+
|
253
|
+
halflife = float(self.n_teacher/4) #for testing
|
254
|
+
|
255
|
+
initial = float(self.N/2)
|
256
|
+
|
257
|
+
return initial*np.exp(-t/halflife)
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
def _learning_ratio(self, t):
|
262
|
+
|
263
|
+
halflife = float(self.n_teacher/4) #for testing
|
264
|
+
|
265
|
+
initial = 0.1
|
266
|
+
|
267
|
+
return initial*np.exp(-t/halflife)
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
def _learning_radius(self, t, d):
|
272
|
+
|
273
|
+
# d is distance from BMU
|
274
|
+
|
275
|
+
s = self._neighbourhood(t)
|
276
|
+
|
277
|
+
return np.exp(-d**2/(2*s**2))
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
N = 20
|
284
|
+
|
285
|
+
teachers = data
|
286
|
+
|
287
|
+
som = SOM(teachers, N=N, seed=10)
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
# Initial map
|
292
|
+
|
293
|
+
plt.imshow(som.nodes.reshape((N, N, 3)),interpolation='none')
|
294
|
+
|
295
|
+
plt.show()
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
# Train
|
300
|
+
|
301
|
+
som.train()
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
# Trained MAP
|
306
|
+
|
307
|
+
plt.imshow(som.nodes.reshape((N, N, 3)),
|
308
|
+
|
309
|
+
interpolation='none')
|
310
|
+
|
311
|
+
plt.show()
|
312
|
+
|
313
|
+
```
|
314
|
+
|
315
|
+
しかし下記のようなエラーがでてしまいます.
|
316
|
+
|
317
|
+
実際のコレクションは33×500の行列なので,(N, N, 3)部分を(N, N, 33)にしたりしてみましたがうまくいきませんでした.
|
318
|
+
|
319
|
+
```
|
320
|
+
|
321
|
+
Traceback (most recent call last):
|
322
|
+
|
323
|
+
File "soms.py", line 65, in <module>
|
324
|
+
|
325
|
+
plt.imshow(som.nodes.reshape((N, N, 3)),interpolation='none')
|
326
|
+
|
327
|
+
ValueError: cannot reshape array of size 13200 into shape (20,20,3)
|
328
|
+
|
329
|
+
```
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
python初学者のため,根本的な話なのかもしれませんがどうかご教授いただけると幸いです.
|