台大机器学习基石上_lesson 2

Machine Learning Foundations –lesson 2

Perceptron definiton

这里写图片描述

Perceptron Hypothesis

下面是的对上面h(x) 向量表示,且假设函数(是一个超平面)的截距(threshold)变成W0, 那么X0变成1,即下面的向量表达顺利成章。
这里写图片描述

Perceptron Learning Algorithm

下面是学习模型一般过程,那么假设函数h(x)已经找到,那么就需要找到定义损失函数来将损失最小化从而学习到模型的参数w
这里写图片描述
学习算法是伪代码
1. 算法实现过程演示
这里写图片描述
这里写图片描述
拿上面的两张图说明这个算法的执行过程:
x 代表 -1 , o 代表 +1 ;
左图(update6)中的一个黑色的点被误分了,那么紧着这个平面的法向量应该再往右。正好体现在Wt+1 = Wt + y*x, 其中y=-1, 如下图所示
这里写图片描述
2. 算法收敛性证明
With all of the discussion above, we have proved that PLA will halt after many times of correction if D is linear separable
https://kelvin.link/2018/06/10/ML2_PerceptronAlgorithm/
当然收敛也是有条件的:必须线性可分!!

Pocket Algorithm–D not linear separable?

这里写图片描述

PLA Algorithm

下面是根据课程题目,利用pandas读写文件实现的PLA算法。当然可以基于此进行演变不同需求的算法

#coding:utf8

import numpy as np
import random
import pandas as pd

class PLA(object):
    ###
     本代码设置最大迭代次数2000
     采用随机遍历样本来更新参数
    ###
    def train_pla_random(self, data):
        X = data[['fea0','fea1','fea2','fea3','fea4']].values
        # # print X[0]
        y = data['label']
        max_iter = 2000
        ite = 1
        count = 0

        while ite <= max_iter:
            #shuffle洗牌,对所有样本全部打乱顺序;还可以在遍历的时候就用random生成一个不同的数字代表行号 
            #这里有点繁琐
            orders = range(data.shape[0])
            random.shuffle(orders)
            W = np.zeros((1,X.shape[1]))
            print 'ite', ite
            # print '---start count---',count

            while True:
                flag = 0
                for i in orders:
                    if np.dot(X[i,:], W[0]) * y[i] <=0 :
                        W[0] +=  y[i] * X[i]  #y[i] * X[i]
                        count += 1
                        flag = 1
                if flag == 0:
                    break
            ite += 1
            # print '---end count---',count
        print count/(1.* max_iter)
        # return count

    ###
    本算法采用的顺序执行
    ###
    def train_pla(self, data):
        X = data[['fea0','fea1','fea2','fea3','fea4']].values
        y = data['label']
        count = 0
        W = np.zeros((1,X.shape[1]))

        while True:
            flag = 0
            for i in range(data.shape[0]):
                if np.dot(X[i,:], W[0]) * y[i] <=0 :
                    W[0] +=  y[i] * X[i]
                    count += 1
                    flag = 1
            if flag == 0:
                break
        print count
        # return count


if __name__ == '__main__':
    # init the class
    my_Percetron = PLA()
    # get the data by pandas 
    data = pd.read_csv('./train.dat',sep='\s+', names=['fea1','fea2','fea3','fea4','label'])
    # add x0 =1
    data['fea0'] = 1
    # get hea data
    print data.head()

    #train PLA
    my_Percetron.train_pla(data)

附:Pocket 算法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值