感知机是二类分类的线性分类模型,其输入为实例的特征向量,输出为实例的类别,取+1和-1二值。感知机对应于输入空间(特征空间)中将实例划分成正负两类的分离超平面,属于判别模型。感知机学习指在求出将训练数据进行线性划分的分离超平面。
输入:训练数据集 T = { ( x 1 , y 1 ) , ( x 2 , y 2 ) , … , ( x N , y N ) } T=\{(x_1,y_1),(x_2,y_2),\dots,(x_N,y_N) \} T={(x1,y1),(x2,y2),…,(xN,yN)},学习率 η ( 0 < η ≤ 1 ) \eta(0<\eta\leq1) η(0<η≤1)
输出:w,b
感知机模型
f ( x ) = s i g n ( w ⋅ x + b ) f(x) = sign(w \cdot x + b) f(x)=sign(w⋅x+b)
原始形式Python代码:
import numpy as np
Data_set = np.array([[3,3,1],[4,3,1],[1,1,-1]])
w = np.array([0,0])
b = 0
theta = 1
history = []
def ganzhiji(dataset):
global w, b, theta , history
s = dataset.shape[0]
history.append([w,b])
for i in range(s):
if (dataset[i,2]*(np.dot(w,dataset[i,0:2])+b)<=0):
w = w + theta*dataset[i,2]*dataset[i,0:2]
b = b + theta*dataset[i,2]
ganzhiji(dataset)
return w,b
if __name__=='__main__':
s = ganzhiji(Data_set)
print("最终迭代参数为:" ,s)
print("迭代过程参数变化: \n" )
for i in history:
print(i)
输入:线性可分的数据集 T = { ( x 1 , y 1 ) , ( x 2 , y 2 ) , … , ( x N , y N ) } T=\{(x_1,y_1),(x_2,y_2),\dots,(x_N,y_N) \} T={(x1,y1),(x2,y2),…,(xN,yN)},学习率 η ( 0 < η ≤ 1 ) \eta(0<\eta\leq1) η(0<η≤1)
输出: α , b \alpha,b α,b
感知机模型: f ( x ) = s i g n ( ∑ j = 1 N α j y j x j ⋅ x + b ) , 其 中 α = ( α 1 , α 2 , … , α N ) T f(x)=sign(\sum_{j=1} ^N \alpha_jy_jx_j\cdot x+b),其中\alpha = (\alpha_1,\alpha_2,\dots,\alpha_N)^T f(x)=sign(j=1∑Nαjyjxj⋅x+b),其中α=(α1,α2,…,αN)T
对偶形式中训练实例仅以内积的形式出现,可以预先将训练集中实例间的内积计算出来并以矩阵的形式存储,这个矩阵就是Gram矩阵: G = [ x i ⋅ x j ] N ∗ N G=[x_i \cdot x_j]_N*_N G=[xi⋅xj]N∗N
对偶形式
import numpy as np
Data_set = np.array([[3,3,1],[4,3,1],[1,1,-1]])
datax = Data_set[:,0:2]
datay = Data_set[:,2]
length = Data_set.shape[0]
history =[]
Gram = np.zeros([length,length])
for i in range(length):
for j in range(length):
Gram[i][j] = np.dot(datax[i],datax[j])
print("输出格拉姆矩阵:\n",Gram)
alpha = np.zeros([1,Data_set.shape[0]])[0]
b = 0
def ganzhiji2():
global w1,w2,history,alpha,b
w1 , w2 = np.dot(alpha*datay,datax)
history.append([[w1,w2],b])
for i in range(Data_set.shape[0]):
if((np.dot(alpha*datay,Gram[:,i])+b)*datay[i] <= 0):
alpha[i] = alpha[i] + 1
b = b + datay[i]
ganzhiji2()
return alpha,b
if __name__== '__main__':
s = ganzhiji2()
print("最终迭代参数为:",w1,w2,b)
print("迭代过程参数变化: \n" )
for i in history:
print(i)
当训练集线性可分时,感知机学习算法存在无穷多个解,其解由于不同的初值或迭代顺序不同而可能有所不同。