zoukankan      html  css  js  c++  java
  • AdaBoost算法实现

    # -*- coding: utf-8 -*-
    # ---------------------------------------------------------------------------
    # AdaBoost.py
    # Created on: 2014-06-12 09:49:56.00000
    # Description:
    # ---------------------------------------------------------------------------
    
    import sys
    import math
    import numpy as np
    
    breakValues = (2.5, 5.5, 8.5)
    X = np.array([0,1,2,3,4,5,6,7,8,9])
    Y = np.array([1,1,1,-1,-1,-1,1,1,1,-1])
    W1 = np.array([0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1])
        
    def Classifier25(x):
        if x <= 2.5:
            return 1
        else:
            return -1
        
    def Classifier55(x):
        if x >= 5.5:
            return 1
        else:
            return -1
        
    def Classifier85(x):
        if x <= 8.5:
            return 1
        else:
            return -1
        
    def ClassifyArray(XArray, Classifier):
        YY = []
        for x in XArray:
            YY.append(Classifier(x))
        print(YY)
        return YY
    def ErrorSum(YY):
        i = 0
        errorValue = 0;
        for y in YY:
            if y != Y[i]:
                errorValue += W1[i]
            i = i+1
        return errorValue
    
    def ErrorAllSum(ExpressArray):
        i = 0
        errorValue = 0;
        for x in X:
            value = 0
            for express in ExpressArray:
                value += express[0] * express[1](x)
            if value > 0:
                value = 1
            else:
                value = -1
            if value != Y[i]:
                errorValue += 0.1
            i = i+1
        return errorValue
    
    
    def SelectClassifierFunction(XArray):
        ClassifierArray = [Classifier25, Classifier55, Classifier85]
        errArray = []
        value = float('NaN')
        errMin = float('Inf')
        for classifier in ClassifierArray:
            #计算分类的结果值
            YY = ClassifyArray(XArray, classifier)
            #计算分类的错误率
            errorValue = ErrorSum(YY)
            errArray.append(errorValue)
            if errorValue < errMin:
                errMin = errorValue
                value = classifier
        print(errArray)
        print(value.__name__)
        return value
    
    print(W1)
    
    '''
    print('--------------------------------')
    classifier = SelectClassifierFunction(X)
    #计算分类的结果值
    G = ClassifyArray(X, classifier)
    #计算分类的错误率
    e = ErrorSum(G)
    a = 0.5 * math.log((1-e)/e)
    a = round(a, 4)
    print(a)
    W2 = W1*np.exp(-a*Y*np.array(G))
    Zm = np.sum(W2)
    #Zm = round(Zm, 4)
    print(Zm)
    W1 = W2 / Zm
    print(W1)
    
    print('--------------------------------')
    
    W1 = np.array([0.0715,0.0715,0.0715,0.0715,0.0715,0.0715,0.1666,0.1666,0.1666,0.07151])
    classifier = SelectClassifierFunction(X)
    #计算分类的结果值
    G = ClassifyArray(X, classifier)
    #计算分类的错误率
    e = ErrorSum(G)
    a = 0.5 * math.log((1-e)/e)
    a = round(a, 4)
    print(a)
    W2 = W1*np.exp(-a*Y*np.array(G))
    Zm = np.sum(W2)
    #Zm = round(Zm, 4)
    print(Zm)
    W1 = W2 / Zm
    print(W1)
    
    print('--------------------------------')
    
    W1 = np.array([0.0455, 0.0455, 0.0455, 0.1667, 0.1667, 0.01667, 0.1060, 0.1060, 0.1060, 0.0455])
    classifier = SelectClassifierFunction(X)
    #计算分类的结果值
    G = ClassifyArray(X, classifier)
    #计算分类的错误率
    e = ErrorSum(G)
    a = 0.5 * math.log((1-e)/e)
    a = round(a, 4)
    print(a)
    W2 = W1*np.exp(-a*Y*np.array(G))
    Zm = np.sum(W2)
    #Zm = round(Zm, 4)
    print(Zm)
    W1 = W2 / Zm
    print(W1)
    '''
    
    errorAll = 100
    ExpressArray = []
    while errorAll > 0.1:
        print('--------------------------------')
        classifier = SelectClassifierFunction(X)
        #计算分类的结果值
        G = ClassifyArray(X, classifier)
        #计算分类的错误率
        e = ErrorSum(G)
        a = 0.5 * math.log((1-e)/e)
        a = round(a, 4)
        print('a:' + str(a))
        W2 = W1*np.exp(-a*Y*np.array(G))
        Zm = np.sum(W2)
        #Zm = round(Zm, 4)
        print(Zm)
        print('Zm:' + str(Zm))
        W1 = W2 / Zm
        print('W1:' + str(W1))
        ExpressArray.append([a,classifier])    
        errorAll = ErrorAllSum(ExpressArray)
        print('errorAll:' + str(errorAll))
    
    
    expressString = 'G(x) = sign( '
    i = 0
    for express in ExpressArray:
        if i > 0:
            expressString += ' + '
        expressString += str(express[0]) + ' * ' + express[1].__name__+'(x)'
        i += 1
    
    expressString += ' )'
    print('--------------------------------')
    print('分类函数为:
    ' + expressString)
    print('--------------------------------')
  • 相关阅读:
    javascript typeof 和 instanceof 的区别和联系
    || and && 理解
    jquery选择器总结
    overflow-y:auto 回到顶部
    HTML 获取屏幕,浏览器,页面的高度
    height()、innerHeight()、outerHeight()函数的区别详解
    git入门篇-----本地操作
    sublime快捷键
    Atom 和 VSCode 同一天发布神器:实时编码分享
    编辑器插件和配置备份神器--sync setting
  • 原文地址:https://www.cnblogs.com/gispathfinder/p/5783406.html
Copyright © 2011-2022 走看看