欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實現(xiàn)感知機(PLA)算法

 更新時間:2021年10月11日 16:46:39   作者:SmileAda  
這篇文章主要為大家詳細介紹了Python實現(xiàn)感知機(PLA)算法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

我們主要講解一下利用Python實現(xiàn)感知機算法。

算法一

首選,我們利用Python,按照上一節(jié)介紹的感知機算法基本思想,實現(xiàn)感知算法的原始形式和對偶形式。

#利用Python實現(xiàn)感知機算法的原始形式
# -*- encoding:utf-8 -*-

"""
Created on 2017.6.7

@author: Ada
"""

import numpy as np
import matplotlib.pyplot as plt

#1、創(chuàng)建數(shù)據(jù)集
def createdata():
 samples=np.array([[3,-3],[4,-3],[1,1],[1,2]])
 labels=[-1,-1,1,1]
 return samples,labels

#訓練感知機模型
class Perceptron:
 def __init__(self,x,y,a=1):
  self.x=x
  self.y=y
  self.w=np.zeros((x.shape[1],1))#初始化權重,w1,w2均為0
  self.b=0
  self.a=1#學習率
  self.numsamples=self.x.shape[0]
  self.numfeatures=self.x.shape[1]

 def sign(self,w,b,x):
  y=np.dot(x,w)+b
  return int(y)

 def update(self,label_i,data_i):
  tmp=label_i*self.a*data_i
  tmp=tmp.reshape(self.w.shape)
  #更新w和b
  self.w=tmp+self.w
  self.b=self.b+label_i*self.a

 def train(self):
  isFind=False
  while not isFind:
   count=0
   for i in range(self.numsamples):
    tmpY=self.sign(self.w,self.b,self.x[i,:])
    if tmpY*self.y[i]<=0:#如果是一個誤分類實例點
     print '誤分類點為:',self.x[i,:],'此時的w和b為:',self.w,self.b
     count+=1
     self.update(self.y[i],self.x[i,:])
   if count==0:
    print '最終訓練得到的w和b為:',self.w,self.b
    isFind=True
  return self.w,self.b

#畫圖描繪
class Picture:
 def __init__(self,data,w,b):
  self.b=b
  self.w=w
  plt.figure(1)
  plt.title('Perceptron Learning Algorithm',size=14)
  plt.xlabel('x0-axis',size=14)
  plt.ylabel('x1-axis',size=14)

  xData=np.linspace(0,5,100)
  yData=self.expression(xData)
  plt.plot(xData,yData,color='r',label='sample data')

  plt.scatter(data[0][0],data[0][1],s=50)
  plt.scatter(data[1][0],data[1][1],s=50)
  plt.scatter(data[2][0],data[2][1],s=50,marker='x')
  plt.scatter(data[3][0],data[3][1],s=50,marker='x')
  plt.savefig('2d.png',dpi=75)

 def expression(self,x):
  y=(-self.b-self.w[0]*x)/self.w[1]#注意在此,把x0,x1當做兩個坐標軸,把x1當做自變量,x2為因變量
  return y

 def Show(self):
  plt.show()


if __name__ == '__main__':
 samples,labels=createdata()
 myperceptron=Perceptron(x=samples,y=labels)
 weights,bias=myperceptron.train()
 Picture=Picture(samples,weights,bias)
 Picture.Show()

實驗結果:

    誤分類點為: [ 3 -3] 此時的w和b為: [[ 0.]
                                     [ 0.]] 0
    誤分類點為: [1 1] 此時的w和b為: [[-3.]
                                    [ 3.]] -1
    最終訓練得到的w和b為: [[-2.]
                         [ 4.]] 0

#利用Python實現(xiàn)感知機算法的對偶形式
# -*- encoding:utf-8 -*-

"""
Created on 2017.6.7

@author: Ada
"""

import numpy as np
import matplotlib.pyplot as plt

#1、創(chuàng)建數(shù)據(jù)集
def createdata():
 samples=np.array([[3,-3],[4,-3],[1,1],[1,2]])
 labels=np.array([-1,-1,1,1])
 return samples,labels

#訓練感知機模型
class Perceptron:
 def __init__(self,x,y,a=1):
  self.x=x
  self.y=y
  self.w=np.zeros((1,x.shape[0]))
  self.b=0
  self.a=1#學習率
  self.numsamples=self.x.shape[0]
  self.numfeatures=self.x.shape[1]
  self.gMatrix=self.cal_gram(self.x)

 def cal_gram(self,x):
  gMatrix=np.zeros((self.numsamples,self.numsamples))
  for i in xrange(self.numsamples):
   for j in xrange(self.numsamples):
    gMatrix[i][j]=np.dot(self.x[i,:],self.x[j,:])
  return gMatrix

 def sign(self,w,b,key):
  y=np.dot(w*self.y,self.gMatrix[:,key])+b
  return int(y)

 def update(self,i):
  self.w[i,]=self.w[i,]+self.a
  self.b=self.b+self.y[i]*self.a

 def cal_w(self):
  w=np.dot(self.w*self.y,self.x)
  return w

 def train(self):
  isFind=False
  while not isFind:
   count=0
   for i in range(self.numsamples):
    tmpY=self.sign(self.w,self.b,i)
    if tmpY*self.y[i]<=0:#如果是一個誤分類實例點
     print '誤分類點為:',self.x[i,:],'此時的w和b為:',self.cal_w(),',',self.b
     count+=1
     self.update(i)
   if count==0:
    print '最終訓練得到的w和b為:',self.cal_w(),',',self.b
    isFind=True
  weights=self.cal_w()
  return weights,self.b

#畫圖描繪
class Picture:
 def __init__(self,data,w,b):
  self.b=b
  self.w=w
  plt.figure(1)
  plt.title('Perceptron Learning Algorithm',size=14)
  plt.xlabel('x0-axis',size=14)
  plt.ylabel('x1-axis',size=14)

  xData=np.linspace(0,5,100)
  yData=self.expression(xData)
  plt.plot(xData,yData,color='r',label='sample data')

  plt.scatter(data[0][0],data[0][1],s=50)
  plt.scatter(data[1][0],data[1][1],s=50)
  plt.scatter(data[2][0],data[2][1],s=50,marker='x')
  plt.scatter(data[3][0],data[3][1],s=50,marker='x')
  plt.savefig('2d.png',dpi=75)

 def expression(self,x):
  y=(-self.b-self.w[:,0]*x)/self.w[:,1]
  return y

 def Show(self):
  plt.show()


if __name__ == '__main__':

 samples,labels=createdata()
 myperceptron=Perceptron(x=samples,y=labels)
 weights,bias=myperceptron.train()
 Picture=Picture(samples,weights,bias)
 Picture.Show()

實驗結果:

誤分類點為: [ 3 -3] 此時的w和b為: [[ 0.  0.]] , 0
最終訓練得到的w和b為: [[-5.  9.]] , -1

通過以上實驗結果可以看出,兩種方法的結果是不同的,一方面,是由于兩種優(yōu)化方法不同;二是,因為在選擇實例點的順序上有關系。但是無論用哪種方法,都可以找到一條直線,把數(shù)據(jù)完全分開。實際上,就算使用同一算法,如果改變初始值w0,b0,或者改變選擇實例點的順序,也可以使得結果不同。

算法二

Python的機器學習包sklearn中也包含了感知機學習算法,我們可以直接調(diào)用,因為感知機算法屬于線性模型,所以從sklearn.linear_model中import下面給出例子。

# -*- encoding:utf-8 -*-

"""
利用sklearn中的感知機學習算法進行實驗
Created on 2017.6.7

@author: Ada
"""

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import Perceptron

#創(chuàng)建數(shù)據(jù),直接定義數(shù)據(jù)列表
def creatdata1():
 samples=np.array([[3,-3],[4,-3],[1,1],[1,2]])
 labels=np.array([-1,-1,1,1])
 return samples,labels

def MyPerceptron(samples,labels):
 #定義感知機
 clf=Perceptron(fit_intercept=True,n_iter=30,shuffle=False)
 #訓練感知機
 clf.fit(samples,labels)
 #得到權重矩陣
 weigths=clf.coef_

 #得到截距bisa
 bias=clf.intercept_

 return weigths,bias

#畫圖描繪
class Picture:
 def __init__(self,data,w,b):
  self.b=b
  self.w=w
  plt.figure(1)
  plt.title('Perceptron Learning Algorithm',size=14)
  plt.xlabel('x0-axis',size=14)
  plt.ylabel('x1-axis',size=14)

  xData=np.linspace(0,5,100)
  yData=self.expression(xData)
  plt.plot(xData,yData,color='r',label='sample data')

  plt.scatter(data[0][0],data[0][1],s=50)
  plt.scatter(data[1][0],data[1][1],s=50)
  plt.scatter(data[2][0],data[2][1],s=50,marker='x')
  plt.scatter(data[3][0],data[3][1],s=50,marker='x')
  plt.savefig('3d.png',dpi=75)

 def expression(self,x):
  y=(-self.b-self.w[:,0]*x)/self.w[:,1]
  return y

 def Show(self):
  plt.show()




if __name__ == '__main__':
 samples,labels=creatdata1()
 weights,bias=MyPerceptron(samples,labels)
 print '最終訓練得到的w和b為:',weights,',',bias
 Picture=Picture(samples,weights,bias)
 Picture.Show()

實驗結果:

    最終訓練得到的w和b為: [[-2.  4.]] , [ 0.]

算法三

利用sklearn包中的感知器算法,并進行測試與評估

# -*- encoding:utf-8 -*-
'''
利用sklearn中的的Perceptron進行實驗,并進行測試
'''
from sklearn.datasets import make_classification
from sklearn.linear_model import Perceptron
from sklearn.cross_validation import train_test_split
from matplotlib import pyplot as plt
import numpy as np
#利用算法進行創(chuàng)建數(shù)據(jù)集
def creatdata():

 x,y = make_classification(n_samples=1000, n_features=2,n_redundant=0,n_informative=1,n_clusters_per_class=1)
 '''
 #n_samples:生成樣本的數(shù)量

 #n_features=2:生成樣本的特征數(shù),特征數(shù)=n_informative() + n_redundant + n_repeated

 #n_informative:多信息特征的個數(shù)

 #n_redundant:冗余信息,informative特征的隨機線性組合

 #n_clusters_per_class :某一個類別是由幾個cluster構成的

 make_calssification默認生成二分類的樣本,上面的代碼中,x代表生成的樣本空間(特征空間)
 y代表了生成的樣本類別,使用1和0分別表示正例和反例

 y=[0 0 0 1 0 1 1 1... 1 0 0 1 1 0]
 '''
 return x,y

if __name__ == '__main__':
 x,y=creatdata()

 #將生成的樣本分為訓練數(shù)據(jù)和測試數(shù)據(jù),并將其中的正例和反例分開
 x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=0)

 #正例和反例
 positive_x1=[x[i,0]for i in range(len(y)) if y[i]==1]
 positive_x2=[x[i,1]for i in range(len(y)) if y[i]==1]
 negetive_x1=[x[i,0]for i in range(len(y)) if y[i]==0]
 negetive_x2=[x[i,1]for i in range(len(y)) if y[i]==0]

 #定義感知機
 clf=Perceptron(fit_intercept=True,n_iter=50,shuffle=False)
 # 使用訓練數(shù)據(jù)進行訓練
 clf.fit(x_train,y_train)
 #得到訓練結果,權重矩陣
 weights=clf.coef_
 #得到截距
 bias=clf.intercept_

 #到此時,我們已經(jīng)得到了訓練出的感知機模型參數(shù),下面用測試數(shù)據(jù)對其進行驗證
 acc=clf.score(x_test,y_test)#Returns the mean accuracy on the given test data and labels.
 print '平均精確度為:%.2f'%(acc*100.0)

 #最后,我們將結果用圖像顯示出來,直觀的看一下感知機的結果
 #畫出正例和反例的散點圖
 plt.scatter(positive_x1,positive_x2,c='red')
 plt.scatter(negetive_x1,negetive_x2,c='blue')

 #畫出超平面(在本例中即是一條直線)
 line_x=np.arange(-4,4)
 line_y=line_x*(-weights[0][0]/weights[0][1])-bias
 plt.plot(line_x,line_y)
 plt.show()

實驗結果為:平均精確度為:96.00

通過算法三和算法四可以看出,直接調(diào)用開源包里面的算法還是比較簡單的,思路是通用的。

算法四

我們利用sklearn包中的感知機算法進行分類算法的實現(xiàn)。

# -*- encoding:utf-8 -*-
import numpy as np


'''
以scikit-learn 中的perceptron為例介紹分類算法

應用及其學習分類算法的五個步驟
(1)選擇特征
(2)選擇一個性能指標
(3)選擇一個分類器和一個優(yōu)化算法
(4)評價模型的性能
(5)優(yōu)化算法

以scikit-learn 中的perceptron為例介紹分類算法
1 讀取數(shù)據(jù)-iris
2 分配訓練集和測試集
3 標準化特征值
4 訓練感知器模型
5 用訓練好的模型進行預測
6 計算性能指標
7 描繪分類界面

'''

from sklearn import datasets
import numpy as np
import matplotlib.pyplot as plt

iris=datasets.load_iris()
X=iris.data[:,[2,3]]
y=iris.target

#訓練數(shù)據(jù)和測試數(shù)據(jù)分為7:3
from sklearn.cross_validation import train_test_split
x_train,x_test,y_train,y_test=train_test_split(X,y,test_size=0.3,random_state=0)

#標準化數(shù)據(jù)
from sklearn.preprocessing import StandardScaler
sc=StandardScaler()
sc.fit(x_train)
x_train_std=sc.transform(x_train)
x_test_std=sc.transform(x_test)

#引入skleran 的Perceptron并進行訓練
from sklearn.linear_model import Perceptron
ppn=Perceptron(n_iter=40,eta0=0.01,random_state=0)
ppn.fit(x_train_std,y_train)

y_pred=ppn.predict(x_test_std)
print '錯誤分類數(shù):%d'%(y_test!=y_pred).sum()

from sklearn.metrics import accuracy_score
print '準確率為:%.2f'%accuracy_score(y_test,y_pred)

#繪制決策邊界
from matplotlib.colors import ListedColormap
import warnings

def versiontuple(v):
 return tuple(map(int,(v.split('.'))))

def plot_decision_regions(X,y,classifier,test_idx=None,resolution=0.02):
 #設置標記點和顏色
 markers=('s','x','o','^','v')
 colors=('red','blue','lightgreen','gray','cyan')
 cmap=ListedColormap(colors[:len(np.unique(y))])

 # 繪制決策面
 x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
 x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
 xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
       np.arange(x2_min, x2_max, resolution))
 Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
 Z = Z.reshape(xx1.shape)
 plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap)
 plt.xlim(xx1.min(), xx1.max())
 plt.ylim(xx2.min(), xx2.max())

 for idx, cl in enumerate(np.unique(y)):
  plt.scatter(x=X[y == cl, 0], y=X[y == cl, 1],
     alpha=0.8, c=cmap(idx),
     marker=markers[idx], label=cl)

 if test_idx:
  # 繪制所有數(shù)據(jù)點
  if not versiontuple(np.__version__) >= versiontuple('1.9.0'):
   X_test, y_test = X[list(test_idx), :], y[list(test_idx)]
   warnings.warn('Please update to NumPy 1.9.0 or newer')
  else:
   X_test, y_test = X[test_idx, :], y[test_idx]
  plt.scatter(X_test[:, 0], X_test[:, 1], c='',
    alpha=1.0, linewidth=1, marker='o',
    s=55, label='test set')

def plot_result():
 X_combined_std = np.vstack((x_train_std, x_test_std))
 y_combined = np.hstack((y_train, y_test))

 plot_decision_regions(X=X_combined_std, y=y_combined,
      classifier=ppn, test_idx=range(105,150))
 plt.xlabel('petal length [standardized]')
 plt.ylabel('petal width [standardized]')
 plt.legend(loc='upper left')

 plt.tight_layout()
 plt.show()

plot_result()

實驗結果為:錯誤分類數(shù):4;準確率為:0.91

<完>

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

相關文章

  • 巧用Python裝飾器 免去調(diào)用父類構造函數(shù)的麻煩

    巧用Python裝飾器 免去調(diào)用父類構造函數(shù)的麻煩

    巧用Python裝飾器 免去調(diào)用父類構造函數(shù)的麻煩,需要的朋友可以參考下
    2012-05-05
  • Python?async+request與async+aiohttp實現(xiàn)異步網(wǎng)絡請求探索

    Python?async+request與async+aiohttp實現(xiàn)異步網(wǎng)絡請求探索

    這篇文章主要介紹了Python?async+request與async+aiohttp實現(xiàn)異步網(wǎng)絡請求探索,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2022-10-10
  • Python 字符串操作詳情

    Python 字符串操作詳情

    這篇文章主要介紹了Python 字符串操作,所謂字符串,就是由0個或者多個字符組成的有限序列,字符串的字符可以是特殊符號、英文字母、中文字符、日文的平假名或片假名、希臘字母、Emoji字符等等。下面我們大家一起來學習文章詳細內(nèi)容吧
    2021-11-11
  • python重要函數(shù)eval多種用法解析

    python重要函數(shù)eval多種用法解析

    這篇文章主要介紹了python重要函數(shù)eval多種用法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-01-01
  • Python實現(xiàn)將通信達.day文件讀取為DataFrame

    Python實現(xiàn)將通信達.day文件讀取為DataFrame

    今天小編就為大家分享一篇Python實現(xiàn)將通信達.day文件讀取為DataFrame,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • python實現(xiàn)支付寶當面付(掃碼支付)功能

    python實現(xiàn)支付寶當面付(掃碼支付)功能

    這篇文章主要為大家詳細介紹了python實現(xiàn)支付寶當面付,掃碼支付功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • python之multimethod包多分派解讀

    python之multimethod包多分派解讀

    這篇文章主要介紹了python之multimethod包多分派問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Python中requests.session()的用法小結

    Python中requests.session()的用法小結

    這篇文章主要介紹了Python中requests.session()的用法小結,可能大家對?session?已經(jīng)比較熟悉了,也大概了解了session的機制和原理,但是我們在做爬蟲時如何會運用到session呢,接下來要講到會話保持,需要的朋友可以參考下
    2022-11-11
  • HTML中使用python屏蔽一些基本功能的方法

    HTML中使用python屏蔽一些基本功能的方法

    這篇文章主要介紹了HTML中使用python屏蔽一些基本功能的方法,需要的朋友可以參考下
    2017-07-07
  • Pandas中DataFrame的分組/分割/合并的實現(xiàn)

    Pandas中DataFrame的分組/分割/合并的實現(xiàn)

    這篇文章主要介紹了Pandas中DataFrame的分組/分割/合并的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-07-07

最新評論