# 感知机原始形式、对偶形式

## 感知机的原始形式

f(x)=sign(wx+b)f(x)=sign(w⋅x+b)
(1)

L(w,b)=xiMyi(wxi+b)L(w,b)=−∑xi∈Myi(w⋅xi+b)
(2)

wL(w,b)=xiMyixibL(w,b)=xiMyi∇wL(w,b)=−∑xi∈Myixi∇bL(w,b)=−∑xi∈Myi
(3)

ww+ηyixibb+ηyiw←w+ηyixib←b+ηyi
(4)

1. 选取初值w0,b0
2. 在训练集中任意选取点(xi,yi)
3. 如果yi(wxi+b)>0−yi(w⋅xi+b)>0则按照(4)式更新w,b4
4. 重复2直到没有被误分的点

## 感知机的对偶形式

w=i=1Nniηyixi=i=1Nαiyixib=i=1Nniηyi=i=1Nαiyiw=∑i=1Nniηyixi=∑i=1Nαiyixib=∑i=1Nniηyi=∑i=1Nαiyi

(5)

f(x)=sign(j=1Nαjyjxjx+b)f(x)=sign(∑j=1Nαjyjxj⋅x+b)
(6)

1. 初始化α=0α=0,b=0b=0.
2. 任意选取(xi,yi)
3. 如果yi(j=1Nαjyjxjxi+b)0yi(∑j=1Nαjyjxj⋅xi+b)≤0,即发生误判，则对αi,bαi,b进行更新：
αiαi+ηbibi+ηyiαi←αi+ηbi←bi+ηyi
4. 重复2直到所有点都被正确分类

python实现:

from __future__ import division
import random
import numpy as np
import matplotlib.pyplot as plt

def sign(v):
if v>=0:
return 1
else:
return -1

def train(train_num,train_datas,lr):
w=[0,0]
b=0
for i in range(train_num):
x=random.choice(train_datas)
x1,x2,y=x
if(y*sign((w[0]*x1+w[1]*x2+b))<=0):
w[0]+=lr*y*x1
w[1]+=lr*y*x2
b+=lr*y
return w,b
def plot_points(train_datas,w,b):
plt.figure()
x1 = np.linspace(0, 8, 100)
x2 = (-b-w[0]*x1)/w[1]
plt.plot(x1, x2, color='r', label='y1 data')
datas_len=len(train_datas)
for i in range(datas_len):
if(train_datas[i][-1]==1):
plt.scatter(train_datas[i][0],train_datas[i][1],s=50)
else:
plt.scatter(train_datas[i][0],train_datas[i][1],marker='x',s=50)
plt.show()

if __name__=='__main__':
train_data1 = [[1, 3, 1], [2, 2, 1], [3, 8, 1], [2, 6, 1]]  # 正样本
train_data2 = [[2, 1, -1], [4, 1, -1], [6, 2, -1], [7, 3, -1]]  # 负样本
train_datas = train_data1 + train_data2  # 样本集
w,b=train(train_num=50,train_datas=train_datas,lr=0.01)
plot_points(train_datas,w,b)

from __future__ import division
import random
import numpy as np
import matplotlib.pyplot as plt

def sign(v):
if v>=0:
return 1
else:
return -1

def train(train_num,train_datas,lr):
w=0.0
b=0
datas_len = len(train_datas)
alpha = [0 for i in range(datas_len)]
train_array = np.array(train_datas)
gram = np.matmul(train_array[:,0:-1] , train_array[:,0:-1].T)
for idx in range(train_num):
tmp=0
i = random.randint(0,datas_len-1)
yi=train_array[i,-1]
for j in range(datas_len):
tmp+=alpha[j]*train_array[j,-1]*gram[i,j]
tmp+=b
if(yi*tmp<=0):
alpha[i]=alpha[i]+lr
b=b+lr*yi
for i in range(datas_len):
w+=alpha[i]*train_array[i,0:-1]*train_array[i,-1]
return w,b,alpha,gram

def plot_points(train_datas,w,b):
plt.figure()
x1 = np.linspace(0, 8, 100)
x2 = (-b-w[0]*x1)/(w[1]+1e-10)
plt.plot(x1, x2, color='r', label='y1 data')
datas_len=len(train_datas)
for i in range(datas_len):
if(train_datas[i][-1]==1):
plt.scatter(train_datas[i][0],train_datas[i][1],s=50)
else:
plt.scatter(train_datas[i][0],train_datas[i][1],marker='x',s=50)
plt.show()

if __name__=='__main__':
train_data1 = [[1, 3, 1], [2, 2, 1], [3, 8, 1], [2, 6, 1]]  # 正样本
train_data2 = [[2, 1, -1], [4, 1, -1], [6, 2, -1], [7, 3, -1]]  # 负样本
train_datas = train_data1 + train_data2  # 样本集
w,b,alpha,gram=train(train_num=500,train_datas=train_datas,lr=0.01)
plot_points(train_datas,w,b)

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120