zoukankan      html  css  js  c++  java
  • 给定数据利用神经网络算法模型进行计算

    给定数据利用神经网络算法模型进行计算,利用FP、BP算法,求得模型最优值。

    神经网络初步学习使用。

     1 import numpy as np
     2 import matplotlib.pylab as plt
     3 from numpy import *
     4 from pylab import *
     5 
     6 from sklearn.neural_network import MLPClassifier
     7 
     8 # 中文 负号
     9 mpl.rcParams['font.sans-serif']=['SimHei']
    10 matplotlib.rcParams['axes.unicode_minus']=False
    11 
    12 X1 = [0.697,0.774,0.634,0.608,0.556,0.403,0.481,0.437,0.666,0.243,0.245,0.343,0.639,0.657,0.360,0.593,0.719]
    13 X2 = [0.460,0.376,0.264,0.318,0.215,0.237,0.149,0.211,0.091,0.267,0.057,0.099,0.161,0.198,0.370,0.042,0.103]
    14 Y = [1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0]
    15 
    16 # 数据初始化
    17 m = len(X1)
    18 X = np.c_[np.ones(m),X1,X2]
    19 y = np.c_[Y]
    20 
    21 # 数据重新洗牌/打乱
    22 np.random.seed(3)
    23 order=np.random.permutation(m)
    24 X=X[order]
    25 y=y[order]
    26 
    27 # 将数据切割 分为训练集和测试集
    28 d = int(m*0.75)
    29 train_X,test_X=np.split(X,[d,])
    30 train_y,test_y=np.split(y,[d,])
    31 
    32 # 定义Sigmoid函数g(z)函数
    33 def g(z,deriv=False):
    34     if deriv == True:
    35         return z*(1-z)
    36     return 1.0/(1.0+np.exp(-z))
    37 
    38 # 初始化theta值 定义神经网络网格结构  L1=3,L2=17,L3=1
    39 np.random.seed(3)
    40 theta1=2*np.random.random((3,17))-1
    41 theta2=2*np.random.random((17,1))-1
    42 
    43 # 初始化代价值
    44 J_hietory = np.zeros(15000)
    45 
    46 # 开始神经网络迭代
    47 for i in range(15000):
    48     # 前项传播算法
    49     a1 = train_X
    50     z2 = a1.dot(theta1)
    51     a2 = g(z2)
    52     z3 = a2.dot(theta2)
    53     a3 = g(z3)
    54     h = a3
    55 
    56 
    57     # 记录每次代价
    58     J_hietory[i] = -1.0/m*(np.dot(train_y.T,np.log(h))+np.dot((1-train_y).T,np.log((1-h))))
    59 
    60     # 反向传播算法
    61 
    62     # 每层的delta
    63     delta_L3 = a3-train_y
    64     delta_L2 = delta_L3.dot(theta2.T)*g(a2,True)
    65     # 每层的deltatheta
    66     deltatheta2 = 1.0/m*np.dot(a2.T,delta_L3)
    67     deltatheta1 = 1.0/m*np.dot(a1.T,delta_L2)
    68     # 每层的theta更新
    69     theta2 -= 0.8*deltatheta2
    70     theta1 -= 0.8*deltatheta1
    71 
    72 plt.plot(J_hietory)
    73 plt.show()
    74 
    75 
    76 # 定义准确率函数
    77 def testAccuracy(X,y):
    78     m = X.shape[0]
    79     count = 0
    80 
    81     for i in range(m):
    82         a1 = X[i]
    83         z2 = a1.dot(theta1)
    84         a2 = g(z2)
    85         z3 = a2.dot(theta2)
    86         a3 = g(z3)
    87         h = a3
    88 
    89         if bool(np.where(h>=0.5,1,0)) == bool(y[i]):
    90             count += 1
    91     return count/m
    92 
    93 # 分别计算训练集和测试集的准确率
    94 print('训练集的准确率:',testAccuracy(train_X,train_y)*100,'%')
    95 print('训练集的准确率:',testAccuracy(test_X,test_y)*100,'%')
  • 相关阅读:
    『BASH』——文件权限批量恢复脚本——「Permission Revovery」
    拾遗:基础知识回顾01
    C之:微代码——柱状图(link_list、struct)
    拾遗:yes 是一个很有用的小命令
    『BASH』
    拾遗:『ext4 Quota』
    循环动态生成html并且绑定事件带参函数
    asp.net页面间传递数据的方法(ZZ)
    Session对象概述
    HTML DOM Introduction 翻译 w3schools.com
  • 原文地址:https://www.cnblogs.com/qiao101/p/10082940.html
Copyright © 2011-2022 走看看