模式识别作业

  1. 以下述两类模式为样本,用感知器算法求判别函数:ω1:{(0 0 0)t,(1 0 0)t,(1 0 1)t,(1 1 0)t}; ω2:{(0 0 1)t,(0 1 1)t,(0 1 0)t,(1 1 1)t}.且令W(1)=(-1 –2 –2 0)t, C=1.

  2. 画出上题所给的二类样本,及所求的判决界面。

  3. 用LMSE算法对题1所给的两样本求判别函数 (可取C=1或C=2) 。

  4. 用势函数算法对题1所给的两样本求判别函数。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

np1 = np.array([[0, 0, 0], [1, 0, 0], [1, 0, 1], [1, 1, 0]])
np2 = np.array([[0, 0, 1], [0, 1, 1], [0, 1, 0], [1, 1, 1]])

new_np1 = np.c_[np1, np.array([1, 1, 1, 1])]
new_np2 = np.c_[np2, np.array([1, 1, 1, 1])]

# print(new_np1)
# print(new_np2)
C = 1
args = [[0, 0, 0, 1]]


def question1():
w = dict()
w[1] = np.array([-1, -2, -2, 0])
change_w = list()

i = 1
error = 1
while error != 0:
error = 0
for row in new_np1:
# 在new_np1中的计算结果必须>0,惩罚为+C*row
# if (w[i] * row).sum() > 0:
if w[i].dot(row) > 0:
w[i + 1] = w[i]
else:
w[i + 1] = w[i] + C * row
error += 1
print("the new w[%s] is :%s" % (i + 1, str(w[i + 1])))
change_w.append(w[i + 1])

i += 1

for row in new_np2:
# 在new_np1中的计算结果必须<0,惩罚为-C*row

# if (w[i] * row).sum() < 0:
if w[i].dot(row) < 0:
w[i + 1] = w[i]
else:
w[i + 1] = w[i] - C * row
error += 1
print("the new w[%s] is :%s" % (i + 1, str(w[i + 1])))
change_w.append(w[i + 1])

i += 1
print(w)
return change_w


def question2(w):
for np_w in w[-1:]:
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(np.array([0, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 1, 0]), c='red', marker='o')
ax.scatter(np.array([0, 0, 0, 1]), np.array([0, 1, 1, 1]), np.array([1, 1, 0, 1]), c='blue', marker='^')
print(np_w)
# 生成[0,2] 间隔0.5的数列,间隔越小,曲面越平滑
X = np.arange(0, 2, 0.5)
Y = np.arange(0, 2, 0.5)
# 原为F(x)= ax1+bx2+cx3+d,x3作为z放到左边,右边全为负且需要除以参数c
X_arg = np_w[0] / np_w[2]
Y_arg = np_w[1] / np_w[2]
H = np_w[3] / np_w[2]

# 格点矩阵
X, Y = np.meshgrid(X, Y)
Z = - X * X_arg - Y * Y_arg - H
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow')

ax.set_xlabel('X Label')

ax.set_ylabel('Y Label')

ax.set_zlabel('Z Label')
plt.show()


def question3():
C = 2
lmse_np = np.concatenate((new_np1, -new_np2), axis=0)
lmse_pinv_np = np.linalg.pinv(lmse_np)
b = dict()
print(lmse_pinv_np)
b[1] = np.array([1, 1, 1, 1, 1, 1, 1, 1])
w = dict()
w[1] = lmse_pinv_np @ b[1]
e = dict()
e[1] = lmse_np @ w[1] - b[1]
print(e[1])
error = 1
i = 1
# 向量的模 np.sum(vector**2)**0.5
while error != 0:
if all(e[i] <= 0.01):
error = 0
else:
b[i + 1] = b[i] + C * (e[i] + abs(e[i]))
w[i + 1] = lmse_pinv_np @ b[i + 1]
e[i + 1] = lmse_np @ w[i + 1] - b[i + 1]
# print(e[i])
print("b[%s]:%s" % (i, b[i]))
print("w[%s]:%s" % (i, w[i]))
print("e[%s]:%s" % (i, e[i]))

i += 1
# np.around()四舍五入取近似值
print("判别函数为:" + str(np.around(w[i])))


def f(x, y, z, args=args):
sum = 0
for l in args:
sum += l[3] * np.exp(-((x - l[0]) ** 2 + (y - l[1]) ** 2 + (z - l[2]) ** 2))
return sum


def plot_implicit(fn, bbox=(-1, 1)):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
''' create a plot of an implicit function
fn ...implicit function (plot where fn==0)
bbox ..the x,y,and z limits of plotted interval'''
xmin, xmax, ymin, ymax, zmin, zmax = bbox * 3
A = np.linspace(xmin, xmax, 100) # resolution of the contour
B = np.linspace(xmin, xmax, 15) # number of slices
A1, A2 = np.meshgrid(A, A) # grid on which the contour is plotted

ax.scatter(np.array([0, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 1, 0]), c='red', marker='o')
ax.scatter(np.array([0, 0, 0, 1]), np.array([0, 1, 1, 1]), np.array([1, 1, 0, 1]), c='blue', marker='^')

for z in B: # plot contours in the XY plane
X, Y = A1, A2
Z = fn(X, Y, z)
cset = ax.contour(X, Y, Z + z, [z], zdir='z')
# [z] defines the only level to plot for this contour for this value of z

for y in B: # plot contours in the XZ plane
X, Z = A1, A2
Y = fn(X, y, Z)
cset = ax.contour(X, Y + y, Z, [y], zdir='y')

for x in B: # plot contours in the YZ plane
Y, Z = A1, A2
X = fn(x, Y, Z)
cset = ax.contour(X + x, Y, Z, [x], zdir='x')

# must set plot limits because the contour will likely extend
# way beyond the displayed level. Otherwise matplotlib extends the plot limits
# to encompass all values in the contour.
ax.set_zlim3d(zmin, zmax)
ax.set_xlim3d(xmin, xmax)
ax.set_ylim3d(ymin, ymax)

plt.show()


def question4():
error = 1
while error != 0:
error = 0
for f_arg in np1:
if f(f_arg[0], f_arg[1], f_arg[2]) > 0:
continue
else:
args.append([f_arg[0], f_arg[1], f_arg[2], 1])
print("修正:" + str(args))
error = 1
for f_arg in np2:
if f(f_arg[0], f_arg[1], f_arg[2]) < 0:
continue
else:
args.append([f_arg[0], f_arg[1], f_arg[2], -1])
print("修正:" + str(args))
error = 1

plot_implicit(f, bbox=(-1, 1))


if __name__ == '__main__':
list_for_q2 = question1()
question2(list_for_q2)
question3()
question4()

-------------本文结束 感谢您的阅读-------------