<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Python 中的極端梯度增強(XGBoost)集成

          共 22160字,需瀏覽 45分鐘

           ·

          2021-04-29 21:18

          Extreme Gradient Boosting(XGBoost)是一個開放源代碼庫,它提供了梯度提升算法的有效實現(xiàn)。
          盡管該方法的其他開放源代碼實現(xiàn)在XGBoost之前就已存在,但XGBoost的發(fā)布似乎釋放了該技術(shù)的力量,并使得應用機器學習社區(qū)更普遍地注意到了梯度提升。在開發(fā)并首次發(fā)布后不久,XGBoost成為了首選方法,并且通常是贏得機器學習競賽中的分類和回歸問題解決方案的關(guān)鍵組成部分。
          在本教程中,您將發(fā)現(xiàn)如何開發(fā)用于分類和回歸的極端梯度增強合奏。完成本教程后,您將知道:
          • 極端梯度提升是隨機梯度提升集成算法的高效開源實現(xiàn)。
          • 如何使用scikit-learn API開發(fā)用于分類和回歸的XGBoost集成。
          • 如何探索XGBoost模型超參數(shù)對模型性能的影響。
          教程概述
          本教程分為三個部分,他們是:
          • 極端梯度提升算法
          • XGBoost Scikit-Learn API
            • XGBoost分類集成
            • XGBoost回歸集成
          • XGBoost超參數(shù)
            • 探索樹木數(shù)量
            • 探索樹深
            • 探索學習率
            • 探索樣品數(shù)量
            • 探索特征數(shù)量
          極端梯度提升算法
          梯度提升是指一類集成機器學習算法,可用于分類或回歸預測建模問題。集成是根據(jù)決策樹模型構(gòu)建的。一次將一棵樹添加到集合中,并進行調(diào)整以糾正由先前模型造成的預測誤差。這是一種集成機器學習模型,稱為Boosting。使用任何任意的微分損失函數(shù)和梯度下降優(yōu)化算法對模型進行擬合。這給該技術(shù)起了名字,稱為“梯度提升”,因為隨著模型的擬合,損耗梯度被最小化,非常類似于神經(jīng)網(wǎng)絡(luò)。有關(guān)梯度增強的更多信息,請參見教程:
          機器學習的梯度提升算法簡介
          https://machinelearningmastery.com/gentle-introduction-gradient-boosting-algorithm-machine-learning/
          極端梯度增強(簡稱XGBoost)是梯度增強算法的一種有效的開源實現(xiàn)。因此,XGBoost是一個算法,一個開源項目和一個Python庫。它最初是由Tianqi Chen開發(fā)的,并由Chen和Carlos Guestrin在其2016年的論文“ XGBoost:可擴展的樹增強系統(tǒng)”中進行了描述。它被設(shè)計為既計算效率高(例如執(zhí)行速度快)又高效,也許比其他開源實現(xiàn)更有效。
          使用XGBoost的兩個主要原因是執(zhí)行速度和模型性能。通常,與其他梯度提升實現(xiàn)相比,XGBoost速度很快。Szilard Pafka執(zhí)行了一些客觀基準測試,將XGBoost的性能與梯度提升和袋裝決策樹的其他實現(xiàn)進行了比較。他在2015年5月的博客文章“對隨機森林實施進行基準測試”中寫下了自己的結(jié)果。他的結(jié)果表明,XGBoost幾乎總是比R,Python Spark和H2O的其他基準實現(xiàn)更快。
          在分類和回歸預測建模問題上,XGBoost主導結(jié)構(gòu)化或表格數(shù)據(jù)集。證據(jù)表明,它是Kaggle競爭數(shù)據(jù)科學平臺上競賽獲勝者的首選算法。
          XGBoost Scikit-Learn API
          XGBoost可以作為獨立庫安裝,并且可以使用scikit-learn API開發(fā)XGBoost模型。第一步是安裝XGBoost庫(如果尚未安裝)。這可以在大多數(shù)平臺上使用pip python軟件包管理器來實現(xiàn)。例如:
          sudo pip install xgboost
          然后,您可以確認XGBoost庫已正確安裝,并且可以通過運行以下腳本來使用。
          # check xgboost version
          import xgboost
          print(xgboost.__version__)
          運行腳本將打印您已安裝的XGBoost庫的版本。您的版本應該相同或更高。如果不是,則必須升級XGBoost庫的版本。
          1.1.1
          您可能對庫的最新版本有疑問。不是你的錯。有時,該庫的最新版本會帶來其他要求,或者可能不太穩(wěn)定。如果您在嘗試運行上述腳本時確實出錯,建議您降級至1.0.1版(或更低版本)。這可以通過指定要安裝到pip命令的版本來實現(xiàn),如下所示:
          sudo pip install xgboost==1.0.1
          如果您看到警告消息,則可以暫時將其忽略。例如,以下是您可能會看到并可以忽略的警告消息的示例:
          FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead.
          如果您需要針對您的開發(fā)環(huán)境的特定說明,請參閱教程:
          XGBoost安裝指南

          https://xgboost.readthedocs.io/en/latest/build.html

          XGBoost庫具有自己的自定義API,盡管我們將通過scikit-learn包裝器類使用該方法:XGBRegressor和XGBClassifier。這將使我們能夠使用scikit-learn機器學習庫中的全套工具來準備數(shù)據(jù)和評估模型。兩種模型的操作方式相同,采用的參數(shù)相同,這會影響如何創(chuàng)建決策樹并將其添加到集合中。隨機性用于模型的構(gòu)建。這意味著算法每次在相同數(shù)據(jù)上運行時,都會產(chǎn)生一個略有不同的模型。當使用具有隨機學習算法的機器學習算法時,優(yōu)良作法是通過在多次運行或交叉驗證重復期間平均其性能來評估它們。在擬合最終模型時,可能需要增加樹的數(shù)量,直到在重復評估中減小模型的方差為止,或者擬合多個最終模型并對它們的預測求平均值。
          讓我們看一下如何為分類和回歸開發(fā)XGBoost集成。
          XGBoost分類集成
          在本節(jié)中,我們將研究如何使用XGBoost解決分類問題。首先,我們可以使用make_classification()函數(shù)創(chuàng)建具有1,000個示例和20個輸入功能的綜合二進制分類問題。
          # test classification dataset
          from sklearn.datasets import make_classification
          # define dataset
          X, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=7)
          # summarize the dataset
          print(X.shape, y.shape)
          運行示例將創(chuàng)建數(shù)據(jù)集并總結(jié)輸入和輸出組件的形狀。
          (100020) (1000,)
          接下來,我們可以在該數(shù)據(jù)集上評估XGBoost模型。我們將使用重復的分層k折交叉驗證(具有3個重復和10折)對模型進行評估。我們將報告所有重復和折疊中模型準確性的均值和標準差。
          # evaluate xgboost algorithm for classification
          from numpy import mean
          from numpy import std
          from sklearn.datasets import make_classification
          from sklearn.model_selection import cross_val_score
          from sklearn.model_selection import RepeatedStratifiedKFold
          from xgboost import XGBClassifier
          # define dataset
          X, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=7)
          # define the model
          model = XGBClassifier()
          # evaluate the model
          cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
          n_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
          # report performance
          print('Accuracy: %.3f (%.3f)' % (mean(n_scores), std(n_scores)))
          運行示例將報告模型的均值和標準差準確性。
          注意:由于算法或評估程序的隨機性,或者數(shù)值精度的差異,您的結(jié)果可能會有所不同??紤]運行該示例幾次并比較平均結(jié)果。
          在這種情況下,我們可以看到具有默認超參數(shù)的XGBoost集成在此測試數(shù)據(jù)集上實現(xiàn)了約92.5%的分類精度。
          Accuracy: 0.925 (0.028)
          我們還可以將XGBoost模型用作最終模型,并進行分類預測。
          首先,將XGBoost集合擬合到所有可用數(shù)據(jù)上,然后可以調(diào)用prepare()函數(shù)對新數(shù)據(jù)進行預測。重要的是,此函數(shù)希望數(shù)據(jù)始終以NumPy數(shù)組的形式作為矩陣提供,每個輸入樣本一行。
          下面的示例在我們的二進制分類數(shù)據(jù)集中展示了這一點。
          # make predictions using xgboost for classification
          from numpy import asarray
          from sklearn.datasets import make_classification
          from xgboost import XGBClassifier
          # define dataset
          X, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=7)
          # define the model
          model = XGBClassifier()
          # fit the model on the whole dataset
          model.fit(X, y)
          # make a single prediction
          row = [0.2929949,-4.21223056,-1.288332,-2.17849815,-0.64527665,2.58097719,0.28422388,-7.1827928,-1.91211104,2.73729512,0.81395695,3.96973717,-2.66939799,3.34692332,4.19791821,0.99990998,-0.30201875,-4.43170633,-2.82646737,0.44916808]
          row = asarray([row])
          yhat = model.predict(row)
          print('Predicted Class: %d' % yhat[0])
          運行示例可以使XGBoost集成模型適合整個數(shù)據(jù)集,然后用于對新的數(shù)據(jù)行進行預測,就像我們在應用程序中使用模型時一樣。
          Predicted Class: 1
          現(xiàn)在,我們已經(jīng)熟悉使用XGBoost進行分類了,讓我們看一下用于回歸的API。
          XGBoost回歸集成
          在本節(jié)中,我們將研究如何使用XGBoost解決回歸問題。首先,我們可以使用make_regression()函數(shù)創(chuàng)建具有1000個示例和20個輸入要素的綜合回歸問題。下面列出了完整的示例。
          # test regression dataset
          from sklearn.datasets import make_regression
          # define dataset
          X, y = make_regression(n_samples=1000, n_features=20, n_informative=15, noise=0.1, random_state=7)
          # summarize the dataset
          print(X.shape, y.shape)
          運行示例將創(chuàng)建數(shù)據(jù)集并總結(jié)輸入和輸出組件的形狀。
          (1000, 20) (1000,)
          接下來,我們可以在該數(shù)據(jù)集上評估XGBoost算法。
          正如我們在上一節(jié)所做的那樣,我們將使用重復的k倍交叉驗證(三個重復和10倍)來評估模型。我們將報告所有重復和折疊的模型的平均絕對誤差(MAE)。scikit-learn庫使MAE變?yōu)樨摂?shù),以便最大化而不是最小化。這意味著較大的負MAE會更好,并且理想模型的MAE為0。
          下面列出了完整的示例。
          # evaluate xgboost ensemble for regression
          from numpy import mean
          from numpy import std
          from sklearn.datasets import make_regression
          from sklearn.model_selection import cross_val_score
          from sklearn.model_selection import RepeatedKFold
          from xgboost import XGBRegressor
          # define dataset
          X, y = make_regression(n_samples=1000, n_features=20, n_informative=15, noise=0.1, random_state=7)
          # define the model
          model = XGBRegressor()
          # evaluate the model
          cv = RepeatedKFold(n_splits=10, n_repeats=3, random_state=1)
          n_scores = cross_val_score(model, X, y, scoring='neg_mean_absolute_error', cv=cv, n_jobs=-1, error_score='raise')
          # report performance
          print('MAE: %.3f (%.3f)' % (mean(n_scores), std(n_scores)))
          運行示例將報告模型的均值和標準差準確性。
          注意:由于算法或評估程序的隨機性,或者數(shù)值精度的差異,您的結(jié)果可能會有所不同。考慮運行該示例幾次并比較平均結(jié)果。
          在這種情況下,我們可以看到具有默認超參數(shù)的XGBoost集成達到了約76的MAE。
          MAE: -76.447 (3.859)
          我們還可以將XGBoost模型用作最終模型,并進行回歸預測。首先,將XGBoost集合擬合到所有可用數(shù)據(jù)上,然后可以調(diào)用prepare()函數(shù)對新數(shù)據(jù)進行預測。與分類一樣,單行數(shù)據(jù)必須以NumPy數(shù)組格式表示為二維矩陣。下面的示例在我們的回歸數(shù)據(jù)集中展示了這一點。
          # gradient xgboost for making predictions for regression
          from numpy import asarray
          from sklearn.datasets import make_regression
          from xgboost import XGBRegressor
          # define dataset
          X, y = make_regression(n_samples=1000, n_features=20, n_informative=15, noise=0.1, random_state=7)
          # define the model
          model = XGBRegressor()
          # fit the model on the whole dataset
          model.fit(X, y)
          # make a single prediction
          row = [0.20543991,-0.97049844,-0.81403429,-0.23842689,-0.60704084,-0.48541492,0.53113006,2.01834338,-0.90745243,-1.85859731,-1.02334791,-0.6877744,0.60984819,-0.70630121,-1.29161497,1.32385441,1.42150747,1.26567231,2.56569098,-0.11154792]
          row = asarray([row])
          yhat = model.predict(row)
          print('Prediction: %d' % yhat[0])
          運行示例可以使XGBoost集成模型適合整個數(shù)據(jù)集,然后用于對新的數(shù)據(jù)行進行預測,就像我們在應用程序中使用模型時一樣。
          Prediction: 50
          現(xiàn)在,我們已經(jīng)熟悉使用XGBoost Scikit-Learn API評估和使用XGBoost集成體,現(xiàn)在讓我們來看一下配置模型。
          XGBoost超參數(shù)
          在本節(jié)中,我們將仔細研究一些您應該考慮為Gradient Boosting集成進行調(diào)優(yōu)的超參數(shù)及其對模型性能的影響。
          探索樹木數(shù)量
          XGBoost集成算法的一個重要的超參數(shù)是集成中使用的決策樹的數(shù)量?;叵胍幌?,決策樹被順序地添加到模型中,以糾正和改進現(xiàn)有樹的預測。因此,越多的樹通常越好??梢酝ㄟ^“ n_estimators”參數(shù)設(shè)置樹的數(shù)量,默認為100。下面的示例探討值在10到5,000之間的樹木數(shù)量的影響。
          # explore xgboost number of trees effect on performance
          from numpy import mean
          from numpy import std
          from sklearn.datasets import make_classification
          from sklearn.model_selection import cross_val_score
          from sklearn.model_selection import RepeatedStratifiedKFold
          from xgboost import XGBClassifier
          from matplotlib import pyplot
           
          # get the dataset
          def get_dataset():
           X, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=7)
           return X, y
           
          # get a list of models to evaluate
          def get_models():
           models = dict()
           trees = [105010050010005000]
           for n in trees:
            models[str(n)] = XGBClassifier(n_estimators=n)
           return models
           
          # evaluate a give model using cross-validation
          def evaluate_model(model):
           cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
           scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
           return scores
           
          # define dataset
          X, y = get_dataset()
          # get the models to evaluate
          models = get_models()
          # evaluate the models and store results
          results, names = list(), list()
          for name, model in models.items():
           scores = evaluate_model(model)
           results.append(scores)
           names.append(name)
           print('>%s %.3f (%.3f)' % (name, mean(scores), std(scores)))
          # plot model performance for comparison
          pyplot.boxplot(results, labels=names, showmeans=True)
          pyplot.show()
          首先運行示例將報告每個已配置數(shù)量的決策樹的平均準確性。
          注意:由于算法或評估程序的隨機性,或者數(shù)值精度的差異,您的結(jié)果可能會有所不同??紤]運行該示例幾次并比較平均結(jié)果。
          在這種情況下,我們可以看到該數(shù)據(jù)集的性能有所提高,直到大約500棵樹為止,此后性能似乎趨于穩(wěn)定或下降。
          >10 0.885 (0.029)
          >50 0.915 (0.029)
          >100 0.925 (0.028)
          >500 0.927 (0.028)
          >1000 0.926 (0.028)
          >5000 0.925 (0.027)
          創(chuàng)建箱須圖以分配每種配置數(shù)量的樹的準確性得分。我們可以看到增加模型性能和集合大小的總體趨勢。

          探索樹深
          改變添加到集合中的每棵樹的深度是用于梯度增強的另一個重要的超參數(shù)。樹的深度控制著每棵樹對訓練數(shù)據(jù)集的專業(yè)化程度:樹的一般程度或過擬合程度。首選的樹不是太淺且太普通(例如AdaBoost),而又不是太深且專用(例如自舉聚合)。漸變增強通常在深度適中的樹上表現(xiàn)良好,從而在技巧和通用性之間找到平衡。樹的深度通過“ max_depth”參數(shù)控制,默認為6。下面的示例探討了1到10之間的樹深及其對模型性能的影響。
          # explore xgboost tree depth effect on performance
          from numpy import mean
          from numpy import std
          from sklearn.datasets import make_classification
          from sklearn.model_selection import cross_val_score
          from sklearn.model_selection import RepeatedStratifiedKFold
          from xgboost import XGBClassifier
          from matplotlib import pyplot
           
          # get the dataset
          def get_dataset():
           X, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=7)
           return X, y
           
          # get a list of models to evaluate
          def get_models():
           models = dict()
           for i in range(1,11):
            models[str(i)] = XGBClassifier(max_depth=i)
           return models
           
          # evaluate a give model using cross-validation
          def evaluate_model(model):
           cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
           scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
           return scores
           
          # define dataset
          X, y = get_dataset()
          # get the models to evaluate
          models = get_models()
          # evaluate the models and store results
          results, names = list(), list()
          for name, model in models.items():
           scores = evaluate_model(model)
           results.append(scores)
           names.append(name)
           print('>%s %.3f (%.3f)' % (name, mean(scores), std(scores)))
          # plot model performance for comparison
          pyplot.boxplot(results, labels=names, showmeans=True)
          pyplot.show()
          首先運行示例將報告每個已配置樹深度的平均準確性。
          注意:由于算法或評估程序的隨機性,或者數(shù)值精度的差異,您的結(jié)果可能會有所不同。考慮運行該示例幾次并比較平均結(jié)果。
          在這種情況下,我們可以看到性能隨著樹的深度而提高,可能是在3到8的深度處偷看,之后,更深,更專業(yè)的樹會導致性能變差。
          >1 0.849 (0.028)
          >2 0.906 (0.032)
          >3 0.926 (0.027)
          >4 0.930 (0.027)
          >5 0.924 (0.031)
          >6 0.925 (0.028)
          >7 0.926 (0.030)
          >8 0.926 (0.029)
          >9 0.921 (0.032)
          >10 0.923 (0.035)
          創(chuàng)建箱須圖以分配每個已配置樹深度的準確性得分。
          我們可以看到隨著樹的深度增加模型性能達到一定程度的總體趨勢,此后性能開始趨于平穩(wěn),或者由于過度專業(yè)化的樹而降低。

          探索學習率
          學習率控制著每個模型對整體預測的貢獻量。較低的費率可能需要集合中的更多決策樹。可以通過“ eta”參數(shù)控制學習率,默認為0.3。下面的示例探索學習率,并比較值在0.0001和1.0之間的效果。
          # explore xgboost learning rate effect on performance
          from numpy import mean
          from numpy import std
          from sklearn.datasets import make_classification
          from sklearn.model_selection import cross_val_score
          from sklearn.model_selection import RepeatedStratifiedKFold
          from xgboost import XGBClassifier
          from matplotlib import pyplot
           
          # get the dataset
          def get_dataset():
           X, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=7)
           return X, y
           
          # get a list of models to evaluate
          def get_models():
           models = dict()
           rates = [0.00010.0010.010.11.0]
           for r in rates:
            key = '%.4f' % r
            models[key] = XGBClassifier(eta=r)
           return models
           
          # evaluate a give model using cross-validation
          def evaluate_model(model):
           cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
           scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
           return scores
           
          # define dataset
          X, y = get_dataset()
          # get the models to evaluate
          models = get_models()
          # evaluate the models and store results
          results, names = list(), list()
          for name, model in models.items():
           scores = evaluate_model(model)
           results.append(scores)
           names.append(name)
           print('>%s %.3f (%.3f)' % (name, mean(scores), std(scores)))
          # plot model performance for comparison
          pyplot.boxplot(results, labels=names, showmeans=True)
          pyplot.show()
          首先運行示例將報告每個配置的學習率的平均準確性。
          注意:由于算法或評估程序的隨機性,或者數(shù)值精度的差異,您的結(jié)果可能會有所不同。考慮運行該示例幾次并比較平均結(jié)果。
          在這種情況下,我們可以看到,較高的學習率會導致此數(shù)據(jù)集的性能更高。我們希望為較小的學習率在合奏中添加更多的樹會進一步提高性能。
          這突出了樹的數(shù)量(訓練速度)和學習率之間的權(quán)衡,例如 我們可以通過使用更少的樹和更大的學習率來更快地擬合模型。
          >0.0001 0.804 (0.039)
          >0.0010 0.814 (0.037)
          >0.0100 0.867 (0.027)
          >0.1000 0.923 (0.030)
          >1.0000 0.913 (0.030)
          創(chuàng)建箱須圖以分配每種配置的學習率的準確性得分。我們可以看到隨著學習率增加0.1,模型性能提高的總體趨勢,此后性能下降。

          探索樣本數(shù)量
          用于擬合每棵樹的樣本數(shù)量可以變化。這意味著每棵樹都適合于訓練數(shù)據(jù)集的隨機選擇子集。使用較少的樣本會為每棵樹引入更多方差,盡管它可以改善模型的整體性能。用于擬合每棵樹的樣本數(shù)量由“子樣本”參數(shù)指定,并且可以設(shè)置為訓練數(shù)據(jù)集大小的一小部分。默認情況下,將其設(shè)置為1.0以使用整個訓練數(shù)據(jù)集。下面的示例演示了樣本大小對模型性能的影響,比率從10%到100%以10%的增量變化。
          # explore xgboost subsample ratio effect on performance
          from numpy import arange
          from numpy import mean
          from numpy import std
          from sklearn.datasets import make_classification
          from sklearn.model_selection import cross_val_score
          from sklearn.model_selection import RepeatedStratifiedKFold
          from xgboost import XGBClassifier
          from matplotlib import pyplot
           
          # get the dataset
          def get_dataset():
           X, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=7)
           return X, y
           
          # get a list of models to evaluate
          def get_models():
           models = dict()
           for i in arange(0.11.10.1):
            key = '%.1f' % i
            models[key] = XGBClassifier(subsample=i)
           return models
           
          # evaluate a give model using cross-validation
          def evaluate_model(model):
           cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
           scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
           return scores
           
          # define dataset
          X, y = get_dataset()
          # get the models to evaluate
          models = get_models()
          # evaluate the models and store results
          results, names = list(), list()
          for name, model in models.items():
           scores = evaluate_model(model)
           results.append(scores)
           names.append(name)
           print('>%s %.3f (%.3f)' % (name, mean(scores), std(scores)))
          # plot model performance for comparison
          pyplot.boxplot(results, labels=names, showmeans=True)
          pyplot.show()
          首先運行示例將報告每個配置的樣本量的平均準確度。
          注意:由于算法或評估程序的隨機性,或者數(shù)值精度的差異,您的結(jié)果可能會有所不同。考慮運行該示例幾次并比較平均結(jié)果。
          在這種情況下,我們可以看到,平均性能對于覆蓋大多數(shù)數(shù)據(jù)集(例如80%或更高)的樣本量可能是最好的。
          >0.1 0.876 (0.027)
          >0.2 0.912 (0.033)
          >0.3 0.917 (0.032)
          >0.4 0.925 (0.026)
          >0.5 0.928 (0.027)
          >0.6 0.926 (0.024)
          >0.7 0.925 (0.031)
          >0.8 0.928 (0.028)
          >0.9 0.928 (0.025)
          >1.0 0.925 (0.028)
          創(chuàng)建箱須圖以分配每個配置的采樣率的準確性得分。
          我們可以看到模型性能不斷提高的總體趨勢,可能達到約80%的峰值并保持一定水平。

          探索特征數(shù)量
          用于適應每個決策樹的功能部件的數(shù)量可以變化。與更改樣本數(shù)量一樣,更改要素數(shù)量也會在模型中引入額外的方差,盡管可能需要增加樹的數(shù)量,但可以提高性能。每棵樹使用的特征數(shù)量取為隨機樣本,并由“ colsample_bytree”參數(shù)指定,并且默認為訓練數(shù)據(jù)集中的所有特征,例如 100%或1.0的值。您還可以為每個拆分采樣列,這由“ colsample_bylevel”參數(shù)控制,但是在此我們將不討論此超參數(shù)。下面的示例探討了特征數(shù)量對模型性能的影響,比率從10%到100%以10%的增量變化。
          # explore xgboost column ratio per tree effect on performance
          from numpy import arange
          from numpy import mean
          from numpy import std
          from sklearn.datasets import make_classification
          from sklearn.model_selection import cross_val_score
          from sklearn.model_selection import RepeatedStratifiedKFold
          from xgboost import XGBClassifier
          from matplotlib import pyplot
           
          # get the dataset
          def get_dataset():
           X, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=7)
           return X, y
           
          # get a list of models to evaluate
          def get_models():
           models = dict()
           for i in arange(0.11.10.1):
            key = '%.1f' % i
            models[key] = XGBClassifier(colsample_bytree=i)
           return models
           
          # evaluate a give model using cross-validation
          def evaluate_model(model):
           cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
           scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
           return scores
           
          # define dataset
          X, y = get_dataset()
          # get the models to evaluate
          models = get_models()
          # evaluate the models and store results
          results, names = list(), list()
          for name, model in models.items():
           scores = evaluate_model(model)
           results.append(scores)
           names.append(name)
           print('>%s %.3f (%.3f)' % (name, mean(scores), std(scores)))
          # plot model performance for comparison
          pyplot.boxplot(results, labels=names, showmeans=True)
          pyplot.show()
          首先運行示例將報告每個配置的列比率的平均準確度。
          注意:由于算法或評估程序的隨機性,或者數(shù)值精度的差異,您的結(jié)果可能會有所不同。考慮運行該示例幾次并比較平均結(jié)果。
          在這種情況下,我們可以看到平均性能提高到功能數(shù)量的一半(50%),并在此之后保持一定水平。令人驚訝的是,每棵樹刪除一半的輸入變量影響很小。
          >0.1 0.861 (0.033)
          >0.2 0.906 (0.027)
          >0.3 0.923 (0.029)
          >0.4 0.917 (0.029)
          >0.5 0.928 (0.030)
          >0.6 0.929 (0.031)
          >0.7 0.924 (0.027)
          >0.8 0.931 (0.025)
          >0.9 0.927 (0.033)
          >1.0 0.925 (0.028)

          創(chuàng)建箱須圖以分配每個配置的列比率的準確性得分。

          我們可以看到提高模型性能的總體趨勢可能達到60%的峰值并保持一定水平。


          者:沂水寒城,CSDN博客專家,個人研究方向:機器學習、深度學習、NLP、CV

          Blog: http://yishuihancheng.blog.csdn.net


          贊 賞 作 者



          更多閱讀



          用 XGBoost 進行時間序列預測


          5分鐘掌握 Python 隨機爬山算法


          5分鐘完全讀懂關(guān)聯(lián)規(guī)則挖掘算法

          特別推薦




          點擊下方閱讀原文加入社區(qū)會員

          瀏覽 87
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  偷拍97| 亚洲逼逼| 国产精品一区在线观看 | 亚洲天堂婷婷 | 亚洲精品乱码久久久久久9色 |