注意:上面的损失函数用的是平方差损失函数。
以下是代码实现部分:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([[1, 3, 3],
[1, 4, 3],
[1, 1, 1],
[1, 0, 2]])
y = np.array([[1],[1], [-1], [-1]])
w = (np.random.random((3,1))-0.5) * 2 # 把初始换参数定位在-1到1之间
lr = 0.11
output = 0
def updata(x, w, y):
print()
output = np.sign(np.dot(x, w))
w_grad = lr * (x.T.dot(y-output)) / int(x.shape[0])
w = w + w_grad
return w
for i in range(100):
print(i)
w = updata(x, w, y)
print(np.sign(np.dot(x,w)))
if (np.sign(np.dot(x, w)) == y).all():
print("Finish")
break
k = -w[1] / w[2]
d = -w[0] / w[2]
xdata = (0, 5)
plt.figure()
plt.plot(xdata, xdata*k+d, 'r')
plt.scatter(x[:2, 1:2],x[:2, 2:3], c='b')
plt.scatter(x[2:4, 1:2],x[2:4, 2:3], c='r')
plt.legend()