感知機 python 代碼實現 —– 統計學習方法

來源:互聯網
上載者:User

感知機 python 代碼實現  ----- 統計學習方法

參考: http://shpshao.blog.51cto.com/1931202/1119113

 

 

 1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # 4 #  未命名.py 5 #   6 #  Copyright 2013 t-dofan <t-dofan@T-DOFAN-PC> 7 #   8 #  This program is free software; you can redistribute it and/or modify 9 #  it under the terms of the GNU General Public License as published by10 #  the Free Software Foundation; either version 2 of the License, or11 #  (at your option) any later version.12 #  13 #  This program is distributed in the hope that it will be useful,14 #  but WITHOUT ANY WARRANTY; without even the implied warranty of15 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the16 #  GNU General Public License for more details.17 #  18 #  You should have received a copy of the GNU General Public License19 #  along with this program; if not, write to the Free Software20 #  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,21 #  MA 02110-1301, USA.22 #  23 #  24 25 class Perceptron:26     #初始化27     def __init__(self,learnrate,w0,w1,b):28         self.learnrate = learnrate29         self.w0 = w030         self.w1 = w131         self.b = b32         33     #模型34     def model(self,x):35         result = x[2]*(self.w0*x[0] + self.w1*x[1] + self.b)36         return result37 38     #策略39     def iserror(self,x):40         result = self.model(x)41         if result <= 0:42             return True43         else:44             return False45             46     ##演算法 ---> 這裡的learnrate 代表。。。。。。。。。。。47     #調整策略: Wi = Wi + n*wixi48     def gradientdescent(self,x):49         self.w0 = self.w0 + self.learnrate * x[2] * x[0] #根據調整策略,此處是否需要*x[2] ? 50         self.w1 = self.w1 + self.learnrate * x[2] * x[1]51         self.b = self.b + self.learnrate * x[2]52 53 54     #訓練55     def traindata(self,data):56         times = 057         done = False58         while not done:59             for i in range(0,6):60                 if self.iserror(data[i]):61                     self.gradientdescent(data[i])62                     times += 163                     done = False64                     break65                 else:66                     done = True    67         print times68         print "rightParams:w0:%d,w1:%d,b:%d" %(self.w0 , self.w1 , self.b)69 70     def testmodel(self,x):71         result  = self.w0*x[0] + self.w1*x[1] + self.b72         if result > 0:73             return 174         else:75             return -176 77 78 def main():79     p = Perceptron(1,0,0,0)80     data = [[3,3,1],[4,3,1],[1,1,-1],[2,2,-1],[5,4,1],[1,3,-1]] 81     testdata = [[4,4,-1],[1,2,-1],[1,4,-1],[3,2,-1],[5,5,1],[5,1,1],[5,2,1]]82     p.traindata(data)83     for i in testdata:84         print "%d  %d  %d" %(i[0],i[1],p.testmodel(i))85         86     return 087 88 if __name__ == '__main__':89     main()

 

仍有幾處疑問, 書上的調整策略為:Wi = Wi + nYi*Xi, 因此在最佳化過程中是否有必要乘以x[2] ?

--------------------------------

看錯了,此時x[2] 即為yi

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.