一.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 !!!