感知機算法實現-python

一.Python實現《李航統計學》第二章感知機算法。
先來說一下算法原原理吧:

  1. 感知機由輸入空間到輸出空間的函數爲:
    y=sign(wx+b)y=sign(w*x+b)
    sign(x)=+1(x>=0)sign(x)= +1 (x>=0)
    sign(x)=1(x<0)sign(x) = -1(x<0)

算法實現的步驟如下:

  1. 初始化w,b
  2. 遍歷數據(x,y)
  3. 如果遇到sign(w*x(i)+b)!= y(i),則更新w,b
    w=w+learningratey(i)x(i)w = w +learning-rate*y(i)*x(i)
    b=b+learningratey(i)b = b+learning-rate*y(i)
  4. 然後結束本次遍歷,轉至步驟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 !!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章