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

質問編集履歴

2

読みやすくしました。

2021/05/27 11:22

投稿

valley
valley

スコア0

title CHANGED
File without changes
body CHANGED
@@ -45,7 +45,8 @@
45
45
  renderer.draw_markers(
46
46
  TypeError: must be real number, not str
47
47
 
48
- ------------------------------------------------------------------------------------------------------
48
+ ----------------------------------------------------------------------------------------------------
49
+ ```python
49
50
  #!/usr/bin/env python3
50
51
  #coding:utf-8
51
52
  #
@@ -226,4 +227,5 @@
226
227
  #
227
228
  # Draw
228
229
  #
229
- plt.show()
230
+ plt.show()
231
+ ```

1

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

2021/05/27 11:22

投稿

valley
valley

スコア0

title CHANGED
File without changes
body CHANGED
@@ -43,4 +43,187 @@
43
43
  return draw(artist, renderer, *args, **kwargs)
44
44
  File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/collections.py", line 406, in draw
45
45
  renderer.draw_markers(
46
- TypeError: must be real number, not str
46
+ TypeError: must be real number, not str
47
+
48
+ ------------------------------------------------------------------------------------------------------
49
+ #!/usr/bin/env python3
50
+ #coding:utf-8
51
+ #
52
+ # パターンが2次元連続変数で
53
+ # 多変量正規分布(Multivariate Normal Distribution) でモデル化した場合の
54
+ # 識別境界面
55
+ #
56
+ from mpl_toolkits.mplot3d import Axes3D
57
+ import numpy as np
58
+ import matplotlib.pyplot as plt
59
+ import matplotlib.tri as tri
60
+ import matplotlib.cm as cm
61
+ import functools
62
+ import scipy.stats as st
63
+ import scipy.special as sp
64
+ import math
65
+ import sys
66
+ from mpl_toolkits.axes_grid1 import make_axes_locatable
67
+
68
+
69
+ class Norm2D:
70
+ """
71
+ 2変量正規分布 (2 dimensional normal distribution)
72
+ """
73
+ def __init__(self, X):
74
+ self.__X = np.copy(X)
75
+ self.__Mu = np.mean(self.__X, axis=0)
76
+ self.__Sigma = np.cov(self.__X, rowvar=False, bias=True)
77
+ self.__dim = len(self.__Mu)
78
+ self.__N = len(self.__X)
79
+
80
+ def get_N(self):
81
+ """ 学習データ数の取得 """
82
+ return self.__N
83
+
84
+ def get_X(self):
85
+ """ 学習データの取得 """
86
+ return self.__X
87
+
88
+ def get_param(self):
89
+ """ 正規分布パラメタの取得 """
90
+ return self.__Mu, self.__Sigma
91
+
92
+ def pdf(self, x):
93
+ """ 与えられた分布の(x)における確率密度値を求める"""
94
+ return st.multivariate_normal.pdf(x, mean=self.__Mu, cov=self.__Sigma)
95
+
96
+ def sampling(self, N):
97
+ """ 与えられた分布に従ってN点サンプリングする"""
98
+ return st.multivariate_normal.rvs(mean=self.__Mu, cov=self.__Sigma, size=N)
99
+
100
+ def this_likelihood(self, x):
101
+ """ パターンxが与えられた時の与えられた分布での尤度を求める """
102
+ L = 1.0
103
+ for n in range(len(x)):
104
+ L *= self.pdf(x[n])
105
+ return L
106
+
107
+ def this_log_likelihood(self, x):
108
+ """ パターンxが与えられた時の与えられた分布での対数尤度を求める """
109
+ logL = 0.0
110
+ for n in range(len(x)):
111
+ logL += log(self.pdf(x[n]))
112
+ return logL
113
+
114
+
115
+ # 例題2
116
+ # x(ω1) = {(0, 4√2), (4, 4√2), (2, 4√2-1), (2, 4√2+1)}
117
+ # x(ω2) = {(0, 0), (4, 0), (2, -1), (2, 1), (2(1-√3), 0), (2(1+√3), 0), (2, -√3), (2, √3)}
118
+ """
119
+ x1 = np.array([[0.0, 4.0*np.sqrt(2.0)],
120
+ [4.0, 4.0*np.sqrt(2.0)],
121
+ [2.0, 4.0*np.sqrt(2.0)-1.0],
122
+ [2.0, 4.0*np.sqrt(2.0)+1.0]])
123
+ x2 = np.array([[0.0, 0.0],
124
+ [4.0, 0.0],
125
+ [2.0,-1.0],
126
+ [2.0, 1.0],
127
+ [2.0*(1.0-np.sqrt(3.0)), 0],
128
+ [2.0*(1.0+np.sqrt(3.0)), 0],
129
+ [2.0,-np.sqrt(3.0)],
130
+ [2.0, np.sqrt(3.0)]])
131
+ # Set the drow range
132
+ x = y = np.arange(-2, 18, 0.1)
133
+ """
134
+
135
+ #
136
+ # 例題3
137
+ # x(ω1) = {(-2, 0), (0, 1), (0, -1), (2, 0)}
138
+ # x(ω2) = {(3, 2), (4, -1), (6, 1), (7, -2)}
139
+ x1 = np.array([[-2.0, 0.0],
140
+ [0.0, 1.0],
141
+ [0.0,-1.0],
142
+ [2.0, 0.0]])
143
+ x2 = np.array([[3.0, 2.0],
144
+ [4.0,-1.0],
145
+ [6.0, 1.0],
146
+ [7.0,-2.0]])
147
+ # Set the drow range
148
+ x = y = np.arange(-5, 10, 0.1)
149
+
150
+ #
151
+ # Create Distributions
152
+ #
153
+ dist1 = Norm2D(x1)
154
+ dist2 = Norm2D(x2)
155
+
156
+ #
157
+ # Show Mu and Sigma
158
+ #
159
+ Mu1, Sigma1 = dist1.get_param()
160
+ print('Mu1=', Mu1);
161
+ print('Sigma1=', Sigma1)
162
+
163
+ Mu2, Sigma2 = dist2.get_param()
164
+ print('Mu2=', Mu2)
165
+ print('Sigma2=', Sigma2)
166
+
167
+
168
+ #
169
+ # Create the drawing mesh
170
+ #
171
+ X, Y = np.meshgrid(x, y)
172
+ pos = np.dstack((X,Y))
173
+
174
+ #
175
+ # Calc. pdf
176
+ #
177
+ Z1 = dist1.pdf(pos)
178
+ Z2 = dist2.pdf(pos)
179
+
180
+ Z = np.log(np.fmax(Z1, Z2)) # Logarithmic expression
181
+ Zdiff = Z1 - Z2
182
+
183
+ maxZ = np.max(Z)
184
+ minZ = np.min(Z)
185
+ #print('max Z=', maxZ)
186
+ #print('min Z=', minZ)
187
+
188
+ minZ = -20 # Clip the lowest Z value
189
+
190
+
191
+ #
192
+ # Init. a graph
193
+ #
194
+ fig = plt.figure(figsize=(7,5))
195
+
196
+ ax = fig.add_subplot(111, aspect='equal')
197
+ ax.grid()
198
+ plt.xlabel('x')
199
+ plt.ylabel('y')
200
+
201
+
202
+ #
203
+ # draw the contour
204
+ #
205
+ levels = np.linspace(minZ, maxZ, 50)
206
+ cs = ax.contourf(X, Y, Z, levels=levels, cmap=cm.inferno, extend='both')
207
+
208
+ #
209
+ # draw the boundary
210
+ #
211
+ ax.contour(X, Y, Zdiff, levels=[-1.0e-300, 1.0e-300], colors='r', linestyles='-')
212
+
213
+ #
214
+ # data
215
+ #
216
+ ax.scatter(dist1.get_X()[:,0], dist1.get_X()[:,1], s=40, c='red', marker='o', alpha=0.5, linewidths='2')
217
+ ax.scatter(dist2.get_X()[:,0], dist2.get_X()[:,1], s=40, c='blue', marker='o', alpha=0.5, linewidths='2')
218
+
219
+ #
220
+ # draw a colorbar
221
+ #
222
+ divider = make_axes_locatable(ax) # create the same size divider as the contour graph
223
+ cax = divider.append_axes('right', size='5%', pad=0.1) # append 5% space to the right-end
224
+ fig.colorbar(cs, cax=cax, format='%.1f') # show a colorbar
225
+
226
+ #
227
+ # Draw
228
+ #
229
+ plt.show()