自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python中用XGBoost和scikit-learn進行隨機梯度增強

開發(fā) 后端
在本文中,您將發(fā)現(xiàn)隨機梯度增強以及如何使用XGBoost和Python中的scikit-learn來調(diào)整采樣參數(shù)。

[[399020]]

 集成決策樹的一種簡單技術(shù)涉及在訓(xùn)練數(shù)據(jù)集的子樣本上訓(xùn)練樹??梢圆捎糜?xùn)練數(shù)據(jù)中行的子集來訓(xùn)練稱為袋裝的單個樹。在計算每個分割點時,如果還使用了訓(xùn)練數(shù)據(jù)的行的子集,則這稱為隨機森林。這些技術(shù)也可以在稱為隨機梯度增強的技術(shù)中用于梯度樹增強模型。

在本文中,您將發(fā)現(xiàn)隨機梯度增強以及如何使用XGBoost和Python中的scikit-learn來調(diào)整采樣參數(shù)。閱讀這篇文章后,您將知道:

  •  在數(shù)據(jù)子樣本上訓(xùn)練樹的原理以及如何將其用于梯度增強。
  •  如何使用scikit-learn調(diào)整XGBoost中基于行的子采樣。
  •  如何在XGBoost中按樹和拆分點調(diào)整基于列的子采樣。

隨機梯度提升

梯度增強是一個貪婪的過程。將新的決策樹添加到模型中,以更正現(xiàn)有模型的殘差。使用貪婪搜索過程創(chuàng)建每個決策樹,以選擇最能最小化目標函數(shù)的分割點。這可能會導(dǎo)致樹一次又一次使用相同的屬性,甚至使用相同的分割點。

套袋是一種創(chuàng)建決策樹集合的技術(shù),每個決策樹都來自訓(xùn)練數(shù)據(jù)中不同的隨機行子集。效果是,由于樣本的隨機性允許創(chuàng)建略有不同的樹木,因此從樹的集合中獲得了更好的性能,從而為集合的預(yù)測增加了方差。隨機森林通過在選擇分割點時對要素(列)進行二次采樣,從而進一步擴大了這一步驟,從而進一步增加了樹木的整體差異。這些相同的技術(shù)可以用于梯度提升中決策樹的構(gòu)建中,這種變化稱為隨機梯度提升。通常使用訓(xùn)練數(shù)據(jù)的激進子樣本,例如40%到80%。

教程概述

在本教程中,我們將研究不同的二次采樣技術(shù)在梯度增強中的作用。我們將調(diào)整Python的XGBoost庫所支持的三種不同的隨機梯度增強方式,特別是:

  •  創(chuàng)建每棵樹時,對數(shù)據(jù)集中的行進行二次采樣。
  •  創(chuàng)建每棵樹時對數(shù)據(jù)集中的列進行二次采樣。
  •  創(chuàng)建每個樹時,數(shù)據(jù)集中每個拆分的列的子采樣。

問題描述:Otto數(shù)據(jù)集

在本教程中,我們將使用“奧托集團產(chǎn)品分類挑戰(zhàn)”數(shù)據(jù)集。該數(shù)據(jù)集可從Kaggle免費獲得(您需要注冊到Kaggle才能下載此數(shù)據(jù)集)。您可以從“數(shù)據(jù)”頁面下載訓(xùn)練數(shù)據(jù)集train.csv.zip并將解壓縮后的train.csv文件放入您的工作目錄中。該數(shù)據(jù)集描述了61,000多種產(chǎn)品的93個混淆細節(jié),這些產(chǎn)品分為10個產(chǎn)品類別(例如,時尚,電子產(chǎn)品等)。輸入屬性是某種不同事件的計數(shù)。目標是對新產(chǎn)品做出預(yù)測,將其作為10個類別中每一個類別的概率數(shù)組,并使用多類對數(shù)損失(也稱為交叉熵)對模型進行評估。該競賽已于2015年5月完成,并且由于示例數(shù)量不多,問題難度大,幾乎不需要數(shù)據(jù)準備(除了將字符串類變量編碼為整數(shù))的事實,該數(shù)據(jù)集對于XGBoost還是一個很大的挑戰(zhàn)。

在XGBoost中調(diào)整行二次采樣

行二次抽樣涉及選擇訓(xùn)練數(shù)據(jù)集的隨機樣本而不進行替換??梢栽趕ubsample參數(shù)的XGBoost類的scikit-learn包裝器中指定行子采樣。默認值為1.0,該值不進行二次采樣。我們可以使用scikit-learn中內(nèi)置的網(wǎng)格搜索功能來評估從0.1到1.0的不同子樣本值對Otto數(shù)據(jù)集的影響。 

  1. [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 1.0] 

子樣本有9個變體,每個模型將使用10倍交叉驗證進行評估,這意味著需要訓(xùn)練和測試9×10或90個模型。

下面提供了完整的代碼清單。 

  1. # XGBoost on Otto dataset, tune subsample  
  2. from pandas import read_csv  
  3. from xgboost import XGBClassifier  
  4. from sklearn.model_selection import GridSearchCV  
  5. from sklearn.model_selection import StratifiedKFold  
  6. from sklearn.preprocessing import LabelEncoder  
  7. import matplotlib  
  8. matplotlib.use('Agg')  
  9. from matplotlib import pyplot  
  10. # load data  
  11. data = read_csv('train.csv')  
  12. datadataset = data.values  
  13. # split data into X and y  
  14. X = dataset[:,0:94]  
  15. y = dataset[:,94]  
  16. # encode string class values as integers  
  17. label_encoded_y = LabelEncoder().fit_transform(y)  
  18. # grid search  
  19. model = XGBClassifier()  
  20. subsample = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 1.0]  
  21. param_grid = dict(subsamplesubsample=subsample)  
  22. kfold = StratifiedKFold(n_splits=10shuffle=Truerandom_state=7 
  23. grid_search = GridSearchCV(model, param_grid, scoring="neg_log_loss"n_jobs=-1, cv=kfold 
  24. grid_result = grid_search.fit(X, label_encoded_y)  
  25. # summarize results  
  26. print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))  
  27. means = grid_result.cv_results_['mean_test_score']  
  28. stds = grid_result.cv_results_['std_test_score']  
  29. params = grid_result.cv_results_['params']  
  30. for mean, stdev, param in zip(means, stds, params):  
  31.  print("%f (%f) with: %r" % (mean, stdev, param))  
  32. # plot  
  33. pyplot.errorbar(subsample, means, yerr=stds 
  34. pyplot.title("XGBoost subsample vs Log Loss")  
  35. pyplot.xlabel('subsample')  
  36. pyplot.ylabel('Log Loss')  
  37. pyplot.savefig('subsample.png') 

運行此示例將打印最佳配置以及每個測試配置的日志丟失。

注意:由于算法或評估程序的隨機性,或者數(shù)值精度的差異,您的結(jié)果可能會有所不同。考慮運行該示例幾次并比較平均結(jié)果。

我們可以看到,獲得的最佳結(jié)果是0.3,或者使用30%的訓(xùn)練數(shù)據(jù)集樣本訓(xùn)練樹。 

  1. Best: -0.000647 using {'subsample': 0.3}  
  2. -0.001156 (0.000286) with: {'subsample': 0.1}  
  3. -0.000765 (0.000430) with: {'subsample': 0.2}  
  4. -0.000647 (0.000471) with: {'subsample': 0.3}  
  5. -0.000659 (0.000635) with: {'subsample': 0.4}  
  6. -0.000717 (0.000849) with: {'subsample': 0.5}  
  7. -0.000773 (0.000998) with: {'subsample': 0.6}  
  8. -0.000877 (0.001179) with: {'subsample': 0.7}  
  9. -0.001007 (0.001371) with: {'subsample': 0.8}  
  10. -0.001239 (0.001730) with: {'subsample': 1.0} 

我們可以繪制這些均值和標準偏差對數(shù)損失值,以更好地了解性能如何隨子樣本值變化。

我們可以看到確實有30%的人具有最佳的平均表現(xiàn),但是我們也可以看到,隨著比率的增加,表現(xiàn)的差異會明顯增加。有趣的是,所有子樣本值的平均性能都優(yōu)于不進行子抽樣的平均性能(子樣本= 1.0)。

在XGBoost中按樹調(diào)整列二次采樣

我們還可以在增強模型中創(chuàng)建每個決策樹之前,創(chuàng)建要使用的特征(或列)的隨機樣本。在scikit-learn的XGBoost包裝器中,這由colsample_bytree參數(shù)控制。默認值為1.0,表示在每個決策樹中使用所有列。我們可以在0.1到1.0之間評估colsample_bytree的值,以0.1為增量。 

  1. [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 1.0] 

完整實例如下: 

  1. # XGBoost on Otto dataset, tune colsample_bytree  
  2. from pandas import read_csv  
  3. from xgboost import XGBClassifier  
  4. from sklearn.model_selection import GridSearchCV  
  5. from sklearn.model_selection import StratifiedKFold  
  6. from sklearn.preprocessing import LabelEncoder  
  7. import matplotlib  
  8. matplotlib.use('Agg')  
  9. from matplotlib import pyplot  
  10. # load data  
  11. data = read_csv('train.csv')  
  12. datadataset = data.values  
  13. # split data into X and y  
  14. X = dataset[:,0:94]  
  15. y = dataset[:,94]  
  16. # encode string class values as integers  
  17. label_encoded_y = LabelEncoder().fit_transform(y)  
  18. # grid search  
  19. model = XGBClassifier()  
  20. colsample_bytree = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 1.0]  
  21. param_grid = dict(colsample_bytreecolsample_bytree=colsample_bytree)  
  22. kfold = StratifiedKFold(n_splits=10shuffle=Truerandom_state=7 
  23. grid_search = GridSearchCV(model, param_grid, scoring="neg_log_loss"n_jobs=-1, cv=kfold 
  24. grid_result = grid_search.fit(X, label_encoded_y)  
  25. # summarize results  
  26. print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))  
  27. means = grid_result.cv_results_['mean_test_score']  
  28. stds = grid_result.cv_results_['std_test_score']  
  29. params = grid_result.cv_results_['params']  
  30. for mean, stdev, param in zip(means, stds, params):  
  31.  print("%f (%f) with: %r" % (mean, stdev, param))  
  32. # plot  
  33. pyplot.errorbar(colsample_bytree, means, yerr=stds 
  34. pyplot.title("XGBoost colsample_bytree vs Log Loss")  
  35. pyplot.xlabel('colsample_bytree')  
  36. pyplot.ylabel('Log Loss')  
  37. pyplot.savefig('colsample_bytree.png') 

運行此示例將打印最佳配置以及每個測試配置的日志丟失。

注意:由于算法或評估程序的隨機性,或數(shù)值精度的差異,您的結(jié)果可能會有所不同。

我們可以看到,模型的最佳性能是colsample_bytree = 1.0。這表明該問題進行二次采樣不會增加價值。 

  1. Best: -0.001239 using {'colsample_bytree': 1.0}  
  2. -0.298955 (0.002177) with: {'colsample_bytree': 0.1}  
  3. -0.092441 (0.000798) with: {'colsample_bytree': 0.2}  
  4. -0.029993 (0.000459) with: {'colsample_bytree': 0.3}  
  5. -0.010435 (0.000669) with: {'colsample_bytree': 0.4}  
  6. -0.004176 (0.000916) with: {'colsample_bytree': 0.5}  
  7. -0.002614 (0.001062) with: {'colsample_bytree': 0.6}  
  8. -0.001694 (0.001221) with: {'colsample_bytree': 0.7}  
  9. -0.001306 (0.001435) with: {'colsample_bytree': 0.8}  
  10. -0.001239 (0.001730) with: {'colsample_bytree': 1.0} 

繪制結(jié)果,我們可以看到模型平穩(wěn)段的性能(至少在此比例下),值為0.5到1.0。

通過拆分在XGBoost中調(diào)整列二次采樣

不必為每個樹對列進行一次子采樣,我們可以在決策樹的每個拆分中對它們進行子采樣。原則上,這是隨機森林中使用的方法。我們可以在scikit-learn的XGBoost包裝器類的colsample_bylevel參數(shù)中設(shè)置每個拆分所使用的列樣本的大小。和以前一樣,我們將比率從10%更改為默認值100%。

下面提供了完整的代碼清單。 

  1. # XGBoost on Otto dataset, tune colsample_bylevel  
  2. from pandas import read_csv  
  3. from xgboost import XGBClassifier  
  4. from sklearn.model_selection import GridSearchCV  
  5. from sklearn.model_selection import StratifiedKFold  
  6. from sklearn.preprocessing import LabelEncoder  
  7. import matplotlib  
  8. matplotlib.use('Agg')  
  9. from matplotlib import pyplot  
  10. # load data  
  11. data = read_csv('train.csv')  
  12. datadataset = data.values  
  13. # split data into X and y  
  14. X = dataset[:,0:94]  
  15. y = dataset[:,94]  
  16. # encode string class values as integers  
  17. label_encoded_y = LabelEncoder().fit_transform(y)  
  18. # grid search  
  19. model = XGBClassifier()  
  20. colsample_bylevel = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 1.0]  
  21. param_grid = dict(colsample_bylevelcolsample_bylevel=colsample_bylevel)  
  22. kfold = StratifiedKFold(n_splits=10shuffle=Truerandom_state=7 
  23. grid_search = GridSearchCV(model, param_grid, scoring="neg_log_loss"n_jobs=-1, cv=kfold 
  24. grid_result = grid_search.fit(X, label_encoded_y)  
  25. # summarize results  
  26. print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))  
  27. means = grid_result.cv_results_['mean_test_score']  
  28. stds = grid_result.cv_results_['std_test_score']  
  29. params = grid_result.cv_results_['params']  
  30. for mean, stdev, param in zip(means, stds, params):  
  31.  print("%f (%f) with: %r" % (mean, stdev, param))  
  32. # plot  
  33. pyplot.errorbar(colsample_bylevel, means, yerr=stds 
  34. pyplot.title("XGBoost colsample_bylevel vs Log Loss")  
  35. pyplot.xlabel('colsample_bylevel')  
  36. pyplot.ylabel('Log Loss')  
  37. pyplot.savefig('colsample_bylevel.png') 

運行此示例將打印最佳配置以及每個測試配置的日志丟失。

注意:由于算法或評估程序的隨機性,或者數(shù)值精度的差異,您的結(jié)果可能會有所不同??紤]運行該示例幾次并比較平均結(jié)果。

我們可以看到,通過將colsample_bylevel設(shè)置為70%可獲得最佳結(jié)果,導(dǎo)致(倒置)對數(shù)損失為-0.001062,這比將每棵樹的列采樣設(shè)置為100%時看到的-0.001239好。

如果每棵樹的結(jié)果建議使用100%的列,則建議不要放棄列二次采樣,而應(yīng)嘗試按拆分的列二次采樣。 

  1. Best: -0.001062 using {'colsample_bylevel': 0.7}  
  2. -0.159455 (0.007028) with: {'colsample_bylevel': 0.1}  
  3. -0.034391 (0.003533) with: {'colsample_bylevel': 0.2}  
  4. -0.007619 (0.000451) with: {'colsample_bylevel': 0.3}  
  5. -0.002982 (0.000726) with: {'colsample_bylevel': 0.4}  
  6. -0.001410 (0.000946) with: {'colsample_bylevel': 0.5}  
  7. -0.001182 (0.001144) with: {'colsample_bylevel': 0.6}  
  8. -0.001062 (0.001221) with: {'colsample_bylevel': 0.7}  
  9. -0.001071 (0.001427) with: {'colsample_bylevel': 0.8}  
  10. -0.001239 (0.001730) with: {'colsample_bylevel': 1.0} 

我們可以繪制每個colsample_bylevel變化的性能。結(jié)果表明,在此比例下的值為0.3后,方差相對較低,并且性能似乎處于平穩(wěn)狀態(tài)。 

 

責(zé)任編輯:龐桂玉 來源: Python中文社區(qū) (ID:python-china)
相關(guān)推薦

2015-07-22 16:16:47

PythonScikit-Lear機器學(xué)習(xí)

2018-09-06 08:00:00

深度學(xué)習(xí)TensorFlowPython

2023-02-13 15:00:13

機器學(xué)習(xí)scikit-leaPyTorch

2018-10-15 09:10:09

Python編程語言數(shù)據(jù)科學(xué)

2023-05-26 12:45:22

predict?方法數(shù)據(jù)

2017-07-20 10:23:20

pythonscikit-lear垃圾郵件過濾

2023-11-13 18:05:53

處理數(shù)據(jù)搭建模型

2017-01-05 10:07:33

大數(shù)據(jù)TheanoCaffe

2018-04-06 05:10:04

K-NN數(shù)據(jù)集算法

2024-02-01 09:43:32

模型人工智能

2018-05-15 08:27:20

Scikit-lear機器學(xué)習(xí)Python

2017-11-03 12:57:06

機器學(xué)習(xí)文本數(shù)據(jù)Python

2022-04-15 10:11:03

機器學(xué)習(xí)scikit-lea

2017-04-21 09:59:11

開源機器學(xué)習(xí)框架

2016-12-18 15:03:57

Python Scikit Lea數(shù)據(jù)

2016-12-20 16:07:13

Python數(shù)據(jù)預(yù)處理

2023-03-27 07:34:28

XGBoostInluxDB時間序列

2021-04-16 20:46:21

PythonXGBoost 特征

2021-04-07 10:02:00

XGBoostPython代碼

2023-08-11 10:58:04

深度學(xué)習(xí)自然語言檢索增強
點贊
收藏

51CTO技術(shù)棧公眾號