偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

5分鐘掌握手動優(yōu)化機器學習模型超參數(shù)

人工智能 機器學習
在本教程中,您將發(fā)現(xiàn)如何手動優(yōu)化機器學習算法的超參數(shù)。

[[396168]]

 機器學習算法具有超參數(shù),可讓這些算法針對特定的數(shù)據(jù)集進行量身定制。

盡管通??梢岳斫獬瑓?shù)的影響,但是可能不知道它們對數(shù)據(jù)集的特定影響以及它們在學習期間的交互作用。因此,作為機器學習項目的一部分,調(diào)整算法超參數(shù)的值很重要。

通常使用簡單的優(yōu)化算法來調(diào)整超參數(shù),例如網(wǎng)格搜索和隨機搜索。另一種方法是使用隨機優(yōu)化算法,例如隨機爬山算法。

在本教程中,您將發(fā)現(xiàn)如何手動優(yōu)化機器學習算法的超參數(shù)。完成本教程后,您將知道:

  •  可以使用隨機優(yōu)化算法代替網(wǎng)格和隨機搜索來進行超參數(shù)優(yōu)化。
  •  如何使用隨機爬山算法調(diào)整 Perceptron 算法的超參數(shù)。
  •  如何手動優(yōu)化 XGBoost 梯度提升算法的超參數(shù)。

教程概述

本教程分為三個部分:他們是:

  •  手動超參數(shù)優(yōu)化
  •  感知器超參數(shù)優(yōu)化
  •  XGBoost 超參數(shù)優(yōu)化

手動超參數(shù)優(yōu)化

機器學習模型具有必須設(shè)置的超參數(shù),以便針對數(shù)據(jù)集自定義模型。通常,超參數(shù)對模型的一般影響是已知的,但是如何為給定的數(shù)據(jù)集最佳地設(shè)置超參數(shù)以及相互作用的超參數(shù)的組合具有挑戰(zhàn)性。更好的方法是客觀地搜索模型超參數(shù)的不同值,然后選擇一個子集,以使模型在給定的數(shù)據(jù)集上獲得最佳性能。這稱為超參數(shù)優(yōu)化或超參數(shù)調(diào)整。盡管最簡單和最常見的兩種方法是隨機搜索和網(wǎng)格搜索,但是可以使用一系列不同的優(yōu)化算法。

隨機搜索。將搜索空間定義為超參數(shù)值的有界域,并在該域中隨機采樣點。

網(wǎng)格搜索。將搜索空間定義為超參數(shù)值的網(wǎng)格,并評估網(wǎng)格中的每個位置。

網(wǎng)格搜索非常適用于抽簽檢查組合,這些組合通常表現(xiàn)良好。隨機搜索非常適合發(fā)現(xiàn)和獲取您可能不會直觀地猜到的超參數(shù)組合,盡管它通常需要更多時間來執(zhí)行。

有關(guān)網(wǎng)格和隨機搜索以進行超參數(shù)調(diào)整的更多信息,請參見教程:

隨機搜索和網(wǎng)格搜索的超參數(shù)優(yōu)化

https://machinelearningmastery.com/hyperparameter-optimization-with-random-search-and-grid-search/

網(wǎng)格和隨機搜索是原始的優(yōu)化算法,可以使用我們喜歡的任何優(yōu)化來調(diào)整機器學習算法的性能。例如,可以使用隨機優(yōu)化算法。當需要良好或出色的性能并且有足夠的資源可用于調(diào)整模型時,這可能是理想的。接下來,讓我們看看如何使用

感知器超參數(shù)優(yōu)化

Perceptron 算法是最簡單的人工神經(jīng)網(wǎng)絡(luò)類型。它是單個神經(jīng)元的模型,可用于兩類分類問題,并為以后開發(fā)更大的網(wǎng)絡(luò)提供了基礎(chǔ)。在本節(jié)中,我們將探索如何手動優(yōu)化 Perceptron 模型的超參數(shù)。首先,讓我們定義一個綜合二進制分類問題,我們可以將其用作優(yōu)化模型的重點。我們可以使用make_classification()函數(shù)來定義一個包含1,000行和五個輸入變量的二進制分類問題。下面的示例創(chuàng)建數(shù)據(jù)集并總結(jié)數(shù)據(jù)的形狀。 

  1. # define a binary classification dataset  
  2. from sklearn.datasets import make_classification  
  3. # define dataset  
  4. X, y = make_classification(n_samples=1000n_features=5n_informative=2n_redundant=1random_state=1 
  5. # summarize the shape of the dataset  
  6. print(X.shape, y.shape) 

運行示例將打印創(chuàng)建的數(shù)據(jù)集的形狀,從而確認我們的期望。

  1. (1000, 5) (1000,) 

scikit-learn 通過 Perceptron 類提供了 Perceptron 模型的實現(xiàn)。

在調(diào)整模型的超參數(shù)之前,我們可以使用默認的超參數(shù)建立性能基準。

我們將通過 RepeatedStratifiedKFold 類使用重復分層k折交叉驗證的良好實踐來評估模型。下面列出了在我們的合成二進制分類數(shù)據(jù)集中使用默認超參數(shù)評估 Perceptron 模型的完整示例。 

  1. # perceptron default hyperparameters for binary classification  
  2. from numpy import mean  
  3. from numpy import std  
  4. from sklearn.datasets import make_classification  
  5. from sklearn.model_selection import cross_val_score  
  6. from sklearn.model_selection import RepeatedStratifiedKFold  
  7. from sklearn.linear_model import Perceptron  
  8. # define dataset  
  9. X, y = make_classification(n_samples=1000n_features=5n_informative=2n_redundant=1random_state=1 
  10. # define model  
  11. model = Perceptron()  
  12. # define evaluation procedure 
  13. cv = RepeatedStratifiedKFold(n_splits=10n_repeats=3random_state=1 
  14. # evaluate model 
  15. scores = cross_val_score(model, X, y, scoring='accuracy'cvcv=cv, n_jobs=-1)  
  16. # report result  
  17. print('Mean Accuracy: %.3f (%.3f)' % (mean(scores), std(scores))) 

運行示例報告將評估模型,并報告分類準確性的平均值和標準偏差。

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

在這種情況下,我們可以看到具有默認超參數(shù)的模型實現(xiàn)了約78.5%的分類精度。

我們希望通過優(yōu)化的超參數(shù)可以實現(xiàn)比此更好的性能。 

  1. Mean Accuracy: 0.786 (0.069) 

接下來,我們可以使用隨機爬山算法優(yōu)化 Perceptron 模型的超參數(shù)。我們可以優(yōu)化許多超參數(shù),盡管我們將重點放在可能對模型的學習行為影響最大的兩個參數(shù)上。他們是:

  •  學習率(eta0)
  •  正則化(alpha)

學習率控制基于預測誤差的模型更新量,并控制學習速度。eta的默認值為1.0。合理的值應大于零(例如,大于1e-8或1e-10),并且可能小于1.0默認情況下,Perceptron 不使用任何正則化但是我們將啟用“彈性網(wǎng)”正則化,在學習過程中同時應用L1和L2正則化。這將鼓勵模型尋求較小的模型權(quán)重,從而往往獲得更好的性能。我們將調(diào)整用于控制正則化權(quán)重的“ alpha”超參數(shù),例如它影響學習的數(shù)量。如果設(shè)置為0.0,則好像沒有使用正則化。合理的值在0.0到1.0之間。首先,我們需要為優(yōu)化算法定義目標函數(shù)。我們將使用平均分類精度和重復的分層k折交叉驗證來評估配置。我們將努力使配置的準確性最大化。下面的 Objective() 函數(shù)實現(xiàn)了這一點,采用了數(shù)據(jù)集和配置值列表。將配置值(學習率和正則化權(quán)重)解壓縮,用于配置模型,然后對模型進行評估,并返回平均準確度。 

  1. # objective function  
  2. def objective(X, y, cfg):  
  3.  # unpack config  
  4.  eta, alpha = cfg  
  5.  # define model  
  6.  model = Perceptron(penalty='elasticnet'alphaalpha=alpha, etaeta0=eta)  
  7.  # define evaluation procedure 
  8.  cv = RepeatedStratifiedKFold(n_splits=10n_repeats=3random_state=1 
  9.  # evaluate model  
  10.  scores = cross_val_score(model, X, y, scoring='accuracy'cvcv=cv, n_jobs=-1)  
  11.  # calculate mean accuracy  
  12.  result = mean(scores)  
  13.  return result 

接下來,我們需要一個函數(shù)來在搜索空間中邁出一步。搜索空間由兩個變量(eta和alpha)定義。搜索空間中的某個步驟必須與先前的值有一定關(guān)系,并且必須綁定到合理的值(例如0到1之間)。我們將使用“步長”超參數(shù)來控制允許算法從現(xiàn)有配置移動多遠。使用高斯分布以當前值作為分布的平均值,以步長作為分布的標準偏差來概率地選擇新的配置。我們可以使用randn(),NumPy函數(shù)生成具有高斯分布的隨機數(shù)。下面的step()函數(shù)實現(xiàn)了這一點,并將在搜索空間中邁出一步,并使用現(xiàn)有配置生成新配置。 

  1. # take a step in the search space  
  2. def step(cfg, step_size):  
  3.  # unpack the configuration  
  4.  eta, alpha = cfg  
  5.  # step eta  
  6.  new_eta = eta + randn() * step_size  
  7.  # check the bounds of eta  
  8.  if new_eta <= 0.0:  
  9.   new_eta = 1e-8  
  10.  # step alpha  
  11.  new_alpha = alpha + randn() * step_size  
  12.  # check the bounds of alpha  
  13.  if new_alpha < 0.0:  
  14.   new_alpha = 0.0  
  15.  # return the new configuration  
  16.  return [new_eta, new_alpha] 

接下來,我們需要實現(xiàn)隨機爬山算法,該算法將調(diào)用我們的Objective()函數(shù)來評估候選解,而我們的step()函數(shù)將在搜索空間中邁出一步。搜索首先生成一個隨機初始解,在這種情況下,eta和alpha值在0到1范圍內(nèi)。然后評估初始解并將其視為當前最佳工作解。 

  1. # starting point for the search  
  2. solution = [rand(), rand()]  
  3. # evaluate the initial point  
  4. solution_eval = objective(X, y, solution) 

接下來,該算法將迭代進行固定次數(shù)的迭代,作為提供給搜索的超參數(shù)。每次迭代都需要采取步驟并評估新的候選解決方案。 

  1. # take a step 
  2. candidate = step(solution, step_size)  
  3. # evaluate candidate point  
  4. candidte_eval = objective(X, y, candidate) 

如果新解決方案比當前工作解決方案好,則將其視為新的當前工作解決方案。 

  1. # check if we should keep the new point  
  2. if candidte_eval >= solution_eval:  
  3.  # store the new point  
  4.  solution, solution_eval = candidate, candidte_eval  
  5.  # report progress  
  6.  print('>%d, cfg=%s %.5f' % (i, solution, solution_eval)) 

搜索結(jié)束時,將返回最佳解決方案及其性能。結(jié)合在一起,下面的hillclimbing()函數(shù)以數(shù)據(jù)集,目標函數(shù),迭代次數(shù)和步長為參數(shù),實現(xiàn)了用于調(diào)整 Perceptron 算法的隨機爬山算法。 

  1. # hill climbing local search algorithm  
  2. def hillclimbing(X, y, objective, n_iter, step_size):  
  3.  # starting point for the search  
  4.  solution = [rand(), rand()]  
  5.  # evaluate the initial point  
  6.  solution_eval = objective(X, y, solution)  
  7.  # run the hill climb  
  8.  for i in range(n_iter): 
  9.   # take a step  
  10.   candidate = step(solution, step_size)  
  11.   # evaluate candidate point  
  12.   candidte_eval = objective(X, y, candidate)  
  13.   # check if we should keep the new point  
  14.   if candidte_eval >= solution_eval:  
  15.    # store the new point  
  16.    solution, solution_eval = candidate, candidte_eval  
  17.    # report progress 
  18.    print('>%d, cfg=%s %.5f' % (i, solution, solution_eval))  
  19.  return [solution, solution_eval] 

然后,我們可以調(diào)用算法并報告搜索結(jié)果。在這種情況下,我們將運行該算法100次迭代,并使用0.1步長,這是在經(jīng)過反復試驗后選擇的。 

  1. # define the total iterations  
  2. n_iter = 100  
  3. # step size in the search space  
  4. step_size = 0.1  
  5. # perform the hill climbing search  
  6. cfg, score = hillclimbing(X, y, objective, n_iter, step_size)  
  7. print('Done!')  
  8. print('cfg=%s: Mean Accuracy: %f' % (cfg, score)) 

結(jié)合在一起,下面列出了手動調(diào)整 Perceptron 算法的完整示例。 

  1. # manually search perceptron hyperparameters for binary classification  
  2. from numpy import mean  
  3. from numpy.random import randn  
  4. from numpy.random import rand  
  5. from sklearn.datasets import make_classification  
  6. from sklearn.model_selection import cross_val_score  
  7. from sklearn.model_selection import RepeatedStratifiedKFold  
  8. from sklearn.linear_model import Perceptron   
  9. # objective function  
  10. def objective(X, y, cfg):  
  11.  # unpack config  
  12.  eta, alpha = cfg  
  13.  # define model  
  14.  model = Perceptron(penalty='elasticnet'alphaalpha=alpha, etaeta0=eta)  
  15.  # define evaluation procedure  
  16.  cv = RepeatedStratifiedKFold(n_splits=10n_repeats=3random_state=1 
  17.  # evaluate model  
  18.  scores = cross_val_score(model, X, y, scoring='accuracy'cvcv=cv, n_jobs=-1)  
  19.  # calculate mean accuracy  
  20.  result = mean(scores)  
  21.  return result  
  22. # take a step in the search space  
  23. def step(cfg, step_size):  
  24.  # unpack the configuration 
  25.  eta, alpha = cfg  
  26.  # step eta  
  27.  new_eta = eta + randn() * step_size  
  28.  # check the bounds of eta  
  29.  if new_eta <= 0.0:  
  30.   new_eta = 1e-8  
  31.  # step alpha  
  32.  new_alpha = alpha + randn() * step_size  
  33.  # check the bounds of alpha  
  34.  if new_alpha < 0.0:  
  35.   new_alpha = 0.0  
  36.  # return the new configuration  
  37.  return [new_eta, new_alpha]   
  38. # hill climbing local search algorithm  
  39. def hillclimbing(X, y, objective, n_iter, step_size):  
  40.  # starting point for the search  
  41.  solution = [rand(), rand()] 
  42.  # evaluate the initial point  
  43.  solution_eval = objective(X, y, solution)  
  44.  # run the hill climb  
  45.  for i in range(n_iter):  
  46.   # take a step  
  47.   candidate = step(solution, step_size)  
  48.   # evaluate candidate point 
  49.    candidte_eval = objective(X, y, candidate)  
  50.   # check if we should keep the new point  
  51.   if candidte_eval >= solution_eval:  
  52.    # store the new point  
  53.    solution, solution_eval = candidate, candidte_eval  
  54.    # report progress  
  55.    print('>%d, cfg=%s %.5f' % (i, solution, solution_eval))  
  56.  return [solution, solution_eval]  
  57. # define dataset  
  58. X, y = make_classification(n_samples=1000n_features=5n_informative=2n_redundant=1random_state=1
  59.  # define the total iterations  
  60. n_iter = 100  
  61. # step size in the search space 
  62. step_size = 0.1  
  63. # perform the hill climbing search  
  64. cfg, score = hillclimbing(X, y, objective, n_iter, step_size)  
  65. print('Done!') 
  66. print('cfg=%s: Mean Accuracy: %f' % (cfg, score)) 

運行示例將在每次搜索過程中看到改進時報告配置和結(jié)果。運行結(jié)束時,將報告最佳配置和結(jié)果。

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

在這種情況下,我們可以看到,最好的結(jié)果涉及在1.004處使用略高于1的學習率和約0.002的正則化權(quán)重,從而獲得約79.1%的平均準確度,比默認配置好于約78.5%的準確度 。 

  1. >0, cfg=[0.5827274503894747, 0.260872709578015] 0.70533  
  2. >4, cfg=[0.5449820307807399, 0.3017271170801444] 0.70567  
  3. >6, cfg=[0.6286475606495414, 0.17499090243915086] 0.71933  
  4. >7, cfg=[0.5956196828965779, 0.0] 0.78633  
  5. >8, cfg=[0.5878361167354715, 0.0] 0.78633  
  6. >10, cfg=[0.6353507984485595, 0.0] 0.78633  
  7. >13, cfg=[0.5690530537610675, 0.0] 0.78633  
  8. >17, cfg=[0.6650936023999641, 0.0] 0.78633  
  9. >22, cfg=[0.9070451625704087, 0.0] 0.78633  
  10. >23, cfg=[0.9253366187387938, 0.0] 0.78633  
  11. >26, cfg=[0.9966143540220266, 0.0] 0.78633  
  12. >31, cfg=[1.0048613895650054, 0.002162219228449132] 0.79133  
  13. Done!  
  14. cfg=[1.0048613895650054, 0.002162219228449132]: Mean Accuracy: 0.791333 

既然我們已經(jīng)熟悉了如何使用隨機爬山算法來調(diào)整簡單的機器學習算法的超參數(shù),那么讓我們來看看如何調(diào)整更高級的算法,例如 XGBoost 。

XGBoost超參數(shù)優(yōu)化

XGBoost 是 Extreme Gradient Boosting 的縮寫,是隨機梯度提升機器學習算法的有效實現(xiàn)。隨機梯度增強算法(也稱為梯度增強機或樹增強)是一種功能強大的機器學習技術(shù),可在各種具有挑戰(zhàn)性的機器學習問題上表現(xiàn)出色,甚至表現(xiàn)最佳。首先,必須安裝XGBoost庫。您可以使用pip安裝它,如下所示: 

  1. sudo pip install xgboost 

一旦安裝,您可以通過運行以下代碼來確認它已成功安裝,并且您正在使用現(xiàn)代版本: 

  1. # xgboost  
  2. import xgboost  
  3. print("xgboost", xgboost.__version__) 

運行代碼,您應該看到以下版本號或更高版本 

  1. xgboost 1.0.1 

盡管XGBoost庫具有自己的 Python API,但我們可以通過 XGBClassifier 包裝器類將 XGBoost 模型與 scikit-learn API 結(jié)合使用??梢詫嵗P偷膶嵗拖駥⑵溆糜谀P驮u估的任何其他 scikit-learn 類一樣使用。例如: 

  1. # define model  
  2. model = XGBClassifier() 

在調(diào)整 XGBoost 的超參數(shù)之前,我們可以使用默認的超參數(shù)建立性能基準。我們將使用與上一節(jié)相同的合成二進制分類數(shù)據(jù)集,并使用重復分層k折交叉驗證的相同測試工具。下面列出了使用默認超參數(shù)評估 XGBoost 性能的完整示例。 

  1. # xgboost with default hyperparameters for binary classification  
  2. from numpy import mean  
  3. from numpy import std  
  4. from sklearn.datasets import make_classification  
  5. from sklearn.model_selection import cross_val_score  
  6. from sklearn.model_selection import RepeatedStratifiedKFold  
  7. from xgboost import XGBClassifier  
  8. # define dataset  
  9. X, y = make_classification(n_samples=1000n_features=5n_informative=2n_redundant=1random_state=1 
  10. # define model  
  11. model = XGBClassifier()  
  12. # define evaluation procedure  
  13. cv = RepeatedStratifiedKFold(n_splits=10n_repeats=3random_state=1 
  14. # evaluate model  
  15. scores = cross_val_score(model, X, y, scoring='accuracy'cvcv=cv, n_jobs=-1)  
  16. # report result  
  17. print('Mean Accuracy: %.3f (%.3f)' % (mean(scores), std(scores))) 

通過運行示例,可以評估模型并報告分類精度的平均值和標準偏差。

注意:由于算法或評估程序的隨機性,或者數(shù)值精度的差異,您的結(jié)果可能會有所不同??紤]運行該示例幾次并比較平均結(jié)果。在這種情況下,我們可以看到具有默認超參數(shù)的模型實現(xiàn)了約84.9%的分類精度。我們希望通過優(yōu)化的超參數(shù)可以實現(xiàn)比此更好的性能。 

  1. Mean Accuracy: 0.849 (0.040) 

接下來,我們可以采用隨機爬山優(yōu)化算法來調(diào)整 XGBoost 模型的超參數(shù)。我們可能要針對 XGBoost 模型優(yōu)化許多超參數(shù)。

有關(guān)如何調(diào)優(yōu) XGBoost 模型的概述,請參見教程:

如何配置梯度提升算法

https://machinelearningmastery.com/configure-gradient-boosting-algorithm/

我們將關(guān)注四個關(guān)鍵的超參數(shù)。他們是:

  •  學習率(learning_rate)
  •  樹數(shù)(n_estimators)
  •  子樣本百分比(子樣本)
  •  樹深(最大深度)

學習速度控制著每棵樹對整體的貢獻。明智的值應小于1.0,而應稍高于0.0(例如1e-8)。樹木的數(shù)量控制著合奏的大小,通常,越多的樹木越好,以至于收益遞減。合理的值在1棵樹與數(shù)百或數(shù)千棵樹之間。子樣本百分比定義用于訓練每棵樹的隨機樣本大小,定義為原始數(shù)據(jù)集大小的百分比。值介于略高于0.0(例如1e-8)和1.0的值之間樹的深度是每棵樹中的級別數(shù)。較深的樹更特定于訓練數(shù)據(jù)集,并且可能過度擬合。較短的樹通常可以更好地概括。明智的值是1到10或20之間。首先,我們必須更新Objective()函數(shù)以解包XGBoost模型的超參數(shù),對其進行配置,然后評估平均分類精度。 

  1. # objective function  
  2. def objective(X, y, cfg):  
  3.  # unpack config  
  4.  lrate, n_tree, subsam, depth = cfg  
  5.  # define model  
  6.  model = XGBClassifier(learning_rate=lraten_estimators=n_treesubsamsubsample=subsam, max_depth=depth)  
  7.  # define evaluation procedure  
  8.  cv = RepeatedStratifiedKFold(n_splits=10n_repeats=3random_state=1 
  9.  # evaluate model  
  10.  scores = cross_val_score(model, X, y, scoring='accuracy'cvcv=cv, n_jobs=-1)  
  11.  # calculate mean accuracy  
  12.  result = mean(scores)  
  13.  return result 

接下來,我們需要定義用于在搜索空間中邁出一步的step()函數(shù)。

每個超參數(shù)的范圍都非常不同,因此,我們將分別為每個超參數(shù)定義步長(分布的標準偏差)。為了使事情保持簡單,我們還將在線定義步長,而不是將其定義為函數(shù)的參數(shù)。

樹的數(shù)量和深度是整數(shù),因此步進值是四舍五入的。選定的步長是任意的,是在經(jīng)過反復試驗后選擇的。下面列出了更新的步進功能。 

  1. # take a step in the search space  
  2. def step(cfg):  
  3.  # unpack config  
  4.  lrate, n_tree, subsam, depth = cfg  
  5.  # learning rate  
  6.  lratelrate = lrate + randn() * 0.01  
  7.  if lrate <= 0.0: 
  8.   lrate = 1e-8  
  9.  if lrate > 1:  
  10.   lrate = 1.0  
  11.  # number of trees  
  12.  n_tree = round(n_tree + randn() * 50)  
  13.  if n_tree <= 0.0:  
  14.   n_tree = 1  
  15.  # subsample percentage  
  16.  subsamsubsam = subsam + randn() * 0.1  
  17.  if subsam <= 0.0:  
  18.   subsam = 1e-8  
  19.  if subsam > 1:  
  20.   subsam = 1.0  
  21.  # max tree depth  
  22.  depth = round(depth + randn() * 7)  
  23.  if depth <= 1: 
  24.   depth = 1  
  25.  # return new config  
  26.  return [lrate, n_tree, subsam, depth] 

最后,必須更新hillclimbing()算法,以定義具有適當值的初始解。在這種情況下,我們將使用合理的默認值,匹配默認的超參數(shù)或接近它們來定義初始解決方案。 

  1. # starting point for the search  
  2. solution = step([0.1, 100, 1.0, 7]) 

結(jié)合在一起,下面列出了使用隨機爬山算法手動調(diào)整 XGBoost 算法的超參數(shù)的完整示例。 

  1. # xgboost manual hyperparameter optimization for binary classification  
  2. from numpy import mean  
  3. from numpy.random import randn 
  4. from numpy.random import rand  
  5. from numpy.random import randint  
  6. from sklearn.datasets import make_classification  
  7. from sklearn.model_selection import cross_val_score  
  8. from sklearn.model_selection import RepeatedStratifiedKFold  
  9. from xgboost import XGBClassifier   
  10. # objective function  
  11. def objective(X, y, cfg): 
  12.  # unpack config  
  13.  lrate, n_tree, subsam, depth = cfg  
  14.  # define model  
  15.  model = XGBClassifier(learning_rate=lraten_estimators=n_treesubsamsubsample=subsam, max_depth=depth)  
  16.  # define evaluation procedure  
  17.  cv = RepeatedStratifiedKFold(n_splits=10n_repeats=3random_state=1 
  18.  # evaluate model 
  19.   scores = cross_val_score(model, X, y, scoring='accuracy'cvcv=cv, n_jobs=-1)  
  20.  # calculate mean accuracy  
  21.  result = mean(scores)  
  22.  return result  
  23. # take a step in the search space  
  24. def step(cfg):  
  25.  # unpack config  
  26.  lrate, n_tree, subsam, depth = cfg  
  27.  # learning rate  
  28.  lratelrate = lrate + randn() * 0.01  
  29.  if lrate <= 0.0:  
  30.   lrate = 1e-8  
  31.  if lrate > 1:  
  32.   lrate = 1.0  
  33.  # number of trees  
  34.  n_tree = round(n_tree + randn() * 50)  
  35.  if n_tree <= 0.0:  
  36.   n_tree = 1  
  37.  # subsample percentage  
  38.  subsamsubsam = subsam + randn() * 0.1  
  39.  if subsam <= 0.0:  
  40.   subsam = 1e-8  
  41.  if subsam > 1:  
  42.   subsam = 1.0  
  43.  # max tree depth  
  44.  depth = round(depth + randn() * 7)  
  45.  if depth <= 1:  
  46.   depth = 1  
  47.  # return new config  
  48.  return [lrate, n_tree, subsam, depth]  
  49. # hill climbing local search algorithm  
  50. def hillclimbing(X, y, objective, n_iter):  
  51.  # starting point for the search  
  52.  solution = step([0.1, 100, 1.0, 7])  
  53.  # evaluate the initial point  
  54.  solution_eval = objective(X, y, solution)  
  55.  # run the hill climb  
  56.  for i in range(n_iter):  
  57.   # take a step  
  58.   candidate = step(solution)  
  59.   # evaluate candidate point  
  60.   candidte_eval = objective(X, y, candidate)  
  61.   # check if we should keep the new point  
  62.   if candidte_eval >= solution_eval:  
  63.    # store the new point  
  64.    solution, solution_eval = candidate, candidte_eval  
  65.    # report progress  
  66.    print('>%d, cfg=[%s] %.5f' % (i, solution, solution_eval))  
  67.  return [solution, solution_eval]  
  68. # define dataset  
  69. X, y = make_classification(n_samples=1000n_features=5n_informative=2n_redundant=1random_state=1
  70. # define the total iterations  
  71. n_iter = 200  
  72. # perform the hill climbing search  
  73. cfg, score = hillclimbing(X, y, objective, n_iter)  
  74. print('Done!') 
  75. print('cfg=[%s]: Mean Accuracy: %f' % (cfg, score)) 

運行示例將在每次搜索過程中看到改進時報告配置和結(jié)果。運行結(jié)束時,將報告最佳配置和結(jié)果。

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

在這種情況下,我們可以看到最好的結(jié)果涉及使用大約0.02的學習率,52棵樹,大約50%的子采樣率以及53個級別的較大深度。此配置產(chǎn)生的平均準確度約為87.3%,優(yōu)于默認配置的平均準確度約為84.9%。 

  1. >0, cfg=[[0.1058242692126418, 67, 0.9228490731610172, 12]] 0.85933  
  2. >1, cfg=[[0.11060813799692253, 51, 0.859353656735739, 13]] 0.86100  
  3. >4, cfg=[[0.11890247679234153, 58, 0.7135275461723894, 12]] 0.86167  
  4. >5, cfg=[[0.10226257987735601, 61, 0.6086462443373852, 17]] 0.86400  
  5. >15, cfg=[[0.11176962034280596, 106, 0.5592742266405146, 13]] 0.86500  
  6. >19, cfg=[[0.09493587069112454, 153, 0.5049124222437619, 34]] 0.86533  
  7. >23, cfg=[[0.08516531024154426, 88, 0.5895201311518876, 31]] 0.86733  
  8. >46, cfg=[[0.10092590898175327, 32, 0.5982811365027455, 30]] 0.86867  
  9. >75, cfg=[[0.099469211050998, 20, 0.36372573610040404, 32]] 0.86900  
  10. >96, cfg=[[0.09021536590375884, 38, 0.4725379807796971, 20]] 0.86900  
  11. >100, cfg=[[0.08979482274655906, 65, 0.3697395430835758, 14]] 0.87000  
  12. >110, cfg=[[0.06792737273465625, 89, 0.33827505722318224, 17]] 0.87000  
  13. >118, cfg=[[0.05544969684589669, 72, 0.2989721608535262, 23]] 0.87200  
  14. >122, cfg=[[0.050102976159097, 128, 0.2043203965148931, 24]] 0.87200  
  15. >123, cfg=[[0.031493266763680444, 120, 0.2998819062922256, 30]] 0.87333  
  16. >128, cfg=[[0.023324201169625292, 84, 0.4017169945431015, 42]] 0.87333  
  17. >140, cfg=[[0.020224220443108752, 52, 0.5088096815056933, 53]] 0.87367  
  18. Done!  
  19. cfg=[[0.020224220443108752, 52, 0.5088096815056933, 53]]: Mean Accuracy: 0.873667  

 

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

2021-03-23 15:35:36

Adam優(yōu)化語言

2017-10-11 15:17:42

sklearn機器學習pandas

2020-12-17 10:00:16

Python協(xié)程線程

2021-01-29 11:25:57

Python爬山算法函數(shù)優(yōu)化

2021-03-12 09:45:00

Python關(guān)聯(lián)規(guī)則算法

2020-12-07 11:23:32

Scrapy爬蟲Python

2017-01-10 09:07:53

tcpdumpGET請求

2020-10-27 10:43:24

Redis字符串數(shù)據(jù)庫

2019-05-08 14:02:52

MySQL索引查詢優(yōu)化數(shù)據(jù)庫

2022-03-26 09:06:40

ActorCSP模型

2021-06-07 09:51:22

原型模式序列化

2009-11-17 14:50:50

Oracle調(diào)優(yōu)

2018-01-30 05:04:06

2021-04-19 23:29:44

MakefilemacOSLinux

2020-12-01 12:44:44

PythonHook鉤子函數(shù)

2020-11-24 11:50:52

Python文件代碼

2023-07-12 15:50:29

機器學習人工智能

2025-01-24 08:38:47

2020-03-06 10:45:48

機器學習人工智能神經(jīng)網(wǎng)絡(luò)

2012-06-28 10:26:51

Silverlight
點贊
收藏

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