一、感知机
感知机是一种用于分类的优美的统计学习算法。感知器实现起来非常简单,它是一种在线算法。最重要的是,当应用于线性可分离集时:它是数学函数、学习算法和算法正确性证明的结合。
二、Python算法实现
import numpy as np
X = np.array([[0, 1, 1], [1, 0, 1], [1, 1, 1], [-1, 1, 1], [1, -1, 1]])
Y = np.array([1, 1, 1, 0, 0])
W = np.zeros(3)
def perceptron(x, w):
return (x @ w >= 0).astype(int)
def train(x, y, w):
for i in range(len(x)):
# 估算感知机
h = perceptron(x[i, :], w)
#误分类
if h != y[i]:
if y[i] == 1:
w += x[i, :]
else:
w -= x[i, :]
return perceptron(x, w)
for i in range(5):
h = train(X, Y, W)
print('w=', W, 'acc=', np.mean(h == Y))
输出结果: