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('分类函数为:\n' + expressString)
print('--------------------------------')
posted @ 2016-08-18 11:47  ParamousGIS  阅读(303)  评论(0编辑  收藏  举报