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

我们也可以用第三方python的神经网络包来对上节的数据模型进行训练

首先读取数据

 

#!/usr/bin/env python

#-*- coding: utf-8 -*-

#code:[email protected]

#http://blog.csdn.net/myhaspl

import numpy as np

import pylab as pl

import neurolab as nl











print 'http://blog.csdn.net/myhaspl'

print '[email protected]'

print

print u'正在处理中'



#x和d样本初始化

train_x =[]

d=[]

f = open("cubage.csv")  

try:  

    f_text = f.read( ) 

finally:  

    f.close( ) 

x_text=f_text.split('\n')

for line_i in xrange(0,len(x_text)):

    line=x_text[line_i]

    if line_i>1 and len(line)>0:

        train_x.append([])

        hdata=line.split(',')

        train_x[line_i-2].append(float(hdata[0]))

        d.append([float(hdata[1])])

 

然后开始训练并仿真

 

本博客所有内容是原创,如果转载请注明来源

http://blog.csdn.net/myhaspl/

 

 

 

 

print u'\n训练神经网络中...'

err = bpnet.train(myinput, mytarget, epochs=800, show=5, goal=0.0005)

if err[len(err)-1]>0.001:

    print u'\n训练神经网络失败...\n'

else:

    print u'\n训练神经网络完毕'    







pl.subplot(211)

pl.plot(err)  

pl.xlabel('Epoch number')

pl.ylabel('error (default SSE)')

#对样本进行测试

simd= bpnet.sim(myinput)

temp_x=myinput

temp_d=mytarget

simd/=tz

temp_y=simd

temp_d/=tz  



              

x_max=np.max(temp_x)

x_min=np.min(temp_x)-5

y_max=np.max(temp_y)+2

y_min=np.min(temp_y)

    

pl.subplot(212)

pl.xlabel(u"x")

pl.xlim(x_min, x_max)

pl.ylabel(u"y")

pl.ylim(y_min, y_max)

lp_x1 = temp_x

lp_x2 = temp_y

lp_d = temp_d

pl.plot(lp_x1, lp_x2, 'r-')

pl.plot(lp_x1,lp_d,'b*')

>>> runfile(r'K:\book_prog\ann_bpdatanh2.py', wdir=r'K:\book_prog')
http://blog.csdn.net/myhaspl
[email protected]


正在处理中


正在建立神经网络


训练神经网络中...
Epoch: 5; Error: 0.00799705870758;
Epoch: 10; Error: 0.000558856952172;
The goal of learning is reached


训练神经网络完毕
>>> 
最后数据模型拟合效果图如下:

 

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

 

你可能感兴趣的:(机器学习)