程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

BP算法鸢尾花分类

发布于2019-10-11 15:04     阅读(1709)     评论(0)     点赞(6)     收藏(1)


BP算法鸢尾花分类


网上很多鸢尾花例子,学习其他人后仿写,我在运行其他人的时候会有溢出和错误。
下述代码准确率95%
附鸢尾花数据集链接:https://pan.baidu.com/s/1J3-2INUT19BVZWCUet3X6A
提取码:y07d
如果有什么不对的或者有什么不懂Iamzhubaoliang@yeah.net
工程地址https://git.lug.ustc.edu.cn/Iamzhubaoliang/irissort

import math
import random
import pandas as pd

random.seed(0)
def rand(a,b):
    return (b-a)*random.random()+a
def dsigmoid(x):
    return x*(1-x)
def sigmoid(gamma):
  if gamma < 0:
    return 1 - 1/(1 + math.exp(gamma))
  else:
    return 1/(1 + math.exp(-gamma))
def makematrix(I,J):
    m = []
    for i in range(I):
        m.append([0.0]*J)#一维构造二维
    return m


class BP:
    ''' 三层BP网络'''
    def __init__(self,ni,nh,no):
        self.ni=ni+1
        self.nh=nh+1
        self.no=no

        self.ai=[1.0]*self.ni
        self.ah=[1.0]*self.nh
        self.ao=[1.0]*self.no

        self.wi=makematrix(self.ni,self.nh)
        self.wo=makematrix(self.nh,self.no)

        for i in range(self.ni):
            for j in range(self.nh):
                self.wi[i][j]=rand(-2,2)

        for i in range(self.nh):
            for j in range(self.no):
                self.wo[i][j]=rand(-2,2)

    def Forward(self,input):
        for i in range(self.ni-1):
            self.ai=input
        for i in range(self.nh):
            sum=0.0
            for j in range(self.ni-1):
                #print(self.ai[j])
                sum+=self.wi[j][i]*self.ai[j]
            self.ah[i]=sigmoid(sum)
        for i in range(self.no):
            sum=0.0
            for j in range(self.no):
                sum+=self.wo[j][i]*self.ah[j]
            self.ao[i]=sigmoid(sum)
        return self.ao
    def backward(self,tartget,lv):
        rerror=0.0
        outerror=[1.0]*self.no
        for i in range(self.no):
            error=tartget[i]-self.ao[i]
            rerror=error
            outerror[i]=dsigmoid(self.ao[i])*error

        herror=[1.0]*self.nh
        for i in range(self.nh):
            error=0.0
            for j in range(self.no):
                error+=outerror[j]*self.wo[i][j]
            herror[i]=dsigmoid(self.ah[i])*error

        for i in range(self.nh):
            for j in range(self.no):
                change=outerror[j]*self.ah[i]
                self.wo[i][j]=self.wo[i][j]+lv*change

        for i in range(self.ni-1):
            for j in range(self.nh):
                change=herror[j]*self.ai[i]
                self.wi[i][j]=self.wi[i][j]+lv*change

        error = 0.0
        error += 0.5 * rerror ** 2
        return error

    def train(self, patterns, iterations, lr=0.009):
        # lr: 学习速率(learning rate)
        for i in range(iterations):
            error = 0.0
            for p in patterns:
                inputs = p[0]
                targets = p[1]
                #print(inputs, ",", p[1])
                self.Forward(inputs)
                error = error + self.backward(targets, lr)
            if i%100==0:
                print('error: %-.9f' % error)
    def test(self,patterns):
        count=0
        for p in patterns:
            target=p[1]
            #print(target)
            result=self.Forward(p[0])
            index = result.index(max(result))
            if index==0 and target==[1,0,0]:
                count=count+1
            elif index==1 and target==[0,1,0]:
                    count=count+1
            elif index==2 and target==[0,0,1]:
                count = count + 1
        accuracy = float(count / len(patterns))
        print('accuracy: %-.9f' % accuracy)
def iris():
    data=[]
    raw=pd.read_csv('iris.csv')
    raw_data=raw.values
    raw_feature=raw_data[:,1:6]
    print(raw_feature)
    for i in range(len(raw_feature)):
        ele=[]
        ele.append(raw_feature[i])
        if raw_data[i][5]=='setosa':
            ele.append([1,0,0])
        elif raw_data[i][5]=='versicolor':
            ele.append([0,1,0])
        else:
            ele.append([0,0,1])
        print(ele)
        data.append(ele)
    random.shuffle(data)
    trainning=data[0:100]
    test=data[101:]
    nn=BP(4,7,3)
    nn.train(data,10000)
    nn.test(test)
if __name__ == '__main__':
    iris()


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141


所属网站分类: 技术文章 > 博客

作者:加班是一种习惯

链接:https://www.pythonheidong.com/blog/article/134405/db9e98512dff64e8ae96/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

6 0
收藏该文
已收藏

评论内容:(最多支持255个字符)