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)