最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實現(xiàn)感知器算法(批處理)

 更新時間:2019年01月18日 11:34:23   作者:CommissarMa  
這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)感知器算法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Python感知器算法實現(xiàn)的具體代碼,供大家參考,具體內(nèi)容如下

先創(chuàng)建感知器類:用于二分類

# -*- coding: utf-8 -*-
 
import numpy as np
 
 
class Perceptron(object):
  """
  感知器:用于二分類
  參照改寫 https://blog.csdn.net/simple_the_best/article/details/54619495
  
  屬性:
  w0:偏差
  w:權(quán)向量
  learning_rate:學(xué)習(xí)率
  threshold:準(zhǔn)則閾值
  """
  
  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)練
    參數(shù):
    x:樣本,維度為n*m(樣本有m個特征,x輸入就是m維),樣本數(shù)量為n
    y:類標(biāo),維度為n*1,取值1和-1(正樣本和負(fù)樣本)
    
    返回:
    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("類標(biāo)只能為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("迭代次數(shù):",k," 錯分樣本數(shù):",err)
        break
    return self
    
    
  def predict(self,x):
    """預(yù)測類別
    參數(shù):
    x:樣本,1*m維,1個樣本,m維特征
    
    返回:
    yhat:預(yù)測的類標(biāo)號,1或者-1,1代表正樣本,-1代表負(fù)樣本
    """
    if np.matmul(self.w,x.T)+self.w0>0:
      yhat=1
    else:
      yhat=-1
    return yhat 
  
  def predict_value(self,x):
    """預(yù)測值
    參數(shù):
    x:樣本,1*m維,1個樣本,m維特征
    
    返回:
    y:預(yù)測值
    """
    y=np.matmul(self.w,x.T)+self.w0
    return y

然后為Iris數(shù)據(jù)集創(chuàng)建一個Iris類,用于產(chǎn)生5折驗證所需要的數(shù)據(jù),并且能產(chǎn)生不同樣本數(shù)量的數(shù)據(jù)集。

# -*- coding: utf-8 -*-
"""
Author:CommissarMa
2018年5月23日 16點52分
"""
import numpy as np
import scipy.io as sio
 
 
class Iris(object):
  """Iris數(shù)據(jù)集
  參數(shù):
  data:根據(jù)size裁剪出來的iris數(shù)據(jù)集
  size:每種類型的樣本數(shù)量
  way:one against the rest || one against one
  
  注意:
  此處規(guī)定5折交叉驗證(5-cv),所以每種類型樣本的數(shù)量要是5的倍數(shù)
  多分類方式:one against the rest
  """
  
  def __init__(self,size=50,way="one against the rest"):
    """
    size:每種類型的樣本數(shù)量
    """
    data=sio.loadmat("C:\\Users\\CommissarMa\\Desktop\\模式識別\\課件ppt\\PR實驗內(nèi)容\\iris_data.mat")
    iris_data=data['iris_data']#iris_data:原數(shù)據(jù)集,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:第幾類作為正類,類別號:負(fù)類樣本為-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模式下需要提供負(fù)類的序號!")
        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:#放負(fù)類樣本
            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):
    """生成測試數(shù)據(jù)
    index_fold:5折驗證的第幾折,范圍:0,1,2,3,4
    
    返回值:
    test_data:對應(yīng)于第index_fold折的測試數(shù)據(jù)
    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

然后我們進(jìn)行訓(xùn)練測試,先使用one against the rest策略:

# -*- coding: utf-8 -*-
 
from perceptron import Perceptron
from iris_data import Iris
import 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)
     #訓(xùn)練完畢,開始測試
     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("錯分?jǐn)?shù)量:",num-correct,"錯誤率:",(num-correct)/num)
     correct_all+=correct
   print("平均錯誤率:",(num*5-correct_all)/(num*5))

然后使用one against one 策略去訓(xùn)練測試:

# -*- coding: utf-8 -*-
 
from perceptron import Perceptron
from iris_data import Iris
import numpy as np
 
if __name__=="__main__":
   iris=Iris(size=10,way="one against one")
   
   correct_all=0
   for n_fold in range(0,5):
     #訓(xùn)練
     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("錯分?jǐn)?shù)量:",num-correct,"錯誤率:",(num-correct)/num)
     correct_all+=correct
   print("平均錯誤率:",(num*5-correct_all)/(num*5))

實驗結(jié)果如圖所示:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 三行代碼使用Python將視頻轉(zhuǎn)Gif的方法示例

    三行代碼使用Python將視頻轉(zhuǎn)Gif的方法示例

    本文主要介紹了三行代碼使用Python將視頻轉(zhuǎn)Gif的方法示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • python BitMap算法處理20億隨機整數(shù)去重

    python BitMap算法處理20億隨機整數(shù)去重

    這篇文章主要為大家介紹了python BitMap算法處理20億隨機整數(shù)去重,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • pytorch-autograde-計算圖的特點說明

    pytorch-autograde-計算圖的特點說明

    這篇文章主要介紹了pytorch-autograde-計算圖的特點,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • Python上下文管理器用法及實例解析

    Python上下文管理器用法及實例解析

    這篇文章主要介紹了Python上下文管理器用法及實例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • Python實現(xiàn)的基于優(yōu)先等級分配糖果問題算法示例

    Python實現(xiàn)的基于優(yōu)先等級分配糖果問題算法示例

    這篇文章主要介紹了Python實現(xiàn)的基于優(yōu)先等級分配糖果問題算法,涉及Python針對列表的遍歷、判斷、計算等相關(guān)操作技巧,需要的朋友可以參考下
    2018-04-04
  • 答題輔助python代碼實現(xiàn)

    答題輔助python代碼實現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了答題輔助python代碼實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 一文詳解如何實現(xiàn)PyTorch模型編譯

    一文詳解如何實現(xiàn)PyTorch模型編譯

    這篇文章主要為大家介紹了如何實現(xiàn)PyTorch?模型編譯詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • Python循環(huán)控制詳解

    Python循環(huán)控制詳解

    本文介紹了Python編程語言中關(guān)于for循環(huán)和if條件控制的一些基本使用,包含了單層循環(huán)的退出機制和多層循環(huán)的退出機制,使得我們在滿足特定條件時,可以直接結(jié)束多層循環(huán),需要的朋友可以參考下
    2024-07-07
  • Python辦公自動化之網(wǎng)絡(luò)監(jiān)控和壓縮文件處理

    Python辦公自動化之網(wǎng)絡(luò)監(jiān)控和壓縮文件處理

    Python辦公?動化是利用Python編程語?來創(chuàng)建腳本和程序,以簡化、加速和?動化?常辦公任務(wù)和工作流程的過程,本文主要介紹了如何進(jìn)行網(wǎng)絡(luò)監(jiān)控和壓縮文件處理,感興趣的可以了解下
    2023-12-12
  • Python3 shelve對象持久存儲原理詳解

    Python3 shelve對象持久存儲原理詳解

    這篇文章主要介紹了Python3 shelve對象持久存儲原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03

最新評論

大悟县| 肃宁县| 青神县| 孝义市| 壤塘县| 巢湖市| 锦州市| 罗城| 望奎县| 梁山县| 祥云县| 漠河县| 连云港市| 通城县| 乌海市| 岗巴县| 桃园县| 双鸭山市| 襄城县| 海兴县| 平塘县| 江达县| 南郑县| 平泉县| 蒙自县| 都兰县| 五莲县| 花莲市| 商水县| 格尔木市| 京山县| 工布江达县| 乌兰察布市| 德惠市| 临澧县| 余姚市| 名山县| 黄骅市| 南通市| 淄博市| 镇康县|