感知机 Python代码

来源:互联网 发布:淘宝客服管理方案 编辑:程序博客网 时间:2024/05/17 22:39
#Python 3.6#author: ald#import numpy as np x = np.array([[3, 3],[4, 3],[1, 1],[5,2]])y = np.array([1, 1, -1,-1])Gamma = x.dot(x.T)eta = 1alpha = np.zeros(len(y), np.float)b = 0judge = Falsedef update(i):    global b, alpha,eta,y    alpha[i] += eta    b += eta * y[i]def check(i):    global y,alpha,Gamma,b    result = y[i] * (np.dot(alpha * y, Gamma[i]) + b) > 0    return resultwhile not judge: #如果judge = False,表明上次循环发生过更新,要继续循环,直至上轮循环未发生更新    judge = True    for m in range(0,len(y)):        while not check(m):            judge = False            update(m)            w = np.dot(alpha*y,x)print('w:',str(w),'\nb:',b)

0 0
原创粉丝点击