摘要:本篇內容為機器學習實戰第章利用元算法提高分類性能程序清單。將當前錯誤率與已有的最小錯誤率進行對比后,如果當前的值較小,那么就在字典中保存該單層決策樹。上述,我們已經構建了單層決策樹,得到了弱學習器。
本篇內容為《機器學習實戰》第 7 章利用 AdaBoost 元算法提高分類性能程序清單。所用代碼為 python3。
AdaBoost
優點:泛化錯誤率低,易編碼,可以應用在大部分分類器上,無參數調整。
缺點:對離群點敏感。
適用數據類型:數值型和標稱型數據。
boosting 方法擁有多個版本,這里將只關注其中一個最流行的版本 AdaBoost。
在構造 AdaBoost 的代碼時,我們將首先通過一個簡單數據集來確保在算法實現上一切就緒。使用如下的數據集:
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
在 python 提示符下,執行代碼加載數據集:
>>> import adaboost >>> datMat, classLabels=adaboost.loadSimpData()
我們先給出函數buildStump()的偽代碼:
""" Created on Sep 20, 2018 @author: yufei Adaboost is short for Adaptive Boosting """ """ 測試是否有某個值小于或大于我們正在測試的閾值 """ 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 """ 在一個加權數據集中循環 buildStump()將會遍歷stumpClassify()函數所有的可能輸入值 并找到具有最低錯誤率的單層決策樹 """ def buildStump(dataArr,classLabels,D): dataMatrix = mat(dataArr); labelMat = mat(classLabels).T m,n = shape(dataMatrix) # 變量 numSteps 用于在特征的所有可能值上進行遍歷 numSteps = 10.0 # 創建一個空字典,用于存儲給定權重向量 D 時所得到的最佳單層決策樹的相關信息 bestStump = {}; bestClasEst = mat(zeros((m,1))) # 初始化為正無窮大,之后用于尋找可能的最小錯誤率 minError = inf # 第一層循環在數據集的所有特征上遍歷 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) # 調用 stumpClassify() 函數,返回分類預測結果 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
為了解實際運行過程,在 python 提示符下,執行代碼并得到結果:
>>> D=mat(ones((5,1))/5) >>> adaboost.buildStump(datMat, classLabels, D) split: dim 0, thresh 0.90, thresh ineqal: lt, the weighted error is 0.400 split: dim 0, thresh 0.90, thresh ineqal: gt, the weighted error is 0.600 split: dim 0, thresh 1.00, thresh ineqal: lt, the weighted error is 0.400 split: dim 0, thresh 1.00, thresh ineqal: gt, the weighted error is 0.600 split: dim 0, thresh 1.10, thresh ineqal: lt, the weighted error is 0.400 split: dim 0, thresh 1.10, thresh ineqal: gt, the weighted error is 0.600 split: dim 0, thresh 1.20, thresh ineqal: lt, the weighted error is 0.400 split: dim 0, thresh 1.20, thresh ineqal: gt, the weighted error is 0.600 split: dim 0, thresh 1.30, thresh ineqal: lt, the weighted error is 0.200 split: dim 0, thresh 1.30, thresh ineqal: gt, the weighted error is 0.800 split: dim 0, thresh 1.40, thresh ineqal: lt, the weighted error is 0.200 split: dim 0, thresh 1.40, thresh ineqal: gt, the weighted error is 0.800 split: dim 0, thresh 1.50, thresh ineqal: lt, the weighted error is 0.200 split: dim 0, thresh 1.50, thresh ineqal: gt, the weighted error is 0.800 split: dim 0, thresh 1.60, thresh ineqal: lt, the weighted error is 0.200 split: dim 0, thresh 1.60, thresh ineqal: gt, the weighted error is 0.800 split: dim 0, thresh 1.70, thresh ineqal: lt, the weighted error is 0.200 split: dim 0, thresh 1.70, thresh ineqal: gt, the weighted error is 0.800 split: dim 0, thresh 1.80, thresh ineqal: lt, the weighted error is 0.200 split: dim 0, thresh 1.80, thresh ineqal: gt, the weighted error is 0.800 split: dim 0, thresh 1.90, thresh ineqal: lt, the weighted error is 0.200 split: dim 0, thresh 1.90, thresh ineqal: gt, the weighted error is 0.800 split: dim 0, thresh 2.00, thresh ineqal: lt, the weighted error is 0.600 split: dim 0, thresh 2.00, thresh ineqal: gt, the weighted error is 0.400 split: dim 1, thresh 0.89, thresh ineqal: lt, the weighted error is 0.400 split: dim 1, thresh 0.89, thresh ineqal: gt, the weighted error is 0.600 split: dim 1, thresh 1.00, thresh ineqal: lt, the weighted error is 0.200 split: dim 1, thresh 1.00, thresh ineqal: gt, the weighted error is 0.800 split: dim 1, thresh 1.11, thresh ineqal: lt, the weighted error is 0.400 split: dim 1, thresh 1.11, thresh ineqal: gt, the weighted error is 0.600 split: dim 1, thresh 1.22, thresh ineqal: lt, the weighted error is 0.400 split: dim 1, thresh 1.22, thresh ineqal: gt, the weighted error is 0.600 split: dim 1, thresh 1.33, thresh ineqal: lt, the weighted error is 0.400 split: dim 1, thresh 1.33, thresh ineqal: gt, the weighted error is 0.600 split: dim 1, thresh 1.44, thresh ineqal: lt, the weighted error is 0.400 split: dim 1, thresh 1.44, thresh ineqal: gt, the weighted error is 0.600 split: dim 1, thresh 1.55, thresh ineqal: lt, the weighted error is 0.400 split: dim 1, thresh 1.55, thresh ineqal: gt, the weighted error is 0.600 split: dim 1, thresh 1.66, thresh ineqal: lt, the weighted error is 0.400 split: dim 1, thresh 1.66, thresh ineqal: gt, the weighted error is 0.600 split: dim 1, thresh 1.77, thresh ineqal: lt, the weighted error is 0.400 split: dim 1, thresh 1.77, thresh ineqal: gt, the weighted error is 0.600 split: dim 1, thresh 1.88, thresh ineqal: lt, the weighted error is 0.400 split: dim 1, thresh 1.88, thresh ineqal: gt, the weighted error is 0.600 split: dim 1, thresh 1.99, thresh ineqal: lt, the weighted error is 0.400 split: dim 1, thresh 1.99, thresh ineqal: gt, the weighted error is 0.600 split: dim 1, thresh 2.10, thresh ineqal: lt, the weighted error is 0.600 split: dim 1, thresh 2.10, thresh ineqal: gt, the weighted error is 0.400 ({"dim": 0, "thresh": 1.3, "ineq": "lt"}, matrix([[0.2]]), array([[-1.], [ 1.], [-1.], [-1.], [ 1.]]))
這一行可以注釋掉,這里為了理解函數的運行而打印出來。
將當前錯誤率與已有的最小錯誤率進行對比后,如果當前的值較小,那么就在字典baseStump中保存該單層決策樹。字典、錯誤率和類別估計值都會返回給 AdaBoost 算法。
上述,我們已經構建了單層決策樹,得到了弱學習器。接下來,我們將使用多個弱分類器來構建 AdaBoost 代碼。
首先給出整個實現的偽代碼如下:
程序清單 7-2 基于單層決策樹的 AdaBoost 訓練過程""" 輸入參數:數據集、類別標簽、迭代次數(需要用戶指定) """ def adaBoostTrainDS(dataArr,classLabels,numIt=40): weakClassArr = [] m = shape(dataArr)[0] # 向量 D 包含了每個數據點的權重,初始化為 1/m D = mat(ones((m,1))/m) #init D to all equal # 記錄每個數據點的類別估計累計值 aggClassEst = mat(zeros((m,1))) for i in range(numIt): # 調用 buildStump() 函數建立一個單層決策樹 bestStump,error,classEst = buildStump(dataArr,classLabels,D)#build Stump print ("D:",D.T) # 計算 alpha,本次單層決策樹輸出結果的權重 # 確保沒有錯誤時不會發生除零溢出 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) # 為下一次迭代計算 D 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) # 為了得到二值分類結果調用 sign() 函數 aggErrors = multiply(sign(aggClassEst) != mat(classLabels).T,ones((m,1))) errorRate = aggErrors.sum()/m print ("total error: ",errorRate) # 若總錯誤率為 0,則中止 for 循環 if errorRate == 0.0: break return weakClassArr,aggClassEst
在 python 提示符下,執行代碼并得到結果:
>>> 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
最后,我們來觀察測試錯誤率。
""" 將弱分類器的訓練過程從程序中抽查來,應用到某個具體的實例上去。 datToClass: 一個或多個待分類樣例 classifierArr: 多個弱分類器組成的數組 返回 aggClassEst 符號,大于 0 返回1;小于 0 返回 -1 """ def adaClassify(datToClass,classifierArr): dataMatrix = mat(datToClass)#do stuff similar to last aggClassEst in adaBoostTrainDS m = shape(dataMatrix)[0] aggClassEst = mat(zeros((m,1))) for i in range(len(classifierArr)): classEst = stumpClassify(dataMatrix, classifierArr[0][i]["dim"], classifierArr[0][i]["thresh"], classifierArr[0][i]["ineq"]) aggClassEst += classifierArr[0][i]["alpha"]*classEst print (aggClassEst) return sign(aggClassEst)
在 python 提示符下,執行代碼并得到結果:
>>> datArr, labelArr = adaboost.loadSimpData() >>> classifierArr = adaboost.adaBoostTrainDS(datArr, labelArr, 30) 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
輸入以下命令進行分類:
>>> adaboost.adaClassify([0,0], classifierArr) [[-0.69314718]] [[-1.66610226]] matrix([[-1.]])
隨著迭代的進行,數據點 [0,0] 的分類結果越來越強。也可以在其它點上分類:
>>> adaboost.adaClassify([[5,5],[0,0]], classifierArr) [[ 0.69314718] [-0.69314718]] [[ 1.66610226] [-1.66610226]] matrix([[ 1.], [-1.]])
這兩個點的分類結果也會隨著迭代的進行而越來越強。
參考鏈接:
GBDT,ADABOOSTING概念區分 GBDT與XGBOOST區別
【機器學習實戰-python3】Adaboost元算法提高分類性能
$$$$
不足之處,歡迎指正。
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/42479.html
摘要:本篇內容為機器學習實戰第章支持向量機部分程序清單。支持向量機優點泛化錯誤率低,計算開銷不大,結果易解釋。注以上給出的僅是簡化版算法的實現,關于完整的算法加速優化并應用核函數,請參照機器學習實戰第頁。 本篇內容為《機器學習實戰》第 6 章 支持向量機部分程序清單。所用代碼為 python3。 支持向量機優點:泛化錯誤率低,計算開銷不大,結果易解釋。 缺點:對參數調節和核函數的選擇敏感,...
摘要:根據錯誤率決定是否回退到訓練階段,通過改變迭代的次數和步長等參數來得到更好的回歸系數。使用回歸方法進行分類所需做的是把測試集上每個特征向量乘以最優化方法得來的回歸系數,再將該乘積結果求和,最后輸入到函數即可。 本篇內容為《機器學習實戰》第 5 章 Logistic 回歸程序清單。 書中所用代碼為 python2,下面給出的程序清單是在 python3 中實踐改過的代碼,希望對你有幫助。...
摘要:本篇內容為機器學習實戰第章決策樹部分程序清單。適用數據類型數值型和標稱型在構造決策樹時,我們需要解決的第一個問題就是,當前數據集上哪個特征在劃分數據分類時起決定性作用。下面我們會介紹如何將上述實現的函數功能放在一起,構建決策樹。 本篇內容為《機器學習實戰》第 3 章決策樹部分程序清單。所用代碼為 python3。 決策樹優點:計算復雜度不高,輸出結果易于理解,對中間值的缺失不敏感,可...
閱讀 2882·2023-04-26 02:49
閱讀 3455·2021-11-25 09:43
閱讀 3430·2021-10-09 09:43
閱讀 3008·2021-09-28 09:44
閱讀 2457·2021-09-22 15:29
閱讀 4529·2021-09-14 18:02
閱讀 2789·2021-09-03 10:48
閱讀 3434·2019-08-30 12:47