AdaBoost梯度提升算法
項目鏈接:https://github.com/Wchenguang/gglearn/blob/master/AdaBoost/李航機器學習講解/AdaBoost.ipynb
算法步驟與原理
-
訓練 m m m 個弱學習分類器,分類器有相同的接口
G m ( x ) : X → { x 1 , x 2 …   } G_{m}(x) : \mathcal{X} \rightarrow\{x_{1},x_{2} \dots\} G m ? ( x ) : X → { x 1 ? , x 2 ? … } -
假設數據有均勻的權值分布,即每個樣本在分類器中作用相同,$ n $個實例的權重為
D 1 = ( w 11 , ?   , w 1 i , ?   , w 1 N ) , w 1 i = 1 N , i = 1 , 2 , ?   , N D_{1}=\left(w_{11}, \cdots, w_{1 i}, \cdots, w_{1 N}\right), \quad w_{1 i}=\frac{1}{N}, \quad i=1,2, \cdots, N D 1 ? = ( w 1 1 ? , ? , w 1 i ? , ? , w 1 N ? ) , w 1 i ? = N 1 ? , i = 1 , 2 , ? , N
對于 m m m 個分類器而言,有$ m \times n $個權重 -
進入迭代循環,在每一次循環中進行如下操作
3.1 計算 m m m 個分類器在加權數據集上的分類錯誤率
e m = P ( G m ( x i ) ≠ y i ) = ∑ C n ( x i ) ≠ y i w m i e_{m}=P\left(G_{m}\left(x_{i}\right) \neq y_{i}\right)=\sum_{C_{n}\left(x_{i}\right) \neq y_{i}} w_{m i} e m ? = P ( G m ? ( x i ? ) ? ? = y i ? ) = C n ? ( x i ? ) ? ? = y i ? ∑ ? w m i ?
3.2 計算每個分類器的權重 a l p h a m alpha_{m} a l p h a m ? ,該權重表明,每個單獨分類器在最終分類器中的重要程度
α m = 1 2 log ? 1 ? e m e m \alpha_{m}=\frac{1}{2} \log \frac{1-e_{m}}{e_{m}} α m ? = 2 1 ? lo g e m ? 1 ? e m ? ?- 由上式可知,隨著分類器的誤差率的減小,其權重值越大
3.3 更新數據集的權重分布
D m + 1 = ( w m + 1 , 1 , ?   , w m + 1 , i , ?   , w m + 1 , N ) w m + 1 , i = w m i Z m exp ? ( ? α m ( y i = = G m ( x i ) ) ) , i = 1 , 2 , ?   , N \begin{array}{c}{D_{m+1}=\left(w_{m+1,1}, \cdots, w_{m+1, i}, \cdots, w_{m+1, N}\right)} \\ {w_{m+1, i}=\frac{w_{m i}}{Z_{m}} \exp \left(-\alpha_{m} (y_{i}== G_{m}\left(x_{i})\right)\right), \quad i=1,2, \cdots, N}\end{array} D m + 1 ? = ( w m + 1 , 1 ? , ? , w m + 1 , i ? , ? , w m + 1 , N ? ) w m + 1 , i ? = Z m ? w m i ? ? exp ( ? α m ? ( y i ? = = G m ? ( x i ? ) ) ) , i = 1 , 2 , ? , N ?
Z m = ∑ i = 1 N w m i exp ? ( ? α m y i G m ( x i ) ) Z_{m}=\sum_{i=1}^{N} w_{m i} \exp \left(-\alpha_{m} y_{i} G_{m}\left(x_{i}\right)\right) Z m ? = i = 1 ∑ N ? w m i ? exp ( ? α m ? y i ? G m ? ( x i ? ) )-
由上式可知
w m + 1 , i = { w m i Z m e ? α m , G m ( x i ) = y i w m i Z m e α m , G m ( x i ) ≠ y i w_{m+1, i}=\left\{\begin{array}{ll}{\frac{w_{m i}}{Z_{m}} \mathrm{e}^{-\alpha_{m}},} & {G_{m}\left(x_{i}\right)=y_{i}} \\ {\frac{w_{m i}}{Z_{m}} \mathrm{e}^{\alpha_{m}},} & {G_{m}\left(x_{i}\right) \neq y_{i}}\end{array}\right. w m + 1 , i ? = { Z m ? w m i ? ? e ? α m ? , Z m ? w m i ? ? e α m ? , ? G m ? ( x i ? ) = y i ? G m ? ( x i ? ) ? ? = y i ? ?
預測錯誤的實例,權重提升。預測正確的實例,權重下降。
import numpy as np
class testClf:
def __init__(self, thresold):
self.thresold = thresold
self.x = None
self.y = None
def fit(self, x, y):
self.x = x
self.y = y
return self
def predict(self, x):
y = x.copy()
less_index = np.where(y[:, 0] < self.thresold)
greater_index = np.where(y[:, 0] > self.thresold)
y[less_index] = 1
y[greater_index] = -1
return y
def fit_predict(self, x, y):
return self.fit(x, y).predict(x)
'''
test_x = np.arange(10).reshape(-1, 1)
test_y = np.array([1,1,1,-1,-1,-1,1,1,1,-1]).reshape(-1, 1)
tc = testClf(2.5)
print(tc.fit_predict(test_x, test_y))
'''
import numpy as np
import matplotlib.pyplot as plt
class AdaBoost:
def __init__(self, clf_list, iteration_times):
'''
分類器需要有相同的fit,predict接口用于訓練及預測
'''
self.clf_list = clf_list
self.iteration_times = iteration_times
self.x_weight_matrix = None
self.clf_weight = None
def _em(self, y_predict, y, x_weight):
y_predict_flag = (y_predict != y).astype(int)
return np.multiply(y_predict_flag, x_weight).sum()
def _am(self, em):
return np.log((1- em) / em) * 0.5
def _update_x_weight(self, y_predict, y, am, x_weight):
y_predict_flag = (y_predict == y).astype(int)
y_predict_flag[np.where(y_predict_flag[:, 0] == 0)] = -1
zm_array = np.multiply(np.exp(y_predict_flag * am * -1),
x_weight)
zm_array = zm_array / zm_array.sum()
return zm_array
def _fit_once(self, x, y, x_weight, clf_weight):
for index in range(len(self.clf_list)):
clf = self.clf_list[index]
y_predict = clf.fit_predict(x, y)
em = self._em(y_predict, y, x_weight)
am = self._am(em)
x_weight = self._update_x_weight(y_predict, y, am, x_weight)
clf_weight[index] = am
print('em', em, 'am', am)
print('更新后權重')
print(x_weight)
def fit(self, x, y):
m = len(self.clf_list)
n = x.shape[0]
if(0 == n or 0 == m):
return
self.x_weight = np.full((n, 1), 1/n)
self.clf_weight = np.full((m, 1), 1/m)
for i in range(self.iteration_times):
self._fit_once(x, y, self.x_weight, self.clf_weight)
def transform(self, x):
if(self.clf_list == None or 0 == len(self.clf_list)):
return None
res = self.clf_weight[0] * self.clf_list[0].predict(x)
for index in range(1, len(self.clf_list)):
res += (self.clf_weight[index] *
self.clf_list[index].predict(x))
return res
test_x = np.arange(10).reshape(-1, 1)
test_y = np.array([1,1,1,-1,-1,-1,1,1,1,-1]).reshape(-1, 1)
adaboost = AdaBoost([testClf(2.5), testClf(8.5), testClf(5.5), ], 1)
adaboost.fit(test_x, test_y)
predict = adaboost.transform(test_x)
predict[np.where(predict[:, 0] < 0)] = -1
predict[np.where(predict[:, 0] >= 0)] = 1
print('predict')
print(predict)
print('truth')
print(test_y)
- 與書中P140-P41結果相符
- 書中分類器G3計算應為錯誤
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
