[python學習]機器學習 -- 感知機

樂樂要當航天猿發表於2020-10-19

李航 《統計學習方法》-- 感知機
原始形式+對偶形式

import numpy as np
import matplotlib.pyplot as plt
'''
def perceptron_strategy_original(data,y,loop_numb):
    w = np.array([[0,0]])
    b = 0
    delta = 1
    for i in range(loop_numb):
        k = 0
        for j in data:
            if y[k]*(np.dot(w,j)+b)<=0:
                w += delta*y[k]*j
                b += delta*y[k]
            k += 1
    return w,b
'''
def perceptron_strategy_dual(data,y,loop_numb):
    w = np.array([[0,0]])
    b = 0
    delta = 1
    gram = [[18,21,6],
            [21,25,7],
            [6,7,2]]
    alpha = [0,0,0]

    for m in range(loop_numb):
        k = 0
        for n in data:
            if y[k]*(alpha[0]*y[0]*gram[0][k]+alpha[1]*y[1]*gram[1][k]+alpha[2]*y[2]*gram[2][k]+b)<=0:
                alpha[k]+=1*delta
                b += y[k]
            k += 1
    for z in range(len(data)):
        w += np.dot(alpha[z]*y[z],data[z])
    return w,b

def perceptron_figure(data,weight,y,b):
    for i in range(len(data)):
        if y[i] == 1:
            plt.plot(data[i][0],data[i][1],'ro')
        else:
            plt.plot(data[i][0],data[i][1],'bo')

    x = np.linspace(0,6,100)
    z = -b/weight[0][1]-x*weight[0][0]/weight[0][1]

    plt.plot(x,z,'k',color='y',linewidth=3,linestyle='-')
    plt.show()

def main():
    T = np.array([[3,3],[4,3],[1,1]])
    y = [1,1,-1]
    loop_numb = 100
    #weight,bias = perceptron_strategy_original(T,y,loop_numb)
    weight,bias = perceptron_strategy_dual(T,y,loop_numb)
    print('weight=',weight,'bias=',bias)
    perceptron_figure(T,weight,y,bias)

main()

相關文章