题目:道理都懂,题目为模式识别这门课的小作业,题目是有错的,(1,1,1,1)是(1,1,1)
代码:
import numpy as np
a = np.array([[0, 0],
[0, 1],
[1, 0],
[1, 1]], dtype=np.float64)
b = np.array([1, 1, 1, 1], dtype=np.float64)
w = np.array([[1],
[1],
[1]], dtype=np.float64)
w2 = np.array([[1, 1, 1]], dtype=np.float64)
x = np.column_stack((a, b))
x[2] = -x[2]
x[3] = -x[3]
x_true = np.matrix(x)
w_true = np.matrix(w)
w2_true = np.matrix(w2)
tag = 0
while tag < 4:
tag = 0
for i in range(4):
if int(x_true[i] * w_true) <= 0.:
w2_true = x_true[i] + w2_true
w_true = x_true[i].reshape(3, 1) + w_true
print(w2_true, i+1)
else:
tag += 1
print(w2_true, i+1)
print("the answer is: %f*x1 + %f*x2 = %f" % (w2_true[0, 0], w2_true[0, 1], w2_true[0, 2]))
#print(w2_true)
结果:嘤嘤嘤