侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

python实现感知器算法(批处理)

Python  /  管理员 发布于 7年前   210

本文实例为大家分享了Python感知器算法实现的具体代码,供大家参考,具体内容如下

先创建感知器类:用于二分类

# -*- coding: utf-8 -*- import numpy as np  class Perceptron(object):  """  感知器:用于二分类  参照改写 https://blog.csdn.net/simple_the_best/article/details/54619495    属性:  w0:偏差  w:权向量  learning_rate:学习率  threshold:准则阈值  """    def __init__(self,learning_rate=0.01,threshold=0.001):    self.learning_rate=learning_rate    self.threshold=threshold      def train(self,x,y):    """训练    参数:    x:样本,维度为n*m(样本有m个特征,x输入就是m维),样本数量为n    y:类标,维度为n*1,取值1和-1(正样本和负样本)        返回:    self:object    """    self.w0=0.0    self.w=np.full(x.shape[1],0.0)        k=0    while(True):      k+=1      dJw0=0.0      dJw=np.zeros(x.shape[1])      err=0.0      for i in range(0,x.shape[0]):        if not (y[i]==1 or y[i]==-1):          print("类标只能为1或-1!请核对!")          break        update=self.learning_rate*0.5*(y[i]-self.predict(x[i]))        dJw0+=update        dJw+=update*x[i]        err+=np.abs(0.5*(y[i]-self.predict(x[i])))      self.w0 += dJw0      self.w += dJw      if np.abs(np.sum(self.learning_rate*dJw))<self.threshold or k>500:        print("迭代次数:",k," 错分样本数:",err)        break    return self          def predict(self,x):    """预测类别    参数:    x:样本,1*m维,1个样本,m维特征        返回:    yhat:预测的类标号,1或者-1,1代表正样本,-1代表负样本    """    if np.matmul(self.w,x.T)+self.w0>0:      yhat=1    else:      yhat=-1    return yhat     def predict_value(self,x):    """预测值    参数:    x:样本,1*m维,1个样本,m维特征        返回:    y:预测值    """    y=np.matmul(self.w,x.T)+self.w0    return y

然后为Iris数据集创建一个Iris类,用于产生5折验证所需要的数据,并且能产生不同样本数量的数据集。

# -*- coding: utf-8 -*-"""Author:CommissarMa2018年5月23日 16点52分"""import numpy as npimport scipy.io as sio  class Iris(object):  """Iris数据集  参数:  data:根据size裁剪出来的iris数据集  size:每种类型的样本数量  way:one against the rest || one against one    注意:  此处规定5折交叉验证(5-cv),所以每种类型样本的数量要是5的倍数  多分类方式:one against the rest  """    def __init__(self,size=50,way="one against the rest"):    """    size:每种类型的样本数量    """    data=sio.loadmat("C:\\Users\\CommissarMa\\Desktop\\模式识别\\课件ppt\\PR实验内容\\iris_data.mat")    iris_data=data['iris_data']#iris_data:原数据集,shape:150*4,1-50个样本为第一类,51-100个样本为第二类,101-150个样本为第三类    self.size=size    self.way=way    self.data=np.zeros((size*3,4))    for r in range(0,size*3):      self.data[r]=iris_data[int(r/size)*50+r%size]        def generate_train_data(self,index_fold,index_class,neg_class=None):    """    index_fold:5折验证的第几折,范围:0,1,2,3,4    index_class:第几类作为正类,类别号:负类样本为-1,正类样本为1    """    if self.way=="one against the rest":      fold_size=int(self.size/5)#将每类样本分成5份      train_data=np.zeros((fold_size*4*3,4))      label_data=np.full((fold_size*4*3),-1)      for r in range(0,fold_size*4*3):        n_class=int(r/(fold_size*4))#第几类        n_fold=int((r%(fold_size*4))/fold_size)#第几折        n=(r%(fold_size*4))%fold_size#第几个        if n_fold<index_fold:          train_data[r]=self.data[n_class*self.size+n_fold*fold_size+n]        else:          train_data[r]=self.data[n_class*self.size+(n_fold+1)*fold_size+n]  label_data[fold_size*4*index_class:fold_size*4*(index_class+1)]=1    elif self.way=="one against one":      if neg_class==None:        print("one against one模式下需要提供负类的序号!")        return      else:        fold_size=int(self.size/5)#将每类样本分成5份        train_data=np.zeros((fold_size*4*2,4))        label_data=np.full((fold_size*4*2),-1)        for r in range(0,fold_size*4*2):          n_class=int(r/(fold_size*4))#第几类          n_fold=int((r%(fold_size*4))/fold_size)#第几折          n=(r%(fold_size*4))%fold_size#第几个          if n_class==0:#放正类样本if n_fold<index_fold:  train_data[r]=self.data[index_class*self.size+n_fold*fold_size+n]else:  train_data[r]=self.data[index_class*self.size+(n_fold+1)*fold_size+n]          if n_class==1:#放负类样本if n_fold<index_fold:  train_data[r]=self.data[neg_class*self.size+n_fold*fold_size+n]else:  train_data[r]=self.data[neg_class*self.size+(n_fold+1)*fold_size+n]        label_data[0:fold_size*4]=1    else:      print("多分类方式错误!只能为one against one 或 one against the rest!")      return        return train_data,label_data      def generate_test_data(self,index_fold):    """生成测试数据    index_fold:5折验证的第几折,范围:0,1,2,3,4        返回值:    test_data:对应于第index_fold折的测试数据    label_data:类别号为0,1,2    """    fold_size=int(self.size/5)#将每类样本分成5份    test_data=np.zeros((fold_size*3,4))    label_data=np.zeros(fold_size*3)    for r in range(0,fold_size*3):      test_data[r]=self.data[int(int(r/fold_size)*self.size)+int(index_fold*fold_size)+r%fold_size]    label_data[0:fold_size]=0    label_data[fold_size:fold_size*2]=1    label_data[fold_size*2:fold_size*3]=2        return test_data,label_data

然后我们进行训练测试,先使用one against the rest策略:

# -*- coding: utf-8 -*- from perceptron import Perceptronfrom iris_data import Irisimport numpy as np if __name__=="__main__":   iris=Iris(size=50,way="one against the rest")      correct_all=0   for n_fold in range(0,5):     p=[Perceptron(),Perceptron(),Perceptron()]     for c in range(0,3):       x,y=iris.generate_train_data(index_fold=n_fold,index_class=c)       p[c].train(x,y)     #训练完毕,开始测试     correct=0     x_test,y_test=iris.generate_test_data(index_fold=n_fold)     num=len(x_test)     for i in range(0,num):       maxvalue=max(p[0].predict_value(x_test[i]),p[1].predict_value(x_test[i]),          p[2].predict_value(x_test[i]))       if maxvalue==p[int(y_test[i])].predict_value(x_test[i]):         correct+=1     print("错分数量:",num-correct,"错误率:",(num-correct)/num)     correct_all+=correct   print("平均错误率:",(num*5-correct_all)/(num*5))

然后使用one against one 策略去训练测试:

# -*- coding: utf-8 -*- from perceptron import Perceptronfrom iris_data import Irisimport numpy as np if __name__=="__main__":   iris=Iris(size=10,way="one against one")      correct_all=0   for n_fold in range(0,5):     #训练     p01=Perceptron()#0类和1类比较的判别器     p02=Perceptron()     p12=Perceptron()     x,y=iris.generate_train_data(index_fold=n_fold,index_class=0,neg_class=1)     p01.train(x,y)     x,y=iris.generate_train_data(index_fold=n_fold,index_class=0,neg_class=2)     p02.train(x,y)     x,y=iris.generate_train_data(index_fold=n_fold,index_class=1,neg_class=2)     p12.train(x,y)     #测试     correct=0     x_test,y_test=iris.generate_test_data(index_fold=n_fold)     num=len(x_test)     for i in range(0,num):       vote0=0       vote1=0       vote2=0       if p01.predict_value(x_test[i])>0:         vote0+=1       else:         vote1+=1       if p02.predict_value(x_test[i])>0:         vote0+=1       else:         vote2+=1       if p12.predict_value(x_test[i])>0:         vote1+=1       else:         vote2+=1  if vote0==max(vote0,vote1,vote2) and int(vote0)==int(y_test[i]):         correct+=1       elif vote1==max(vote0,vote1,vote2) and int(vote1)==int(y_test[i]):         correct+=1       elif vote2==max(vote0,vote1,vote2) and int(vote2)==int(y_test[i]):         correct+=1     print("错分数量:",num-correct,"错误率:",(num-correct)/num)     correct_all+=correct   print("平均错误率:",(num*5-correct_all)/(num*5))

实验结果如图所示:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    python感知机实现代码
    下一条:
    python实现多层感知器
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客