统计学习方法第二章作业:感知机模型原始形式与对偶形式代码实现

原始形式实现

import numpy as np
import matplotlib.pyplot as plt

class Perceptron_orginal:
    def __init__(self,n=1,max_iter=10):
        self.rate = n
        self.max_iter = max_iter

    def fit(self,X,Y):
        X = np.array(X)
        Y = np.array(Y)
        self.feature_num = len(X[0])
        self.w = np.zeros(self.feature_num)
        self.b = 0
        self.iter = 0
        while bool(1-(self.predict(X) == Y).all()):
            for i,j in zip(X,Y):
                result = self.predict(i)*j
                if result >0:
                    continue
                else:
                    self.w += i*j*self.rate
                    self.b += j*self.rate
            self.iter += 1
            if self.iter >= self.max_iter:
                print('cant not completely delieve the data')
                break
        print('training complete')
        pass

    def predict(self,X):
        return np.sign(np.sum(self.w * X, -1) + self.b)
        
def main():
    p1 = Perceptron_orginal(0.5,20)
    x = [[3,3],[4,3],[1,1],[1,2],[6,2]]
    y = [1,1,-1,1,-1]
    p1.fit(x,y)
    print(p1.predict(x))
    print(p1.w)
    print(p1.b)

    positive_ =  np.array(x)[np.array(y) == 1]
    negetive_ = np.array(x)[np.array(y) == -1]
    plt.scatter([k[0] for k in positive_],[k[1] for k in positive_],c='r',label='1')
    plt.scatter([k[0] for k in negetive_], [k[1] for k in negetive_],c='b',label='0')
    x_ = np.arange(0, 10, 0.1)
    y_ = -(p1.w[0] * x_ + p1.b) / p1.w[1]
    plt.plot(x_, y_)
    plt.show()

if __name__ == '__main__':
    main()

####result####################
/usr/bin/python3 /Users/zhengyanzhao/PycharmProjects/tongjixuexi/perceptron_original.py
training complete
[ 1.  1. -1.  1. -1.]
[-2.   3.5]
-2.0

对偶形式实现

import numpy as np
import matplotlib.pyplot as plt

class Perceptron_dual:
    def __init__(self,n=1,max_iter=10):
        self.max_iter = max_iter
        self.n  = n

    def fit(self,X,Y):
        X = np.array(X)
        Y = np.array(Y)
        sample_num = len(X)
        self.b = 0
        self.a = np.zeros(sample_num)
        self.w = np.sum((np.array(self.a) * np.array(Y)) * np.array(X).T, -1)
        #计算Gram矩阵
        self.G = np.zeros((sample_num,sample_num))
        for i in range(sample_num):
            for j in range(sample_num):
                self.G[i,j]=X[i].dot(X[j])
        self.iter = 0

        while bool(1-(self.predict(X) == Y).all()):
            for index,(i,j) in enumerate(zip(X,Y)):
                result = 0
                for m in range(sample_num):
                    result_mid = self.a[m]*Y[m]*self.G[m,index]
                    result += result_mid
                if j*(result + self.b) >0:
                    continue
                else:
                    self.a[index] += self.n
                    self.b += j*self.n
                    print(self.a,self.b)
            self.iter += 1
            if self.iter >= self.max_iter:
                print('cant not completely delieve the data')
                break
            self.w = np.sum((np.array(self.a) * np.array(Y)) * np.array(X).T, -1)
        print('training complete')
        pass

    def predict(self,X):
        return np.sign(np.sum(self.w * X, -1) + self.b)
        
def main():
    p1 = Perceptron_dual(1,20)
    x = [[3,3],[4,3],[1,1]]
    y = [1,1,-1]
    p1.fit(x,y)
    print(p1.predict(x))
    print(p1.w)
    print(p1.b)
    positive_ =  np.array(x)[np.array(y) == 1]
    negetive_ = np.array(x)[np.array(y) == -1]
    plt.scatter([k[0] for k in positive_],[k[1] for k in positive_],c='r',label='1')
    plt.scatter([k[0] for k in negetive_], [k[1] for k in negetive_],c='b',label='0')
    x_ = np.arange(0, 10, 0.1)
    y_ = -(p1.w[0] * x_ + p1.b) / p1.w[1]
    plt.plot(x_, y_)
    plt.show()

if __name__ == '__main__':
    main()

####result####################
[1. 0. 0.] 1
[1. 0. 1.] 0
[1. 0. 2.] -1
[1. 0. 3.] -2
[2. 0. 3.] -1
[2. 0. 4.] -2
[2. 0. 5.] -3
training complete
[ 1.  1. -1.]
[1. 1.]
-3
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值