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

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

系統(tǒng) 1955 0

作者:chen_h
微信號 & QQ:862251340
微信公眾號:coderpai


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

(二)bagging 方法

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

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

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

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

(七)AdaBoost 簡介

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


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

算法

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

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 的第一步中,每個樣本都與一個權(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òu)建決策樹,對數(shù)據(jù)進行分類并評估結(jié)果

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

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

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

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

Step 3:計算最終分類中樹的重要性

一旦我們決定了決策樹。我們使用前面的公式來計算它在最終分類中的權(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 是錯誤分類的樣本的權(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

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

Step 4:更新樣本權(quán)重,以便下一個決策樹將上一個決策樹所產(chǎn)生的錯誤考慮在內(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)重,是因為我們希望新的樣本權(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 提高到負指數(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)容是,前一個樹樁錯誤分類的樣本應(yīng)該與較大的樣本權(quán)重相關(guān)聯(lián),并且正確分類的樣本應(yīng)該與較小的樣本權(quán)重相關(guān)聯(lián)。

注意,如果我們將所有樣本權(quán)重加起來,我們得到的是一個小于 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:形成一個新的數(shù)據(jù)集

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

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

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

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

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

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

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

代碼

讓我們看看如何在 Python 中實現(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

            
          

在這個例子中,我們將使用 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è)置為 1 ,并將良性設(shè)置為 0 。

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

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

            
              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 用于告訴我們的模型我們希望我們的森林中單顆樹的只有一個節(jié)點。n_estimators 用于制定樹林中樹的總數(shù)。

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

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

            
              predictions 
              
                =
              
               classifier
              
                .
              
              predict
              
                (
              
              test_X
              
                )
              
            
          

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

            
              confusion_matrix
              
                (
              
              test_y
              
                ,
              
               predictions
              
                )
              
            
          

輸出結(jié)果為:

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

最后

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


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 石楼县| 瑞金市| 治县。| 宣武区| 河北区| 洛川县| 沅江市| 翁源县| 乌兰县| 图们市| 黑龙江省| 托克托县| 姚安县| 资阳市| 岢岚县| 科尔| 麻江县| 长治县| 抚顺县| 乐亭县| 毕节市| 宜兰市| 玉门市| 黎平县| 西乡县| 中宁县| 渝北区| 阿克| 墨竹工卡县| 广州市| 石楼县| 仪陇县| 黄大仙区| 丹东市| 犍为县| 乌兰察布市| 灵武市| 三都| 读书| 新丰县| 舒兰市|