利用AdaBoost元算法提高分类性能

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/QFire/article/details/78185474

      当做重要决定时,大家可能都会考虑吸取多个专家而不只是一个人的意见,这就是元算法(meta-algorithm)或者叫集成方法(ensemble method)背后的思路.

      接下来我们将集中关注一个称作AdaBoost的最流行的元算法

             优点:泛化错误率低,易编码,可以应用在大部分分类器上,无参数调节

             缺点:对离群点敏感

             适用数据类型:数值型和标称型数据

一,bagging: 基于数据随机重抽样的分类器构建方法

    自举汇聚法(bootstrap aggregating),也称为bagging方法,是在从原始数据集选择S次后得到S个新数据集的一种技术.新数据集和原始数据集的大小相等,允许新数据集中可以有重复的值,而原始数据集的某些值在新集合中则不再出现.

    在S个数据集建好后,将某个学习算法分别作用于每个数据集就得到了S个分类器.选择分类器投票结果最多的类别作为最后的分类结果.

    当然,还有一些更先进的bagging方法,比如随机森林(random forest).

二,boosting

     boosting分类的结果是基于所有分类器的加权求和结果的.    

     bagging中的分类器权重是相等的,而boosting中的分类器权重并不相等

     AdaBoost的一般流程

三,基于单层决策树构建弱分类器

from numpy import *

def loadSimpData():
	datMat = matrix([[1., 2.1],
        [ 2. ,  1.1],
        [ 1.3,  1. ],
        [ 1. ,  1. ],
        [ 2. ,  1. ]])
    classLabels = [1.0, 1.0, -1.0, -1.0, 1.0]
    return datMat,classLabels
单层决策树

def stumpClassify(dataMatrix,dimen,threshVal,threshIneq):#just classify the data
    retArray = ones((shape(dataMatrix)[0],1))
    if threshIneq == 'lt':
        retArray[dataMatrix[:,dimen] <= threshVal] = -1.0
    else:
        retArray[dataMatrix[:,dimen] > threshVal] = -1.0
    return retArray
    

def buildStump(dataArr,classLabels,D):
    dataMatrix = mat(dataArr); labelMat = mat(classLabels).T
    m,n = shape(dataMatrix)
    numSteps = 10.0; bestStump = {}; bestClasEst = mat(zeros((m,1)))
    minError = inf #init error sum, to +infinity
    for i in range(n):#loop over all dimensions
        rangeMin = dataMatrix[:,i].min(); rangeMax = dataMatrix[:,i].max();
        stepSize = (rangeMax-rangeMin)/numSteps
        for j in range(-1,int(numSteps)+1):#loop over all range in current dimension
            for inequal in ['lt', 'gt']: #go over less than and greater than
                threshVal = (rangeMin + float(j) * stepSize)
                predictedVals = stumpClassify(dataMatrix,i,threshVal,inequal)#call stump classify with i, j, lessThan
                errArr = mat(ones((m,1)))
                errArr[predictedVals == labelMat] = 0
                weightedError = D.T*errArr  #calc total error multiplied by D
                #print "split: dim %d, thresh %.2f, thresh ineqal: %s, the weighted error is %.3f" % (i, threshVal, inequal, weightedError)
                if weightedError < minError:
                    minError = weightedError
                    bestClasEst = predictedVals.copy()
                    bestStump['dim'] = i
                    bestStump['thresh'] = threshVal
                    bestStump['ineq'] = inequal
    return bestStump,minError,bestClasEst
>>> import adaboost
>>> datMat,classLabels=adaboost.loadSimpData()
>>> from numpy import *
>>> D = mat(ones((5,1))/5)
>>> adaboost.buildStump(datMat, classLabels, D)
({'dim': 0, 'ineq': 'lt', 'thresh': 1.3}, matrix([[ 0.2]]), array([[-1.],
       [ 1.],
       [-1.],
       [-1.],
       [ 1.]]))

四,完整AdaBoost算法的实现

伪代码:

对每次迭代:

       利用buildStump()函数找到最佳的单层决策树

       将最佳单层决策树加入到单层决策树数组

       计算alpha

       计算新的权重向量D

       更新累计类别估计值

       如果错误率等于0.0, 则退出循环

def adaBoostTrainDS(dataArr, classLabels, numIt=40):
	weakClassArr = []
	m = shape(dataArr)[0]
	D = mat(ones((m,1))/m)
	aggClassEst = mat(zeros((m,1)))
	for i in range(numIt):
		bestStump,error,classEst = buildStump(dataArr,classLabels,D) #build Stump
		print "D:", D.T
		alpha = float(0.5*log((1.0-error)/max(error,1e-16)))  #calc alpha, throw in max(error,eps) to account for error=0
		bestStump['alpha'] = alpha
		weakClassArr.append(bestStump)      #store Stump Params in Array
		print "classEst: ",classEst.T
		expon = multiply(-1*alpha*mat(classLabels).T,classEst)   #exponent for D calc, getting messy
		D = multiply(D,exp(expon))                               #Calc New D for next iteration
		D = D/D.sum()
		#calc training error of all classifiers, if this is 0 quit for loop early (use break)
		aggClassEst += alpha*classEst
		print "aggClassEst: ",aggClassEst.T
		aggErrors = multiply(sign(aggClassEst) != mat(classLabels).T, ones((m,1)))
		errorRate = aggErrors.sum()/m
		print "total error: ",errorRate
		if errorRate == 0.0: break
	return weakClassArr,aggClassEst
>>> reload(adaboost)
<module 'adaboost' from 'adaboost.py'>
>>> classifierArray=adaboost.adaBoostTrainDS(datMat,classLabels,9)
D: [[ 0.2  0.2  0.2  0.2  0.2]]
classEst:  [[-1.  1. -1. -1.  1.]]
aggClassEst:  [[-0.69314718  0.69314718 -0.69314718 -0.69314718  0.69314718]]
total error:  0.2
D: [[ 0.5    0.125  0.125  0.125  0.125]]
classEst:  [[ 1.  1. -1. -1. -1.]]
aggClassEst:  [[ 0.27980789  1.66610226 -1.66610226 -1.66610226 -0.27980789]]
total error:  0.2
D: [[ 0.28571429  0.07142857  0.07142857  0.07142857  0.5       ]]
classEst:  [[ 1.  1.  1.  1.  1.]]
aggClassEst:  [[ 1.17568763  2.56198199 -0.77022252 -0.77022252  0.61607184]]
total error:  0.0
测试算法

>>> reload(adaboost)
<module 'adaboost' from 'adaboost.py'>
>>> datMat,classLabels=adaboost.loadSimpData()
>>> classifierArray=adaboost.adaBoostTrainDS(datMat,classLabels,30)
>>> adaboost.adaClassify([0,0],classifierArray[0])
[[-0.69314718]]
[[-1.66610226]]
[[-2.56198199]]
matrix([[-1.]])

五,示例: 在一个难数据集上应用AdaBoost

  马仙病数据集  

def loadDataSet(fileName):      #general function to parse tab -delimited floats
    numFeat = len(open(fileName).readline().split('\t')) #get number of fields 
    dataMat = []; labelMat = []
    fr = open(fileName)
    for line in fr.readlines():
        lineArr =[]
        curLine = line.strip().split('\t')
        for i in range(numFeat-1):
            lineArr.append(float(curLine[i]))
        dataMat.append(lineArr)
        labelMat.append(float(curLine[-1]))
    return dataMat,labelMat
>>> reload(adaboost)
<module 'adaboost' from 'adaboost.py'>
>>> datArr,labelArr=adaboost.loadDataSet('horseColicTraining2.txt')
>>> classifierArray=adaboost.adaBoostTrainDS(datArr,labelArr,10)
total error:  0.284280936455
total error:  0.284280936455
total error:  0.247491638796
total error:  0.247491638796
total error:  0.254180602007
total error:  0.240802675585
total error:  0.240802675585
total error:  0.220735785953
total error:  0.247491638796
total error:  0.230769230769
>>> testArr,testLabelArr=adaboost.loadDataSet('horseColicTest2.txt')
>>> prediction10 = adaboost.adaClassify(testArr,classifierArray[0])
>>> errArr=mat(ones((67,1)))
>>> errArr[prediction10!=mat(testLabelArr).T].sum()
16.0


  很多人都认为,AdaBoost和SVM是监督机器学习中最强大的两种方法.实际上,这两者之间拥有不少相似之处.我们可以把弱分类器想象成SVM中的一个核函数,也可以按照最大化某个最小间隔的方式重写AdaBoost算法.而它们的不同就在于其所定义的间隔计算方式有所不同,因此导致的结果也不同.特别是在高维空间下,这两者之间的差异就会更加明显.


六,非均衡问题

     性能度量指标:正确率, 召回率及ROC曲线

def plotROC(predStrengths, classLabels):
    import matplotlib.pyplot as plt
    cur = (1.0,1.0) #cursor
    ySum = 0.0 #variable to calculate AUC
    numPosClas = sum(array(classLabels)==1.0)
    yStep = 1/float(numPosClas); xStep = 1/float(len(classLabels)-numPosClas)
    sortedIndicies = predStrengths.argsort()#get sorted index, it's reverse
    fig = plt.figure()
    fig.clf()
    ax = plt.subplot(111)
    #loop through all the values, drawing a line segment at each point
    for index in sortedIndicies.tolist()[0]:
        if classLabels[index] == 1.0:
            delX = 0; delY = yStep;
        else:
            delX = xStep; delY = 0;
            ySum += cur[1]
        #draw line from cur to (cur[0]-delX,cur[1]-delY)
        ax.plot([cur[0],cur[0]-delX],[cur[1],cur[1]-delY], c='b')
        cur = (cur[0]-delX,cur[1]-delY)
    ax.plot([0,1],[0,1],'b--')
    plt.xlabel('False positive rate'); plt.ylabel('True positive rate')
    plt.title('ROC curve for AdaBoost horse colic detection system')
    ax.axis([0,1,0,1])
    plt.show()
    print "the Area Under the Curve is: ",ySum*xStep
>> reload(adaboost)
>>> classifierArray,aggClassEst=adaboost.adaBoostTrainDS(datArr,labelArr,10)
>>> adaboost.plotROC(aggClassEst.T,labelArr)
the Area Under the Curve is:  0.858296963506




    

展开阅读全文

没有更多推荐了,返回首页