成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

數據科學:AdaBoost分類器

大數據
在本文中,我們討論了理解AdaBoost算法的各種方法.AdaBoost就像是一個恩賜,如果正確使用它可以提高分類算法的準確性。

[[328383]]

介紹:

  • Boosting是一種集成技術,用于從多個弱分類器創建強分類器。 集成技術的一個眾所周知的例子是隨機森林(Random Forest),它使用多個決策樹來創建一個隨機森林。

直覺:

  • AdaBoost,Adaptive Boosting的縮寫,是第一個成功的針對二進制分類開發的Boosting算法。它是一種監督式機器學習算法,用于提高任何機器學習算法的性能。 最好與像決策樹這樣的弱學習者一起使用。 這些模型在分類問題上的準確性要高于隨機機會。

AdaBoost的概念很簡單:

  • 我們將把數據集傳遞給多個基礎學習者,每個基礎學習者將嘗試更正其前輩分類錯誤的記錄。

我們會將數據集(如下所示,所有行)傳遞給Base Learner1。所有被Base Learner 1誤分類的記錄(行5,6和7被錯誤分類)將被傳遞給Base Learner 2,類似地,所有Base分類器的錯誤分類記錄 學習者2將被傳遞給基本學習者3。最后,根據每個基本學習者的多數票,我們將對新記錄進行分類。

 

數據科學:AdaBoost分類器

讓我們逐步了解AdaBoost分類器的實現:

步驟1:獲取數據集,并將初始權重分配給指定數據集中的所有樣本(行)。

W = 1 / n => 1/7,其中n是樣本數。

 

數據科學:AdaBoost分類器

步驟2:我們將創建第一個基礎學習器。在AdaBoost中,我們將決策樹用作基礎學習器。這里要注意的是,我們將僅創建深度為1的決策樹,它們被稱為決策樹樁或樹樁。

 

數據科學:AdaBoost分類器

我們將為數據集的每個特征創建一個樹樁,就像在我們的例子中,我們將創建三個樹樁,每個特征一個。

步驟3:我們需要根據每個特征的熵值或吉尼系數,選擇任何基礎學習器模型(為特征1創建的基礎學習器1,為特征2創建的基礎學習器2,為特征3創建的基礎學習器3) (我在決策樹文章中已經討論了Ginni和熵)。

熵或吉尼系數值最小的基礎學習器,我們將為第一個基礎學習器模型選擇該模型。

第4步:我們需要找到在第3步中選擇的基本學習者模型正確分類了多少條記錄以及錯誤分類了多少條記錄。

我們必須找到所有錯誤分類的總錯誤,讓我們說我們是否正確分類了4條記錄而錯誤分類了1條記錄

Total Error =分類錯誤的記錄的樣本權重的總和。

因為我們只有1個錯誤,所以總錯誤= 1/7

步驟5:通過以下公式檢查樹樁的性能。

 

數據科學:AdaBoost分類器

當我們將Total Error放入該公式時,我們將得到0.895的值(不要偷懶地將這些值進行計算)

通過以下等式更新正確和錯誤分類的樣本(行)的樣本權重。

錯誤分類的樣本的新樣本權重=

 

數據科學:AdaBoost分類器

 

數據科學:AdaBoost分類器

正確分類的樣本的新樣本權重=

 

數據科學:AdaBoost分類器

 

數據科學:AdaBoost分類器

 

數據科學:AdaBoost分類器

注意:我們假設第3行的分類不正確

步驟6:我們在這里考慮的要點是,當我們將所有樣本權重相加時,它等于1,但如果更新了權重,則總和不等于1。因此,我們將每個更新的權重除以總和,然后歸一化 值為1。

更新權重的總和是0.68

 

數據科學:AdaBoost分類器

第7步:我們將通過刪除"樣品重量"和"更新重量"功能來創建數據集,并將每個樣品(行)分配到一個桶中。

 

數據科學:AdaBoost分類器

步驟8:建立新的資料集。 為此,我們必須從0到1(對于每個樣本(行))中隨機選擇一個值,然后選擇該樣本,該樣本在" Bucket"下掉落,并將該樣本保留在" NEW DATASET"中。 由于分類錯誤的記錄具有較大的存儲桶大小,因此選擇該記錄的可能性非常高。

 

數據科學:AdaBoost分類器

我們在這里可以看到,由于數據桶大小比其他行大,因此已在我們的數據集中選擇了3次錯誤分類的record(Row3)。

第9步:為下一個樹樁(基礎學習者2,基礎學習者3)獲取此新數據集,并按照步驟1至8進行所有功能。

Adaboost模型通過讓森林中的每棵樹對樣本進行分類來進行預測。然后,根據樹木的決策將它們分成幾組。現在,對于每組,我們對各樹中每棵樹的重要性進行累加。 整個森林的數量由總和最大的群體決定。下圖。

 

數據科學:AdaBoost分類器

Adaboost分類器的逐步Python代碼:

  1. In [1]: 
  2. #General idea behind boosting methods is to train predictors sequentially,each trying to correct its predecessor 
  3.  
  4. # AdaBoost Classifier Example In Python 
  5. # Step 1: Initialize the sample weights 
  6. # Step 2: Build a decision tree with each feature, classify the data and evaluate the result 
  7. # Step 3: Calculate the significance of the tree in the final classification 
  8. # Step 4: Update the sample weights so that the next decision tree will take the errors made by the preceding decision tree into account 
  9. # Step 5: Form a new dataset 
  10. # Step 6: Repeat steps 2 through 5 until the number of iterations equals the number specified by the hyperparameter (i.e. number of estimators) 
  11. # Step 7: Use the forest of decision trees to make predictions on data outside of the training set 
  12. In [2]: 
  13. from sklearn.ensemble import AdaBoostClassifier 
  14. from sklearn.tree import DecisionTreeClassifier 
  15. from sklearn.datasets import load_breast_cancer 
  16. import pandas as pd 
  17. import numpy as np 
  18. import seaborn as sns 
  19. import matplotlib.pyplot as plt 
  20. from sklearn.model_selection import train_test_split 
  21. from sklearn.metrics import confusion_matrix,accuracy_score 
  22. from sklearn.preprocessing import LabelEncoder 
  23. import warnings 
  24. warnings.filterwarnings('ignore'
  25. In [3]: 
  26. cancer_data=load_breast_cancer() 
  27. In [4]: 
  28. X=pd.DataFrame(cancer_data.data,columns=cancer_data.feature_names) 
  29. In [5]: 
  30. y = pd.Categorical.from_codes(cancer_data.target, cancer_data.target_names) 
  31. In [6]: 
  32. #y=pd.DataFrame(y,columns=['Cancer_Target']) 
  33. Data Preprocessing 
  34. In [7]: 
  35. X.describe() 
  36. Out[7]: 
  37. mean radius mean texture    mean perimeter  mean area   mean smoothness mean compactness    mean concavity  mean concave points mean symmetry   mean fractal dimension  ... worst radius    worst texture   worst perimeter worst area  worst smoothness    worst compactness   worst concavity worst concave points    worst symmetry  worst fractal dimension 
  38. count   569.000000  569.000000  569.000000  569.000000  569.000000  569.000000  569.000000  569.000000  569.000000  569.000000  ... 569.000000  569.000000  569.000000  569.000000  569.000000  569.000000  569.000000  569.000000  569.000000  569.000000 
  39. mean    14.127292   19.289649   91.969033   654.889104  0.096360    0.104341    0.088799    0.048919    0.181162    0.062798    ... 16.269190   25.677223   107.261213  880.583128  0.132369    0.254265    0.272188    0.114606    0.290076    0.083946 
  40. std 3.524049    4.301036    24.298981   351.914129  0.014064    0.052813    0.079720    0.038803    0.027414    0.007060    ... 4.833242    6.146258    33.602542   569.356993  0.022832    0.157336    0.208624    0.065732    0.061867    0.018061 
  41. min 6.981000    9.710000    43.790000   143.500000  0.052630    0.019380    0.000000    0.000000    0.106000    0.049960    ... 7.930000    12.020000   50.410000   185.200000  0.071170    0.027290    0.000000    0.000000    0.156500    0.055040 
  42. 25% 11.700000   16.170000   75.170000   420.300000  0.086370    0.064920    0.029560    0.020310    0.161900    0.057700    ... 13.010000   21.080000   84.110000   515.300000  0.116600    0.147200    0.114500    0.064930    0.250400    0.071460 
  43. 50% 13.370000   18.840000   86.240000   551.100000  0.095870    0.092630    0.061540    0.033500    0.179200    0.061540    ... 14.970000   25.410000   97.660000   686.500000  0.131300    0.211900    0.226700    0.099930    0.282200    0.080040 
  44. 75% 15.780000   21.800000   104.100000  782.700000  0.105300    0.130400    0.130700    0.074000    0.195700    0.066120    ... 18.790000   29.720000   125.400000  1084.000000 0.146000    0.339100    0.382900    0.161400    0.317900    0.092080 
  45. max 28.110000   39.280000   188.500000  2501.000000 0.163400    0.345400    0.426800    0.201200    0.304000    0.097440    ... 36.040000   49.540000   251.200000  4254.000000 0.222600    1.058000    1.252000    0.291000    0.663800    0.207500 
  46. rows × 30 columns 
  47.  
  48. In [8]: 
  49. X.isnull().count() 
  50. Out[8]: 
  51. mean radius                569 
  52. mean texture               569 
  53. mean perimeter             569 
  54. mean area                  569 
  55. mean smoothness            569 
  56. mean compactness           569 
  57. mean concavity             569 
  58. mean concave points        569 
  59. mean symmetry              569 
  60. mean fractal dimension     569 
  61. radius error               569 
  62. texture error              569 
  63. perimeter error            569 
  64. area error                 569 
  65. smoothness error           569 
  66. compactness error          569 
  67. concavity error            569 
  68. concave points error       569 
  69. symmetry error             569 
  70. fractal dimension error    569 
  71. worst radius               569 
  72. worst texture              569 
  73. worst perimeter            569 
  74. worst area                 569 
  75. worst smoothness           569 
  76. worst compactness          569 
  77. worst concavity            569 
  78. worst concave points       569 
  79. worst symmetry             569 
  80. worst fractal dimension    569 
  81. dtype: int64 
  82. In [9]: 
  83. def heatMap(df): 
  84.     #Create Correlation df 
  85.     corr = X.corr() 
  86.     #Plot figsize 
  87.     fig, ax = plt.subplots(figsize=(25, 25)) 
  88.     #Generate Color Map 
  89.     colormap = sns.diverging_palette(220, 10, as_cmap=True
  90.     #Generate Heat Map, allow annotations and place floats in map 
  91.     sns.heatmap(corr, cmap=colormap, annot=True, fmt=".2f"
  92.     #Apply xticks 
  93.     plt.xticks(range(len(corr.columns)), corr.columns); 
  94.     #Apply yticks 
  95.     plt.yticks(range(len(corr.columns)), corr.columns) 
  96.     #show plot 
  97.     plt.show() 
  98. In [10]: 
  99. heatMap(X) 
  100.  
  101. All the above correlated features can be dropped 
  102. In [11]: 
  103. X.boxplot(figsize=(10,15)) 
  104. Out[11]: 
  105. <matplotlib.axes._subplots.AxesSubplot at 0x1e1a0eb7fd0> 
  106.  
  107. In [12]: 
  108. le=LabelEncoder() 
  109. In [13]: 
  110. y=pd.Series(le.fit_transform(y)) 
  111. Data Split and Model Initialization 
  112. In [14]: 
  113. X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.20,random_state=42) 
  114. In [15]: 
  115. adaboost=AdaBoostClassifier(DecisionTreeClassifier(max_depth=1),n_estimators=200) 
  116. In [16]: 
  117. adaboost.fit(X_train,y_train) 
  118. Out[16]: 
  119. AdaBoostClassifier(algorithm='SAMME.R'
  120.           base_estimator=DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=1, 
  121.             max_features=None, max_leaf_nodes=None, 
  122.             min_impurity_decrease=0.0, min_impurity_split=None, 
  123.             min_samples_leaf=1, min_samples_split=2, 
  124.             min_weight_fraction_leaf=0.0, presort=False, random_state=None, 
  125.             splitter='best'), 
  126.           learning_rate=1.0, n_estimators=200, random_state=None) 
  127. In [17]: 
  128. adaboost.score(X_train,y_train) 
  129. Out[17]: 
  130. 1.0 
  131. In [18]: 
  132. adaboost.score(X_test,y_test) 
  133. Out[18]: 
  134. 0.9736842105263158 
  135. In [19]: 
  136. y_pred=adaboost.predict(X_test) 
  137. In [20]: 
  138. y_pred 
  139. Out[20]: 
  140. array([0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 
  141.        1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 
  142.        0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 
  143.        1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 
  144.        0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 
  145.        1, 0, 0, 1]) 
  146. Result Visualization 
  147. In [21]: 
  148. cm=confusion_matrix(y_test,y_pred) 
  149. print('Confusion Matrix\n {} '.format(cm)) 
  150. Confusion Matrix 
  151.  [[70  1] 
  152.  [ 2 41]]  
  153. In [22]: 
  154. ax= plt.subplot() 
  155. sns.heatmap(cm, annot=True, ax = ax); 
  156. # labels, title and ticks 
  157. ax.set_xlabel('Predicted labels'); 
  158. ax.set_ylabel('True labels');  
  159. ax.set_title('Confusion Matrix'); 
  160.  
  161. In [23]: 
  162. acc=accuracy_score(y_test,y_pred) 
  163. print('Model Accuracy is {} % '.format(round(acc,3))) 
  164. Model Accuracy is 0.974 %  

AdaBoost分類器的優點:

  • AdaBoost可用于提高弱分類器的準確性,從而使其更靈活。 現在,它已擴展到二進制分類之外,并且還發現了文本和圖像分類中的用例。
  • AdaBoost具有很高的精度。
  • 不同的分類算法可以用作弱分類器。

缺點:

  • 提升技巧是逐步學習的,確保您擁有高質量的數據非常重要。
  • AdaBoost對噪聲數據和異常值也非常敏感,因此,如果您打算使用AdaBoost,則強烈建議消除它們。
  • AdaBoost還被證明比XGBoost慢。

結論:在本文中,我們討論了理解AdaBoost算法的各種方法.AdaBoost就像是一個恩賜,如果正確使用它可以提高分類算法的準確性。

希望您喜歡我的文章。請鼓掌(最多50次),這會激發我寫更多的文章。

責任編輯:未麗燕 來源: 今日頭條
相關推薦

2024-11-20 08:29:26

2018-04-16 12:14:34

數據科學機器學習神經網絡

2022-08-19 07:38:51

數據備份系統存儲

2018-04-16 11:11:56

2020-05-27 11:16:49

數據科學機器學習Python

2016-10-21 19:24:35

數據科學家數據科學

2017-08-04 15:53:10

大數據真偽數據科學家

2022-11-14 10:36:55

數據科學數據分析

2023-10-16 10:25:34

數據科學大數據

2019-09-30 09:10:11

Python編程語言數據科學

2015-06-11 10:27:29

數據科學家

2018-06-29 16:00:56

數據科學家數據清理數據分析

2024-12-09 09:44:34

機器學習模型分類器

2020-10-25 20:00:18

數據科學數據科學家

2015-07-23 14:53:50

貝葉斯分類器

2015-07-29 11:14:20

r語言數據科學

2018-11-06 20:30:23

Python開源工具機器學習

2014-07-01 09:20:56

大數據

2012-12-26 10:51:20

數據科學家

2020-12-31 06:18:08

人工智能物聯網大數據
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美精品一区二区三区在线播放 | 男人天堂手机在线视频 | 亚洲欧美日韩精品久久亚洲区 | 欧美色综合一区二区三区 | 国产高清精品一区二区三区 | 亚洲精品二三区 | 日本中文字幕在线视频 | 欧美精品一区二区三区在线播放 | 日本亚洲欧美 | 日韩在线观看中文字幕 | 久草资源在线视频 | 看一级黄色毛片 | 香蕉视频黄色 | 特级黄一级播放 | 欧美三区视频 | 99久久中文字幕三级久久日本 | 激情网站在线观看 | 国产成在线观看免费视频 | 午夜精品视频 | 欧美亚洲国产日韩 | 日本高清不卡视频 | 中文字幕一区在线观看视频 | 久久久久国产精品一区 | 欧美一级在线 | 天堂免费看片 | 国产在线观看一区二区三区 | 99re在线| 国产在线精品一区二区 | 国产在线视频一区二区 | 成人欧美一区二区三区在线观看 | 91影库| 国产一区二区日韩 | 国产精品视频一二三区 | 一区二区在线 | 欧美午夜影院 | 精品一区二区三区免费视频 | 国产精品久久久久久久免费观看 | 一级在线视频 | 国产精品久久精品 | 亚洲精品久久久久久一区二区 | 亚洲成年在线 |