资源简介

Adaboost实战代码,利用决策树桩作为基弱分类器,并利用集成学习器进行病马死亡率的预测,得到了良好的预测效果。

资源截图

代码片段和文件信息

# -*- coding: utf-8 -*-
“““
Created on Fri Nov  9 15:01:36 2018

@author: zhe

E-mail: 1194585271@qq.com
“““

import numpy as np

def loadSimpData():
    datMat = np.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 datMatclassLabels

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

def buildStump(dataArrclassLabelsD):
    dataMatrix = np.mat(dataArr); labelMat = np.mat(classLabels).T
    mn = np.shape(dataMatrix)
    numSteps = 10.0; bestStump = {}; bestClasEst = np.mat(np.zeros((m1)))
    minError = np.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(-1int(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(dataMatrixithreshValinequal)#call stump classify with i j lessThan
                errArr = np.mat(np.ones((m1)))
                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 bestStumpminErrorbestClasEst


if __name__==‘__main__‘:
    datMatclassLabels = loadSimpData()
    D = np.mat(np.ones((51))/5)
    print (buildStump(datMatclassLabelsD))

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-11-09 15:05  Chapter7_Adaboost\1.realizeWeakclassifer\
     文件        2285  2018-11-09 15:24  Chapter7_Adaboost\1.realizeWeakclassifer\Adaboost.py
     目录           0  2018-11-10 15:52  Chapter7_Adaboost\2.realizeAdaboost\
     文件        5937  2018-11-10 16:44  Chapter7_Adaboost\2.realizeAdaboost\Adaboost.py
     目录           0  2018-11-10 16:27  Chapter7_Adaboost\3.inAction\
     文件        6166  2018-11-12 16:26  Chapter7_Adaboost\3.inAction\Adaboost.py
     文件       13614  2010-12-06 08:29  Chapter7_Adaboost\3.inAction\horseColicTest2.txt
     文件       60778  2010-12-06 08:26  Chapter7_Adaboost\3.inAction\horseColicTraining2.txt

评论

共有 条评论