mplannliner 艾伯特

这是艾伯特机器学习实践的mplannliner模块代码,小编把它打出来费了不少时间,这是Python3版本的跟Python2几乎没有区别,只是print的格式不同,小编还在代码的一些地方加了自己的注释

import numpy as np
import math
import matplotlib.pyplot as plt
class Mplannliner:
def init(self):
self.b=1
self.a0=0.1
self.a=0.0
self.r=20.0
self.expect_e=0.05
self.traincount=100
self.testpoint=[]
def testpoint_init(self):
self.testpoint=[]
def e_init(self,mye):
self.expect_e=mye
def samples_init(self,mysamples): #mysamples[ [features],[labels]]
my_x=[]
my_d=[]
my_w=[self.b]
myexamp=mysamples
for mysmp in myexamp:
tempsmp=[1]+mysmp[0] #[1] is bias
my_x.append(tempsmp)
my_d.append(mysmp[1])
for i in range(len(my_x[0])-1):
my_w.append(0.0)
self.x = np.array(my_x)
self.d = np.array(my_d)
self.w = np.array(my_w)
def a_init(self,mya):
self.a0=mya
def r_init(self,myr):
self.r=myr
def maxtry_init(self,maxc):
self.traincount=maxc
def sgn(self,v):
if v>0:
return 1
else:
return -1
def get_v(self,myw,myx):
return self.sgn(np.dot(myw.T,myx))
def neww(self,oldw,myd,myx,a,mycount):
mye=self.get_e(oldw,myx,myd)
self.a=self.a0/(1+mycount/float(self.r))
return (oldw+amyemyx,mye)
def get_e(self,myw,myx,myd):
return myd-self.get_v(myw,myx)
def train(self):
mycount=0
while True:
mye=0
i=0
for xn in self.x:
self.w,e=self.neww(self.w,self.d[i],xn,self.a,mycount)
i+=1
mye+=pow(e,2)
mye=math.sqrt(mye)
mycount+=1
print(“第{}次调整中…误差: {}”.format(mycount,mye))
if abs(mye)<self.expect_e or mycount>self.traincount:
if mycount>self.traincount:
print(“已经达到最大训练次数:”,mycount)
break
def simulate(self,testdata):
if self.get_v(self.w,np.array([1]+testdata))>0:
return 1
else:
return -1
def drawponint_add(self,point):
self.testpoint.append(point)
def draw2d(self):
temp_x=[]
temp_y=[]
i=0
for mysamp in self.x:
temp_x.append(mysamp[1])
temp_y.append(mysamp[2])
if self.d[i]>0:
plt.plot(mysamp[1],mysamp[2],‘or’)
else:
plt.plot(mysamp[1],mysamp[2],‘og’)
i+=1
mytestpointx=[]
mytestpointy=[]
for addpoint in self.testpoint:
if self.simulate(addpoint)==1:
plt.plot(addpoint[0],addpoint[1],'r’)
else:
plt.plot(addpoint[0],addpoint[1],'g’)
mytestpointx.append(addpoint[0])
mytestpointy.append(addpoint[1])
x_max=max(max(temp_x),max(mytestpointx))+5
x_min=min(min(temp_x),min(mytestpointx))
y_max=max(max(temp_y),max(mytestpointy))+5
y_min=min(min(temp_y),min(mytestpointy))
if x_min>0:
x_min=0
if y_min>0:
y_min=0
plt.xlabel(u’x1’)
plt.xlim(x_min,x_max)
plt.ylabel(u’x2’)
plt.ylim(y_min,y_max)
plt.title(“ANN-LINER[red:+ ,green:-]”)
# draw line
lp_x1=[x_min,x_max]
lp_x2=[]
myb=self.w[0]
myw1=self.w[1]
myw2=self.w[2]
myy=(-myb-myw1
lp_x1[0])/float(myw2)
lp_x2.append(myy)
myy=(-myb-myw1
lp_x1[1])/float(myw2)
lp_x2.append(myy)
plt.plot(lp_x1,lp_x2,‘b–’)
plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值