书中感知机算法学习的目标是极小化所有误分类点到分离超平面的距离和, 于是损失函数定义为
其中M为误分类样本集合.
采用随机梯度下降(SGD), 考虑单个误分类样本
对参数求导得
更新参数
import numpy as np
class Perceptron(object):
def __init__(self, feature_num, alpha, max_step=10000):
self._alpha = alpha
self._w = np.zeros(feature_num)
self._b = 0
self._max_step = max_step
def fit(self, X, y):
misclassify = True
step = 0
while misclassify and step <= self._max_step:
misclassify = False
step += 1
for tx, ty in zip(X, y):
if ty * (np.dot(tx, self._w) + self._b) <= 0:
self._w += self._alpha * tx * ty
self._b += self._alpha * ty
misclassify = True
def predict(self, X):
return np.where((X @ self._w.T + self._b).astype(int) > 0, 1, -1)
网友评论