質問編集履歴

2

読みやすくしました。

2021/05/27 11:22

投稿

valley
valley

スコア0

test CHANGED
File without changes
test CHANGED
@@ -92,7 +92,9 @@
92
92
 
93
93
 
94
94
 
95
- ------------------------------------------------------------------------------------------------------
95
+ ----------------------------------------------------------------------------------------------------
96
+
97
+ ```python
96
98
 
97
99
  #!/usr/bin/env python3
98
100
 
@@ -455,3 +457,5 @@
455
457
  #
456
458
 
457
459
  plt.show()
460
+
461
+ ```

1

report7.py追加しました。お願いします

2021/05/27 11:22

投稿

valley
valley

スコア0

test CHANGED
File without changes
test CHANGED
@@ -89,3 +89,369 @@
89
89
  renderer.draw_markers(
90
90
 
91
91
  TypeError: must be real number, not str
92
+
93
+
94
+
95
+ ------------------------------------------------------------------------------------------------------
96
+
97
+ #!/usr/bin/env python3
98
+
99
+ #coding:utf-8
100
+
101
+ #
102
+
103
+ # パターンが2次元連続変数で
104
+
105
+ # 多変量正規分布(Multivariate Normal Distribution) でモデル化した場合の
106
+
107
+ # 識別境界面
108
+
109
+ #
110
+
111
+ from mpl_toolkits.mplot3d import Axes3D
112
+
113
+ import numpy as np
114
+
115
+ import matplotlib.pyplot as plt
116
+
117
+ import matplotlib.tri as tri
118
+
119
+ import matplotlib.cm as cm
120
+
121
+ import functools
122
+
123
+ import scipy.stats as st
124
+
125
+ import scipy.special as sp
126
+
127
+ import math
128
+
129
+ import sys
130
+
131
+ from mpl_toolkits.axes_grid1 import make_axes_locatable
132
+
133
+
134
+
135
+
136
+
137
+ class Norm2D:
138
+
139
+ """
140
+
141
+ 2変量正規分布 (2 dimensional normal distribution)
142
+
143
+ """
144
+
145
+ def __init__(self, X):
146
+
147
+ self.__X = np.copy(X)
148
+
149
+ self.__Mu = np.mean(self.__X, axis=0)
150
+
151
+ self.__Sigma = np.cov(self.__X, rowvar=False, bias=True)
152
+
153
+ self.__dim = len(self.__Mu)
154
+
155
+ self.__N = len(self.__X)
156
+
157
+
158
+
159
+ def get_N(self):
160
+
161
+ """ 学習データ数の取得 """
162
+
163
+ return self.__N
164
+
165
+
166
+
167
+ def get_X(self):
168
+
169
+ """ 学習データの取得 """
170
+
171
+ return self.__X
172
+
173
+
174
+
175
+ def get_param(self):
176
+
177
+ """ 正規分布パラメタの取得 """
178
+
179
+ return self.__Mu, self.__Sigma
180
+
181
+
182
+
183
+ def pdf(self, x):
184
+
185
+ """ 与えられた分布の(x)における確率密度値を求める"""
186
+
187
+ return st.multivariate_normal.pdf(x, mean=self.__Mu, cov=self.__Sigma)
188
+
189
+
190
+
191
+ def sampling(self, N):
192
+
193
+ """ 与えられた分布に従ってN点サンプリングする"""
194
+
195
+ return st.multivariate_normal.rvs(mean=self.__Mu, cov=self.__Sigma, size=N)
196
+
197
+
198
+
199
+ def this_likelihood(self, x):
200
+
201
+ """ パターンxが与えられた時の与えられた分布での尤度を求める """
202
+
203
+ L = 1.0
204
+
205
+ for n in range(len(x)):
206
+
207
+ L *= self.pdf(x[n])
208
+
209
+ return L
210
+
211
+
212
+
213
+ def this_log_likelihood(self, x):
214
+
215
+ """ パターンxが与えられた時の与えられた分布での対数尤度を求める """
216
+
217
+ logL = 0.0
218
+
219
+ for n in range(len(x)):
220
+
221
+ logL += log(self.pdf(x[n]))
222
+
223
+ return logL
224
+
225
+
226
+
227
+
228
+
229
+ # 例題2
230
+
231
+ # x(ω1) = {(0, 4√2), (4, 4√2), (2, 4√2-1), (2, 4√2+1)}
232
+
233
+ # x(ω2) = {(0, 0), (4, 0), (2, -1), (2, 1), (2(1-√3), 0), (2(1+√3), 0), (2, -√3), (2, √3)}
234
+
235
+ """
236
+
237
+ x1 = np.array([[0.0, 4.0*np.sqrt(2.0)],
238
+
239
+ [4.0, 4.0*np.sqrt(2.0)],
240
+
241
+ [2.0, 4.0*np.sqrt(2.0)-1.0],
242
+
243
+ [2.0, 4.0*np.sqrt(2.0)+1.0]])
244
+
245
+ x2 = np.array([[0.0, 0.0],
246
+
247
+ [4.0, 0.0],
248
+
249
+ [2.0,-1.0],
250
+
251
+ [2.0, 1.0],
252
+
253
+ [2.0*(1.0-np.sqrt(3.0)), 0],
254
+
255
+ [2.0*(1.0+np.sqrt(3.0)), 0],
256
+
257
+ [2.0,-np.sqrt(3.0)],
258
+
259
+ [2.0, np.sqrt(3.0)]])
260
+
261
+ # Set the drow range
262
+
263
+ x = y = np.arange(-2, 18, 0.1)
264
+
265
+ """
266
+
267
+
268
+
269
+ #
270
+
271
+ # 例題3
272
+
273
+ # x(ω1) = {(-2, 0), (0, 1), (0, -1), (2, 0)}
274
+
275
+ # x(ω2) = {(3, 2), (4, -1), (6, 1), (7, -2)}
276
+
277
+ x1 = np.array([[-2.0, 0.0],
278
+
279
+ [0.0, 1.0],
280
+
281
+ [0.0,-1.0],
282
+
283
+ [2.0, 0.0]])
284
+
285
+ x2 = np.array([[3.0, 2.0],
286
+
287
+ [4.0,-1.0],
288
+
289
+ [6.0, 1.0],
290
+
291
+ [7.0,-2.0]])
292
+
293
+ # Set the drow range
294
+
295
+ x = y = np.arange(-5, 10, 0.1)
296
+
297
+
298
+
299
+ #
300
+
301
+ # Create Distributions
302
+
303
+ #
304
+
305
+ dist1 = Norm2D(x1)
306
+
307
+ dist2 = Norm2D(x2)
308
+
309
+
310
+
311
+ #
312
+
313
+ # Show Mu and Sigma
314
+
315
+ #
316
+
317
+ Mu1, Sigma1 = dist1.get_param()
318
+
319
+ print('Mu1=', Mu1);
320
+
321
+ print('Sigma1=', Sigma1)
322
+
323
+
324
+
325
+ Mu2, Sigma2 = dist2.get_param()
326
+
327
+ print('Mu2=', Mu2)
328
+
329
+ print('Sigma2=', Sigma2)
330
+
331
+
332
+
333
+
334
+
335
+ #
336
+
337
+ # Create the drawing mesh
338
+
339
+ #
340
+
341
+ X, Y = np.meshgrid(x, y)
342
+
343
+ pos = np.dstack((X,Y))
344
+
345
+
346
+
347
+ #
348
+
349
+ # Calc. pdf
350
+
351
+ #
352
+
353
+ Z1 = dist1.pdf(pos)
354
+
355
+ Z2 = dist2.pdf(pos)
356
+
357
+
358
+
359
+ Z = np.log(np.fmax(Z1, Z2)) # Logarithmic expression
360
+
361
+ Zdiff = Z1 - Z2
362
+
363
+
364
+
365
+ maxZ = np.max(Z)
366
+
367
+ minZ = np.min(Z)
368
+
369
+ #print('max Z=', maxZ)
370
+
371
+ #print('min Z=', minZ)
372
+
373
+
374
+
375
+ minZ = -20 # Clip the lowest Z value
376
+
377
+
378
+
379
+
380
+
381
+ #
382
+
383
+ # Init. a graph
384
+
385
+ #
386
+
387
+ fig = plt.figure(figsize=(7,5))
388
+
389
+
390
+
391
+ ax = fig.add_subplot(111, aspect='equal')
392
+
393
+ ax.grid()
394
+
395
+ plt.xlabel('x')
396
+
397
+ plt.ylabel('y')
398
+
399
+
400
+
401
+
402
+
403
+ #
404
+
405
+ # draw the contour
406
+
407
+ #
408
+
409
+ levels = np.linspace(minZ, maxZ, 50)
410
+
411
+ cs = ax.contourf(X, Y, Z, levels=levels, cmap=cm.inferno, extend='both')
412
+
413
+
414
+
415
+ #
416
+
417
+ # draw the boundary
418
+
419
+ #
420
+
421
+ ax.contour(X, Y, Zdiff, levels=[-1.0e-300, 1.0e-300], colors='r', linestyles='-')
422
+
423
+
424
+
425
+ #
426
+
427
+ # data
428
+
429
+ #
430
+
431
+ ax.scatter(dist1.get_X()[:,0], dist1.get_X()[:,1], s=40, c='red', marker='o', alpha=0.5, linewidths='2')
432
+
433
+ ax.scatter(dist2.get_X()[:,0], dist2.get_X()[:,1], s=40, c='blue', marker='o', alpha=0.5, linewidths='2')
434
+
435
+
436
+
437
+ #
438
+
439
+ # draw a colorbar
440
+
441
+ #
442
+
443
+ divider = make_axes_locatable(ax) # create the same size divider as the contour graph
444
+
445
+ cax = divider.append_axes('right', size='5%', pad=0.1) # append 5% space to the right-end
446
+
447
+ fig.colorbar(cs, cax=cax, format='%.1f') # show a colorbar
448
+
449
+
450
+
451
+ #
452
+
453
+ # Draw
454
+
455
+ #
456
+
457
+ plt.show()