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

(八)Python 中的 AdaBoost 分類器實(shí)例

系統(tǒng) 3054 0

作者:chen_h
微信號(hào) & QQ:862251340
微信公眾號(hào):coderpai


(一)機(jī)器學(xué)習(xí)中的集成學(xué)習(xí)入門

(二)bagging 方法

(三)使用Python進(jìn)行交易的隨機(jī)森林算法

(四)Python中隨機(jī)森林的實(shí)現(xiàn)與解釋

(五)如何用 Python 從頭開始實(shí)現(xiàn) Bagging 算法

(六)如何利用Python從頭開始實(shí)現(xiàn)隨機(jī)森林算法

(七)AdaBoost 簡介

(八)Python 中的 AdaBoost 分類器實(shí)例


boosting 算法背后的思路是按照順序訓(xùn)練預(yù)測模型,每個(gè)模型都試圖去糾正前面的錯(cuò)誤。兩種最常見的模型就是AdaBoost 和 Gradient Boosting。在上一篇文章中,我們已經(jīng)介紹了 AdaBoost 。從較高的層面上看,AdaBoost 類似于隨機(jī)森林,因?yàn)樗鼈兌冀y(tǒng)計(jì)了森林中每個(gè)決策樹所做的預(yù)測,從而來決定最終的分類。但是,它們之間肯定有一些微妙的差異。例如,在 AdaBoost 中,決策樹的深度為 1。此外,每個(gè)決策樹做出的預(yù)測對(duì)模型的最終預(yù)測產(chǎn)生不同的影響。

算法

在前面的實(shí)例中,我們將使用一個(gè)數(shù)據(jù)集,給定一些人的特征,來判斷這個(gè)人是不是受歡迎。

weight smart polite fit attractive
180 no no no no
150 yes Yes no no
175 yes Yes yes yes
165 Yes yes yes yes
190 no yes No no
201 yes yes yes yes
185 yes yes no yes
168 Yes No Yes yes

Step 1:初始化樣本權(quán)重

在 AdaBoost 的第一步中,每個(gè)樣本都與一個(gè)權(quán)重相關(guān)聯(lián),該權(quán)重表明它在分類方面的重要性。最初,所有樣本具有想聽的權(quán)重。(1除以樣本總數(shù), 1 / N 1/N 1 / N

weight smart polite fit attractive Sample weight
180 no no no no 1/8
150 yes Yes no no 1/8
175 yes Yes yes yes 1/8
165 Yes yes yes yes 1/8
190 no yes No no 1/8
201 yes yes yes yes 1/8
185 yes yes no yes 1/8
168 Yes No Yes yes 1/8

Step 2:使用每個(gè)特征去構(gòu)建決策樹,對(duì)數(shù)據(jù)進(jìn)行分類并評(píng)估結(jié)果

接下來,對(duì)于每個(gè)特征,我們構(gòu)建一個(gè)深度為 1 的決策樹。然后,我們使用每個(gè)決策樹對(duì)數(shù)據(jù)進(jìn)行分類。然后,我們將每棵樹的預(yù)測與訓(xùn)練集中的實(shí)際標(biāo)簽進(jìn)行比較。我們對(duì)訓(xùn)練樣本中得到最好的結(jié)果的特征和樹模型使其成為森林中的下一課樹。

例如,假設(shè)我們建造了一棵樹,如果人 smart = yes,那么我們就認(rèn)為這個(gè)人具有吸引力。如果人 smart = no,那么我們就認(rèn)為這個(gè)人沒有吸引力。

(八)Python 中的 AdaBoost 分類器實(shí)例_第1張圖片

根據(jù)他們是否 smart 進(jìn)行分類,決策樹錯(cuò)誤的將一個(gè)人計(jì)算錯(cuò)誤了。將一個(gè) smart = yes 的人歸納為吸引,但真實(shí)情況是不吸引。

Step 3:計(jì)算最終分類中樹的重要性

一旦我們決定了決策樹。我們使用前面的公式來計(jì)算它在最終分類中的權(quán)重

s i g n i f i c a n c e = 1 2 l o g ( 1 ? t o t a l e r r o r t o t a l e r r o r ) significance = \frac{1}{2}log(\frac{1-totalerror}{totalerror}) s i g n i f i c a n c e = 2 1 ? l o g ( t o t a l e r r o r 1 ? t o t a l e r r o r ? )

totalerror 是錯(cuò)誤分類的樣本的權(quán)重之和。回到我們的例子,total error 應(yīng)該等于如下:

t o t a l e r r o r = s u m o f w e i g h t s f o r i n c o r r e c t l y c l a s s i f i e d s a m p l e s = 1 8 ? 1 = 1 8 total error = sum of weights for incorrectly classified samples= \frac{1}{8} * 1 = \frac{1}{8} t o t a l e r r o r = s u m o f w e i g h t s f o r i n c o r r e c t l y c l a s s i f i e d s a m p l e s = 8 1 ? ? 1 = 8 1 ?

我們將 totalerror 參數(shù)插入到我們的公式中,我們得到:

s i g n i f i c a n c e = 1 2 l o g ( 1 ? 1 8 1 8 ) = 0.97 significance = \frac{1}{2} log(\frac{1-\frac{1}{8}}{\frac{1}{8}}) = 0.97 s i g n i f i c a n c e = 2 1 ? l o g ( 8 1 ? 1 ? 8 1 ? ? ) = 0 . 9 7

正如我們稍后將看到的,這個(gè)數(shù)字用于后續(xù)的權(quán)重更新。

Step 4:更新樣本權(quán)重,以便下一個(gè)決策樹將上一個(gè)決策樹所產(chǎn)生的錯(cuò)誤考慮在內(nèi)

我們查看當(dāng)前樹分類不正確的樣本,并使用以下公式增加其相關(guān)權(quán)重。

n e w s a m p l e w e i g h t = s a m p l e w e i g h t ? e x p ( s i g n i f i c a n c e ) newsampleweight=sampleweight * exp(significance) n e w s a m p l e w e i g h t = s a m p l e w e i g h t ? e x p ( s i g n i f i c a n c e )

這里沒有什么花哨的東西,我們使用 e 來提高權(quán)重,是因?yàn)槲覀兿M碌臉颖緳?quán)重呈現(xiàn)指數(shù)增長。

n e w s a m p l e w e i g h t = 1 8 ? e x p ( 0.97 ) = 1 8 ? 2.64 = 0.33 newsampleweight=\frac{1}{8} * exp(0.97) = \frac{1}{8} * 2.64 = 0.33 n e w s a m p l e w e i g h t = 8 1 ? ? e x p ( 0 . 9 7 ) = 8 1 ? ? 2 . 6 4 = 0 . 3 3

然后,我們使用以下公式查看樹正確分類的樣本,并且降低其相關(guān)權(quán)重。

n e w s a m p l e w e i g h t = s a m p l e w e i g h t ? e x p ( ? s i g n i f i c a n c e ) newsampleweight=sampleweight * exp(- significance) n e w s a m p l e w e i g h t = s a m p l e w e i g h t ? e x p ( ? s i g n i f i c a n c e )

該方程跟此前的方程式相同,我們只是將 e 提高到負(fù)指數(shù)。

n e w s a m p l e w e i g h t = 1 8 ? e x p ( ? 0.97 ) = 1 8 ? 0.38 = 0.05 newsampleweight=\frac{1}{8} * exp(-0.97) = \frac{1}{8} * 0.38 = 0.05 n e w s a m p l e w e i g h t = 8 1 ? ? e x p ( ? 0 . 9 7 ) = 8 1 ? ? 0 . 3 8 = 0 . 0 5

這里的主要內(nèi)容是,前一個(gè)樹樁錯(cuò)誤分類的樣本應(yīng)該與較大的樣本權(quán)重相關(guān)聯(lián),并且正確分類的樣本應(yīng)該與較小的樣本權(quán)重相關(guān)聯(lián)。

注意,如果我們將所有樣本權(quán)重加起來,我們得到的是一個(gè)小于 1 的數(shù)字。因此,我們將新樣本權(quán)重標(biāo)準(zhǔn)化,使得他們加起來為 1 。

weight smart polite fit attractive Sample weight new weight normalized weight
180 no no no no 1/8 0.05 0.07
150 yes Yes no no 1/8 0.33 0.49
175 yes Yes yes yes 1/8 0.05 0.07
165 Yes yes yes yes 1/8 0.05 0.07
190 no yes No no 1/8 0.05 0.07
201 yes yes yes yes 1/8 0.05 0.07
185 yes yes no yes 1/8 0.05 0.07
168 Yes No Yes yes 1/8 0.05 0.07

Step 5:形成一個(gè)新的數(shù)據(jù)集

我們首先創(chuàng)建的一個(gè)與原始數(shù)據(jù)集大小相同的新數(shù)據(jù)集。然后,想象一下輪盤賭桌,其中每個(gè)口袋對(duì)應(yīng)一個(gè)樣本權(quán)重。我們隨機(jī)選擇 0 到 1 之間的數(shù)字。每個(gè)數(shù)字落在的位置決定了我們?cè)谛碌臄?shù)據(jù)集中放置的樣本。

(八)Python 中的 AdaBoost 分類器實(shí)例_第2張圖片

由于錯(cuò)誤分類的樣本相對(duì)于其他樣本具有更高的權(quán)重,因此隨機(jī)數(shù)落在其分布的切片下的可能性更大。因此,新數(shù)據(jù)集將傾向于包含由前一樹錯(cuò)誤分類的樣本的多個(gè)副本。因此,當(dāng)我們回到評(píng)估每個(gè)決策樹所做預(yù)測的步驟時(shí),得分最高的那個(gè)將正確的分類前一個(gè)樹被錯(cuò)誤分類的樣本。

Step 6:重復(fù)步驟 2 到 5,直到迭代次數(shù)等于超參數(shù)指定的數(shù)量(即估計(jì)量的數(shù)量)

Step 7:使用決策樹林來預(yù)測訓(xùn)練集之外的數(shù)據(jù)

AdaBoost 模型通過讓森林中的每棵樹對(duì)樣本進(jìn)行分類來進(jìn)行預(yù)測。然后,我們根據(jù)它們的決定將樹分成幾組。對(duì)于每個(gè)組,我們將組內(nèi)每棵樹的重要性加起來。森林作為一個(gè)整體進(jìn)行的最終分類由綜合最大的群體決定。

(八)Python 中的 AdaBoost 分類器實(shí)例_第3張圖片

代碼

讓我們看看如何在 Python 中實(shí)現(xiàn) AdaBoost 。首先,我們導(dǎo)入以下庫。

            
              
                from
              
               sklearn
              
                .
              
              ensemble 
              
                import
              
               AdaBoostClassifier

              
                from
              
               sklearn
              
                .
              
              tree 
              
                import
              
               DecisionTreeClassifier

              
                from
              
               sklearn
              
                .
              
              datasets 
              
                import
              
               load_breast_cancer

              
                import
              
               pandas 
              
                as
              
               pd

              
                import
              
               numpy 
              
                as
              
               np

              
                from
              
               sklearn
              
                .
              
              model_selection 
              
                import
              
               train_test_split

              
                from
              
               sklearn
              
                .
              
              metrics 
              
                import
              
               confusion_matrix

              
                from
              
               sklearn
              
                .
              
              preprocessing 
              
                import
              
               LabelEncoder

            
          

在這個(gè)例子中,我們將使用 AdaBoost 將腫瘤歸類為惡性或者良性腫瘤。我們使用 scikit-learn API 將數(shù)據(jù)集導(dǎo)入到我們的程序中。

            
              breast_cancer 
              
                =
              
               load_breast_cancer
              
                (
              
              
                )
              
              
X 
              
                =
              
               pd
              
                .
              
              DataFrame
              
                (
              
              breast_cancer
              
                .
              
              data
              
                ,
              
               columns
              
                =
              
              breast_cancer
              
                .
              
              feature_names
              
                )
              
              
y 
              
                =
              
               pd
              
                .
              
              Categorical
              
                .
              
              from_codes
              
                (
              
              breast_cancer
              
                .
              
              target
              
                ,
              
               breast_cancer
              
                .
              
              target_names
              
                )
              
            
          

每當(dāng)我們使用分類功能時(shí),我們必須將其編碼為數(shù)字。對(duì)于這個(gè)問題,我們將惡性設(shè)置為 1 ,并將良性設(shè)置為 0 。

            
              encoder 
              
                =
              
               LabelEncoder
              
                (
              
              
                )
              
              
binary_encoded_y 
              
                =
              
               pd
              
                .
              
              Series
              
                (
              
              encoder
              
                .
              
              fit_transform
              
                (
              
              y
              
                )
              
              
                )
              
            
          

我們將數(shù)據(jù)分為訓(xùn)練集和測試集,來評(píng)估我們的模型。

            
              train_X
              
                ,
              
               test_X
              
                ,
              
               train_y
              
                ,
              
               test_y 
              
                =
              
               train_test_split
              
                (
              
              X
              
                ,
              
               binary_encoded_y
              
                ,
              
               random_state
              
                =
              
              
                1
              
              
                )
              
            
          

接下來,我們構(gòu)建并將我們的模型擬合到訓(xùn)練集。max_depth = 1 用于告訴我們的模型我們希望我們的森林中單顆樹的只有一個(gè)節(jié)點(diǎn)。n_estimators 用于制定樹林中樹的總數(shù)。

            
              classifier 
              
                =
              
               AdaBoostClassifier
              
                (
              
              
    DecisionTreeClassifier
              
                (
              
              max_depth
              
                =
              
              
                1
              
              
                )
              
              
                ,
              
              
    n_estimators
              
                =
              
              
                200
              
              
                )
              
              
classifier
              
                .
              
              fit
              
                (
              
              train_X
              
                ,
              
               train_y
              
                )
              
            
          

我們使用我們的模型來預(yù)測腫瘤是否惡性的或者良性的。

            
              predictions 
              
                =
              
               classifier
              
                .
              
              predict
              
                (
              
              test_X
              
                )
              
            
          

最后,我們使用混淆矩陣評(píng)估模型。該模型產(chǎn)生了 2 個(gè)誤報(bào)和 3 個(gè)假陰性。

            
              confusion_matrix
              
                (
              
              test_y
              
                ,
              
               predictions
              
                )
              
            
          

輸出結(jié)果為:

[[86, 2], [3, 52]]

最后

與隨機(jī)森林一樣,AdaBoost 通過對(duì)每個(gè)樣本應(yīng)用多個(gè)決策樹并組合各個(gè)樹的預(yù)測來進(jìn)行預(yù)測。然而,在 AdaBoost 算法中,不是采用森林中每個(gè)決策樹所作出的預(yù)測的平均值,而是每個(gè)決策樹對(duì)最終預(yù)測貢獻(xiàn)的不同的量


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

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

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

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

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 苗栗市| 广西| 磴口县| 邓州市| 始兴县| 大名县| 临潭县| 雅江县| 宁陵县| 准格尔旗| 玛沁县| 庆城县| 石泉县| 德化县| 商南县| 庆安县| 筠连县| 时尚| 电白县| 乌恰县| 青岛市| 江北区| 新昌县| 义马市| 唐河县| 石城县| 武功县| 吉木乃县| 上犹县| 沧州市| 响水县| 吐鲁番市| 酒泉市| 鄯善县| 吉木萨尔县| 呼和浩特市| 临猗县| 海丰县| 沙洋县| 二连浩特市| 工布江达县|