感知機-Python實現


如圖3所示的訓練數據集,其正實例點是(3,3),(3,4),負實例點是(1,1),試用感知機學習算法的原始形式求感知機模型,即求出w和b。這里,

QQ截圖20130412173717

圖3

這里我們取初值,取。具體問題解釋不寫了,求解的方法就是算法1

Python代碼如下:

 

import os
 
# An example in that book, the training set and parameters' sizes are fixed
training_set = [[(3, 3), 1], [(4, 3), 1], [(1, 1), -1]]
 
w = [0, 0]
b = 0
 
# update parameters using stochastic gradient descent
def update(item):
    global w, b
    w[0] = w[0] + 1 * item[1] * item[0][0]
    w[1] = w[1] + 1 * item[1] * item[0][1]
    b = b + 1 * item[1]
    # print w, b # you can uncomment this line to check the process of stochastic gradient descent
 
# calculate the functional distance between 'item' an the dicision surface
def cal(item):
    global w, b
    res = 0
    for i in range(len(item[0])):
        res += item[0][i] * w[i]
    res += b
    res *= item[1]
    return res
 
# check if the hyperplane can classify the examples correctly
def check():
    flag = False
    for item in training_set:
        if cal(item) <= 0:
            flag = True
            update(item)
    if not flag:
        print "RESULT: w: " + str(w) + " b: "+ str(b)
        os._exit(0)
    flag = False
 
if __name__=="__main__":
    for i in range(1000):
        check()
    print "The training_set is not linear separable. "

 

運行結果如下:
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM