统计学习方法第二章例题2.2代码实践,感知机的对偶形式的代码实现

针对统计学习方法第二章的第二个例题设计了代码如下:

(注释部分为自己出错的部分调试代码,小白码代码略扎心)

#-*- coding:utf-8 -*-
import sys
from numpy import *
import os
from compiler.ast import flatten
reload(sys)
sys.setdefaultencoding('utf-8')

#首先将书上的训练集输入
def loadDataset():
    dataset=[[3,3],[4,3],[1,1]]
    labels=[1,1,-1]
    return dataset,labels

#定义sign函数
def function_sign(x):
    if x>=0:return 1
    else:return -1

#通过AMat计算权重W的函数,其中flatten函数的作用是将二维数组转换成一维
def calc_w(mat1,mat2,labels):
    mat2List=flatten(mat2.tolist())
    m,n=mat1.shape
    for i in range(m):
        mat1[i] *= (mat2List[i]*labels[i])
    return sum(mat1,axis=0).T

#定义函数是否需要更新AMat和b的值
def judge_functon(dataMat,labels,labelsMat,b,m,AMat):
    for i in range(m):
        if (labels[i] * (sum(multiply(dataMat * dataMat[i].T, multiply(AMat, labelsMat))) + b)) <= 0:
        #if (labels[i]*(sum(array(dataMat*dataMat[i].T)*labels*array(AMat.T))+b))<=0:
            return True
    return False
#算法主程序
def perceptron(dataset,labels,alpha):
    dataMat=mat(dataset)
    m,n=shape(dataMat)
    aArr=zeros((m,1))
    AMat=mat(aArr);b=0
    w=mat(zeros((n,1)))
    labelsMat=mat(labels).T
    #while ([function_sign(j) for j in array(dataMat * w+b)]) != labels:
    while judge_functon(dataMat,labels,labelsMat,b,m,AMat):
        #print array(dataMat*dataMat[i].T
        for i in range(m):
            #print sum(multiply(dataMat*dataMat[i].T,multiply(AMat,labelsMat))
            if (labels[i]*(sum(multiply(dataMat*dataMat[i].T,multiply(AMat,labelsMat)))+b))<=0:
            #if (labels[i]*(sum(array(dataMat*dataMat[i].T)*labels*array(AMat.T))+b))<=0:
                #print array(dataMat*dataMat[i].T)*labels*array(AMat.T)
                AMat[i] += alpha
                b += alpha*labels[i]
                #print AMat
                #print b
    return AMat,b



dataSet,labels=loadDataset()
dataMat=mat(dataSet);labelsMat=mat(labels)
AMat,b=perceptron(dataSet,labels,1)
print AMat,b
w=mat(calc_w(dataMat,AMat,labels))
print w,'\n',b
结果如下:

w=  [[1]
    [1]] 
b=  -3

made by zcl at CUMT


阅读更多
换一批

没有更多推荐了,返回首页