The original problem is to write a BP neural network to fit the watermelon data set, watermelon data set I have been numerically the following:
number, color, foundine, knock, texture, navel, touch, density, sugar content, good melon 1,1,1,3,1,1,1,0.697,0.46,12,2,1,2,1,1,1,0.774,0.376,13,2,1,3,1,1,1,0.634,0.264,14,1,1,2,1,1,1,0.608,0.318,15,3,1,3,1,1,1 , 0.556,0.215,16,1,2,3,1,2,2,0.403,0.237,17,2,2,3,2,2,2,0.481,0.149,18,2,2,3,1,2,1,0.437,0.211,19,2,2,2,2,2,1,0.666,0.091 , 010,1,3,1,1,3,2,0.243,0.267, 011,3,3,1,3,3,1,0.245,0.057, 0 12,3,1,3,3,3,2,0.343,0.099, 013,1,2,3,2,1,1,0.639,0.161,014,3,2,2,2,1,1,0.657,0.198, 0 15,2,2,3,1,2,2,0.36,0.37, 016,3,1,3,3,3,1,0.593,0.042, 017,1,1,2,2,2,1,0.719,0.103,0
The Pybrain library is then called to create a single hidden layer neural network with 50 cells, as follows
#!/usr/bin/python
#-*-Coding:utf-8-*-
Import NumPy as NP
Import Matplotlib.pyplot as Plt
From matplotlib import Colors
From pybrain.tools.shortcuts import buildnetwork
From pybrain.supervised.trainers import Backproptrainer
From pybrain.datasets import Superviseddataset
file1 = open (' C:\quant\watermelon.csv ', ' R ')
data = [Line.strip (' \ n '). Split (', ') for line in File1]
data = Np.array (data)
X = [Raw for Raw in data[1:,1:-1]]
y = [1 if raw[-1]== ' 1 ' else 0 for Raw in data[1:]]
x = Np.array (x)
y = Np.array (y)
Print x, y
###################################################################### #以上是西瓜
FNN = Buildnetwork (8,50,1)
DS = Superviseddataset (8,1)
For a, b in zip (x, y):
Ds.addsample (A, B)
# Trainer using BP algorithm
# verbose = True when training will print the total error, the ratio of the default training set and the validation set in the library is 4:1, can be changed in parentheses
Trainer = Backproptrainer (FNN, DS, verbose = True, learningrate=0.01)
# Maxepochs is the maximum number of convergence iterations you need, the method used here is training to convergence, I generally set to 1000
Trainer.trainuntilconvergence (maxepochs=10000)
# Activate function is the output value of the predicted X2 after the neural network training
For a, b in zip (x, y):
Prediction = Fnn.activate (a)
Print Prediction,b
Below are the comparison of the effects of training 10,000 and 1000 times:
training 10,000 times, left is training result, right is ideal output [ 0.99417443] 1[ 0.99774329] 1[ 1.00390992] 1[ 0.99456691] 1[ 0.99167349] 1[ 0.99627566 ] 1[ -0.16419402] 1[ 0.99678622] 1[ -0.00259512 0[ 1.46741515 0.57305884 0[ -0.00284737 "0[- 0.0029103 0[ -0.00400758 0[ 1.19899233 0[ -0.00333452 -0.26766382] 0
0.3439171] 10.71063964] 10.86324691] 10.39205173] 10.97416348] 1 0.55886924] 10.1247508] 11.6945434] 10.383524440.7585709] 0[- 0.232125590.03274158] 0[-0.33641601] 0[-0.651058171.22768539 0.11638493] 0[-0.13244805] 0
It can be seen that the training error of 10,000 times is obviously much lower, but there may have been a fitting problem.
Reference article: http://www.zengmingxia.com/use-pybrain-to-fit-neural-networks/
"Machine learning" Zhou Zhihua exercise answer 5.5