資源簡介
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?=?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
- 上一篇:支持向量機實戰代碼全
- 下一篇:DHT11在linux上的驅動程序
評論
共有 條評論