# 数学之路(三)-机器学习(3)-机器学习算法-神经网络[21]

www.MyException.Cn  网友分享于：2013-09-10  浏览：18次

[0,0,1]

[0,1,0]

[1,0,0]

```#x和d样本初始化
train_x =[]
d=[]
sp_d=[]
sp_d.append([0,0,1])
sp_d.append([0,1,0])
sp_d.append([1,0,0])
#读取图片
for ii in xrange(1,4):
for jj in xrange(1,3):
fn='p'+str(ii)+'-'+str(jj)+'.jpg'
pictz=readpic(fn)
train_x.append(pictz)
d.append(sp_d[ii-1])

myinput=np.array(train_x)
mytarget=np.array(d)
mymax=np.max(myinput)```

```    print u'\n训练神经网络完毕'
pl.subplot(111)
pl.plot(err)
pl.xlabel('Epoch number')
pl.ylabel('error (default SSE)')
print u"对样本进行测试"
simd= bpnet.sim(myinput)
mysimd=getresult(simd)
print mysimd
print u"进行仿真"
testpictz=np.array([readpic('ptest1.jpg')])
simtest=bpnet.sim(testpictz)
mysimtest=getresult(simtest)
print mysimtest
testpictz=np.array([readpic('ptest2.jpg')])
simtest=bpnet.sim(testpictz)
mysimtest=getresult(simtest)
print mysimtest
testpictz=np.array([readpic('ptest3.jpg')])
simtest=bpnet.sim(testpictz)
mysimtest=getresult(simtest)
print mysimtest
pl.show()```

```def getresult(simjg):
jg=[]
for j in xrange(0,len(simjg)):
maxjg=-2
nowii=0
for i in xrange(0,len(simjg[0])):
if simjg[j][i]>maxjg:
maxjg=simjg[j][i]
nowii=i
jg.append(nowii+1)
return jg```