日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

8.李航機(jī)器學(xué)習(xí)-AdaBoost梯度提升算法python實(shí)現(xiàn)

系統(tǒng) 1680 0

AdaBoost梯度提升算法

項(xiàng)目鏈接:https://github.com/Wchenguang/gglearn/blob/master/AdaBoost/李航機(jī)器學(xué)習(xí)講解/AdaBoost.ipynb

算法步驟與原理

  1. 訓(xùn)練 m m m 個(gè)弱學(xué)習(xí)分類器,分類器有相同的接口
    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 ? }

  2. 假設(shè)數(shù)據(jù)有均勻的權(quán)值分布,即每個(gè)樣本在分類器中作用相同,$ n $個(gè)實(shí)例的權(quá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
    對(duì)于 m m m 個(gè)分類器而言,有$ m \times n $個(gè)權(quán)重

  3. 進(jìn)入迭代循環(huán),在每一次循環(huán)中進(jìn)行如下操作

    3.1 計(jì)算 m m m 個(gè)分類器在加權(quán)數(shù)據(jù)集上的分類錯(cuò)誤率
    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 計(jì)算每個(gè)分類器的權(quán)重 a l p h a m alpha_{m} a l p h a m ? ,該權(quán)重表明,每個(gè)單獨(dú)分類器在最終分類器中的重要程度
    α 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 ? ?

    • 由上式可知,隨著分類器的誤差率的減小,其權(quán)重值越大

    3.3 更新數(shù)據(jù)集的權(quán)重分布

    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 ? ?
      預(yù)測(cè)錯(cuò)誤的實(shí)例,權(quán)重提升。預(yù)測(cè)正確的實(shí)例,權(quán)重下降。
            
              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接口用于訓(xùn)練及預(yù)測(cè)
        '''
        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('更新后權(quán)重')
            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結(jié)果相符
  • 書中分類器G3計(jì)算應(yīng)為錯(cuò)誤

更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 尼木县| 塘沽区| 赣榆县| 玛多县| 射洪县| 南昌县| 涟水县| 德钦县| 蕲春县| 浮山县| 武强县| 鲁甸县| 三门县| 南郑县| 明光市| 罗城| 通山县| 石嘴山市| 樟树市| 牟定县| 北京市| 饶平县| 香河县| 临朐县| 南昌市| 宜章县| 垫江县| 延安市| 赫章县| 洛宁县| 泗水县| 莱阳市| 集贤县| 河东区| 清涧县| 福鼎市| 彩票| 新郑市| 张家港市| 乌拉特中旗| 蛟河市|