普通版
本人编写感知器模型部分的代码实例如下:
class Perception(object):
def __init__(self):
self.w = None # dim x 1
self.b = 0
self.lr = 0.5
def obj_func(self, xi, yi):
return yi*(xi.dot(self.w) + self.b)
def fit(self, x, y):
self.w = np.zeros(x.shape[1])
i = 0
while True:
if self.obj_func(x[i], y[i]) <= 0:
self.w = self.w + self.lr * y[i] * x[i]
self.b = self.b + self.lr * y[i]
print('weight: {}, bias: {}'.format(self.w, self.b))
i