1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
import numpy as np
X = [np.matrix([3,3,1]).T, np.matrix([4,3,1]).T, np.matrix([1,1,1]).T] Y = [1,1,-1] w = np.zeros(2) w = np.r_[w,0].reshape((3,1)) l_r = 1
def sign_result(ww,xx,yy): return np.sum(w.T*x)*yy
while True: flag = True for x,y in zip(X,Y): result = sign_result(w,x,y) if result<=0: w = w + l_r * y * x flag = False continue if flag: break print(w)
|