Python implementation of simple BP neural network __python

Source: Internet
Author: User
Tags rand

Although the neural network has a very complete and useful framework, and BP Neural network is a relatively simple and inefficient one, but for the purpose of learning to achieve this neural network is still meaningful, I think.

The following program uses the iris dataset, in order to facilitate the drawing first with PCA to the data to reduce the dimension. At the same time, the classification results are labeled, according to the characteristics of neural networks, three neurons as output to represent three different categories, the code is as follows:

Import math import random from sklearn.decomposition import PCA from sklearn.cross_validation import train_test_split impo RT Matplotlib.pyplot as PLT import matplotlib as MPL import NumPy as NP from sklearn.metrics import Accuracy_score def t Rtype (s): #定义类别转换函数 types = {' Iris-setosa ': 0, ' Iris-versicolor ': 1, ' Iris-virginica ': 2} return types[s] data = n P.loadtxt (' Iris.data ', delimiter= ', ', converters={4:trtype}) #读入数据, the fifth column is converted to category 012 X,y = np.split (data, (4,), Axis=1) # Split data and label PCA=PCA (n_components=2) x=pca.fit_transform (x) #为方便绘图, PCA reduction to X to two-dimensional #划分测试集和训练集 def label_tr (y): #标签转换, 
    Converts one-dimensional labels to three-dimensional L = {0:[1,0,0],1:[0,1,0],2:[0,0,1]} ys = [] for i in range (len (y)): Ys.append (L[int (y[i))] Return Np.array (YS) def inv_label_tr (y_1d): #标签转换逆过程 y_pres = [] for I in range (Y_1d.shape[0]): FO R j in range (3): if (y_1d[i][j]==1): y_lable = J Y_pres.append (y_lable) r Eturn Np.array (y_pres) y = label_tr (y) #划分数According to X_train, X_test, y_train, y_test = Train_test_split (x, Y, random_state=1, train_size=0.6) random.seed (0) def rand (A, b): #随机数函数 return (b-a) * Random.random () + A def make_matrix (M, N, fill=0.0): #矩阵生成函数 mat = [] for i in RA Nge (M): Mat.append ([fill] * N) return Mat def sigmoid (x): #激活函数 return 1.0/(1.0 + MATH.EXP (x)) def s Igmoid_derivative (x): #激活函数求导 return x * (1-x) class Bpneuralnetwork: #BP神经网络类 def __init__ (self): #初始化 s Elf.input_n = 0 Self.hidden_n = 0 self.output_n = 0 self.input_cells = [] Self.hidden_cell s = [] Self.output_cells = [] Self.input_weights = [] Self.output_weights = [] Self.input_ Correction = [] Self.output_correction = [] def setup (self, NI, NH, no): #初始化输入, hidden layer, output number SE Lf.input_n = ni + 1 self.hidden_n = NH Self.output_n = no # initialization Neuron self.input_cells = [1.0
       ] * Self.input_n Self.hidden_cells = [1.0] * Self.hidden_n self.output_cells = [1.0] * self.output_n # Initialize weight matrix sel F.input_weights = Make_matrix (Self.input_n, self.hidden_n) self.output_weights = Make_matrix (Self.hidden_n, SELF.O
                UTPUT_N) # Initialize weight for I in range (Self.input_n): for h in range (Self.hidden_n): SELF.INPUT_WEIGHTS[I][H] = rand ( -0.2, 0.2) for h in range (Self.hidden_n): For O in range (self.output_n ): Self.output_weights[h][o] = rand (-2.0, 2.0) # Initialize bias self.input_correction = Make_matrix (Self.input_n, self.hidden_n) self.output_correction = Make_matrix (Self.hidden_n, self.output_n) def Predic
        T (self, inputs): # Activate input layer for I in range (self.input_n-1): self.input_cells[i] = inputs[i]
                # Activate the hidden layer for J. Range (self.hidden_n): total = 0.0 to I in range (Self.input_n): Total = self. input_cells[i] * Self.input_weights[i][j] self.hidden_cells[j] = sigmoid (total) # Activate output layer for K In range (self.output_n): total = 0.0 to J in range (self.hidden_n): total = self . hidden_cells[j] * Self.output_weights[j][k] self.output_cells[k] = sigmoid (total) return Self.output_
        cells[:] def back_propagate (self, case, label, learn, correct): # Reverse propagation self.predict (case) # Find out the output error Output_deltas = [0.0] * self.output_n for O in range (self.output_n): Error = Label[o]
        -Self.output_cells[o] output_deltas[o] = sigmoid_derivative (Self.output_cells[o]) * ERROR # to find the hidden layer error Hidden_deltas = [0.0] * Self.hidden_n for H in range (self.hidden_n): Error = 0.0 for o in range (self.output_n): Error + output_deltas[o] * Self.output_weights[h][o] Hidden_deltas [H] = sigmoid_derivative(Self.hidden_cells[h]) * ERROR # Update output weights for h in range (Self.hidden_n): For O in range (self.out Put_n): Change = output_deltas[o] * Self.hidden_cells[h] self.output_weights[h][o] + = Lear 
        n * changes + correct * self.output_correction[h][o] self.output_correction[h][o] = change # update input weights For I in Range (Self.input_n): for h in range (self.hidden_n): Change = Hidden_deltas[h 
                ] * Self.input_cells[i] self.input_weights[i][h] + = learn * change + correct * self.input_correction[i][h] SELF.INPUT_CORRECTION[I][H] = change # find Global error = 0.0 for o in range (Len label ): Error + 0.5 * (Label[o]-self.output_cells[o]) * * 2 return Error def train (self, cases,
            Labels, limit=10000, learn=0.05, correct=0.1): #训练神经网络 for J in range (limit): Error = 0.0 For I in range (lEn (cases)): label = labels[i] case = cases[i] Error + = Self.back_propagate (case, label, learn, correct) def fit (self,x_test): #离散预测函数用于输出数据 y_pre_1d = [] for case in X_tes t:y_pred = self.predict (case) for I in Range (len (y_pred)): if (y_pred[i] = max (y
        _pred)): y_pred[i] = 1 Else:y_pred[i] = 0 y_pre_1d.append (y_pred) Return Inv_label_tr (Np.array (y_pre_1d)) def fit2 (self,x_test): #连续预测函数用于画图 y_pre_1d = [] for CAs E in x_test:w = Np.array ([0,1,2]) y_pred = self.predict (case) Y_pre_1d.append (Np.arra Y (y_pred). dot (w.t)) return Np.array (y_pre_1d) If __name__ = ' __main__ ': #主函数 nn = bpneuralnetwork () nn.s Etup (2, 5, 3) #初始化 Nn.train (X_train, Y_train, 100000, 0.05, 0.1) #训练 y_pre_1d = Nn.fit (x_test) #测试 y_test_1d = in V_LABEL_TR (Y_test) pRint Accuracy_score (y_pre_1d,y_test_1d) #打印测试精度 #画图 mpl.rcparams[' font.sans-serif '] = [u ' simhei '] mpl.rcparams[' a Xes.unicode_minus '] = False cm_light = Mpl.colors.ListedColormap ([' #FFA0A0 ', ' #A0FFA0 ', ' #A0A0FF ']) Cm_dark = mpl.colors . Listedcolormap ([' #AAAAFF ', ' #FFAAAA ', ' #AAFFAA ']) x1_min, X1_max = x[:, 0].min (), x[: 0].max () # The range of the No. 0 column x2_min, x2_ma x = x[:, 1].min (), x[:, 1].max () # The range of the 1th column x1, x2 = np.mgrid[x1_min:x1_max:200j, x2_min:x2_max:200j] # Generate grid sampling points grid_tes   t = Np.stack ((X1.flat, X2.flat), Axis=1) # test Point grid_hat = Nn.fit2 (grid_test) #预测结果 grid_hat = Grid_hat.reshape (X1.shape) # make it the same shape as the input Plt.pcolormesh (x1, x2, Grid_hat, Cmap=cm_light) plt.scatter (x[:, 0], x[:, 1], c=y, edgecolors= ' K ', s=50, Cmap=cm_dark) plt.title (U ' bpnn two feature classification ', fontsize=15) plt.show () print Grid_hat.shape
The final classification results are rated at 0.983, but it can be seen in the figure below that the boundary between the green and blue dots has a problem of fitting, a problem that will be solved slowly in the future.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.