import numpy as np
training_set = [[[3,3],1],[[4,3],1],[[1,1],-1]]
eta = 1 #study rate
#原始形式
def perceptron1(train_data,eta):
w = [0,0]
b = 0
for item in train_data:
if item[1]*(np.dot(w,item[0]) + b) <= 0:
step = item[0][0]*item[1]*eta
w = [i + step for i in w]
b = b + eta*item[1]
print(w,b)
return w,b
t1,t2 = perceptron1(training_set,eta)
#对偶形式
def perceptron2(train_data,eta):
N = len(train_data)
a = np.zeros(shape=N)
b = 0
X = np.array([i[0] for i in train_data])
Y = np.array([i[1] for i in train_data])
#计算Gram矩阵
G = np.zeros(shape=(N,N))
index = 0
for i in X:
G[index] = np.array([np.dot(i,j) for j in X])
print(G[index])
index = index + 1
Gram = np.array(G)
#根据每个样本进行迭代
for iters in range(N):
print(iters)
print(Y[iters],np.sum(a*Y*Gram[iters])+b)
if Y[iters]*(np.sum(a*Y*Gram[iters])+b) <= 0:
a = a + 1
b = b + train_data[iters][1]
print(iters,a,b)
w = np.dot(a*Y,X)
return w,b
t3,t4 = perceptron2(training_set,eta)
不知道为什么两种方法得出的结果不一样,sad