一.Python实现《李航统计学》第二章感知机算法。
先来说一下算法原原理吧:
- 感知机由输入空间到输出空间的函数为:
算法实现的步骤如下:
- 初始化w,b
- 遍历数据(x,y)
- 如果遇到sign(w*x(i)+b)!= y(i),则更新w,b
- 然后结束本次遍历,转至步骤2,知道数据中没有分类错误的点存在。
import numpy as np
x = np.array([
[3,3],
[4,3],
[1,1]
])
y = np.array([1,1,-1])
w = np.array([1,1])
b = 0
learning_rate = 0.1
for i in range(10):
temp = -1
for j in range(x.shape[0]):
pred = np.sign(np.dot(w,x[j])+b)
if pred != y[j]:
temp = j
break
if temp != -1:
w = w + learning_rate*x[temp]*y[temp]
b = b + learning_rate*y[temp]
基本迭代几次就可以得到该平面了。
版本二:
import numpy as np
def process(x,y,learning_rate=0.01,epoch=100):
w = np.ones_like(x[1])
b = 1
for i in range(epoch):
temp = -1
for j in range(x.shape[0]):
pred = np.sign(np.dot(w,x[j])+b)
if pred != y[j]:
temp = j
break
if temp != -1:
w = w + learning_rate * x[temp] * y[temp]
b = b + learning_rate * y[temp]
x = np.array([
[3,3],
[4,3],
[1,1]
])
y = np.array([1,1,-1])
process(x,y,1,10)
版本三:
import numpy as np
class Perceptron:
def fit(self,x,y,learning_rate=0.1, epoch=100):
self.x = x
self.y = y
self.learning_rate = learning_rate
self.epoch = epoch
self.w = np.ones_like(x[1])
self.b = 1
for i in range(self.epoch):
temp = -1
for j in range(self.x.shape[0]):
pred = np.sign(np.dot(self.w, self.x[j]) + self.b)
if pred != self.y[j]:
temp = j
break
if temp != -1:
self.w = self.w + self.learning_rate * self.x[temp] * self.y[temp]
self.b = self.b + self.learning_rate * self.y[temp]
def pred(self,x):
return np.sign(np.dot(self.w,x)+self.b)
x = np.array([
[3,3],
[4,3],
[1,1]
])
y = np.array([1,1,-1])
a = Perceptron()
a.fit(x,y,1)
print(a.pred(np.array([1,1])))
上面以三种不同形式的代码实现了感知机,随着以后的深入学习,还会继续完善这些代码。
Thank for your reading !!!