理论知识是参考的《统计学习方法》
代码需要用到numpy和matplotlib.pyplot
import numpy as np
import matplotlib.pyplot as plt
def perceptron_v2(x,y,mu):
#omg与x维度相同
omg = np.zeros((1,x.shape[1]))
b = 0
finish_flag = False
while finish_flag == False:
finish_flag = True;
for i in range(len(y)):
#若有误分类点,则继续计算
if y[i] * (np.dot(omg, x[i]) + b) <= 0:
finish_flag = False
omg += mu * y[i] * x[i]
b += mu * y[i]
break
omg = omg[0]
#如果是二维数据,则将结果可视化
if x.shape[1] == 2:
for i in range(x.shape[0]):
#正实例点
if y[i] == 1:
plt.scatter(x[i,0],x[i,1],c = 'b')
#负实例点
else:
plt.scatter(x[i,0],x[i,1],c = 'r')
#超平面的在水平轴上的端点取x[:,]的最大值和最小值
x0 = np.array((x[:,0].min(),x[:,0].max()))
y0 = np.array(((-1 * b - omg[0] * x0[0]) / omg[1], (-1 * b - omg[0] * x0[1]) / omg[1]))
plt.plot(x0,y0,c='g')
return omg,b
测试
x = np.array([[3,3],[4,3],[1,1]])
y = np.array([1,1,-1])
mu = 1
perceptron_v2(x,y,mu)
结果
(array([1., 1.]), -3) #输出的omg和b的值
对偶形式
def perceptron_dual(x,y,mu):
g = np.empty([x.shape[0],x.shape[0]])
for i in range(x.shape[0]):
for j in range(x.shape[0]):
g[i,j] = np.dot(x[i],x[j])
alpha = np.zeros((1,x.shape[0]))
alpha = alpha[0]
b = 0
finish_flag = False
while finish_flag == False:
finish_flag = True;
for i in range(len(y)):
temp = 0
for j in range(len(y)):
temp += alpha[j] * y[j] * g[i,j]
if y[i] * (temp + b) <= 0:
finish_flag = False
alpha[i] += mu
b += mu * y[i]
break
return alpha,b
在对偶性形式中画超平面,只需由α计算出ω,再利用第一段代码中的画图语句即可