机器学习教程之1-感知器(Perceptron)的sklearn实现

来源:互联网 发布:mac有的软件删不掉 编辑:程序博客网 时间:2024/06/05 05:30

0.概述

优点
简单且易于实现

缺点

1.感知器模型

如果数据是线性可分的,并且是二分类的,则可以以下函数模型表示输入到输出的关系:

这里写图片描述

2.感知器学习策略

将所有误分点到超平面距离之和表示为代价函数

这里写图片描述

不考虑
,得到感知器的代价函数

说明:李航的书用L(w,b)表示代价函数,而Ng教程用J()表示代价函数。

3.感知器学习算法

这里写图片描述

这里写图片描述

4.代码

# @Author: Tianze Tang# @Date:   2017-07-10# @Email:  454779758@qq.com# @Last modified by:   Tianze Tang# @Last modified time: 2017-07-10import pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport cv2import randomimport timefrom sklearn.model_selection import train_test_splitfrom sklearn.metrics import accuracy_scoreclass Perceptron(object):    def __init__(self):        self.learning_step = 1        self.max_iteration = 100000    def sign(self,x):        if (x >= 0):            logic=1        else:             logic=0        return logic    #  w*x+b    def threshold(self,w,b,x):        result = np.dot(w ,x) + b;        return result    # train    def train(self,x,y):        w = np.zeros(len(x[0]))        b = 0        i = 0        while (i<self.max_iteration):            index = len(y)            random_number = random.randint(0,index-1)            if (y[random_number]* self.threshold(w,b,x[random_number])<= 0):                w = w + self.learning_step * y[random_number]*x[random_number]                b = b + self.learning_step * y[random_number]            i = i + 1        return w,bx = np.array([[3,3],[4,3],[1,1]],dtype= int)y = np.array([1,1,-1])plt.plot([3,4],[3,3],'rx')plt.plot([1],[1],'b*')plt.axis([0,6,0,6])test = Perceptron()w,b=test.train(x,y)# w*x+b=0y1=(-b-w[0]*1)/w[1]x2=(-b-w[1]*1)/w[0]plt.plot([1,y1],[x2,1],'g')plt.show()

这里写图片描述

5.总结

这里写图片描述

6.参考资料

[1] 李航《统计学习方法》第二章——用Python实现感知器模型(MNIST数据集)
[2] Matplotlib Pyplot tutorial

阅读全文
0 0
原创粉丝点击