機器學習實戰 | XGBoost建模應用詳解


作者:韓信子@ShowMeAI
教程地址https://www.showmeai.tech/tutorials/41
本文地址https://www.showmeai.tech/article-detail/204
聲明:版權所有,轉載請聯系平台與作者並注明出處
收藏ShowMeAI查看更多精彩內容


引言

XGBoost是eXtreme Gradient Boosting的縮寫稱呼,它是一個非常強大的Boosting算法工具包,優秀的性能(效果與速度)讓其在很長一段時間內霸屏數據科學比賽解決方案榜首,現在很多大廠的機器學習方案依舊會首選這個模型。XGBoost在並行計算效率、缺失值處理、控制過擬合、預測泛化能力上都變現非常優秀。

本篇內容ShowMeAI展開給大家講解XGBoost的工程應用方法,對於XGBoost原理知識感興趣的同學,歡迎參考ShowMeAI的另外一篇原理文章 圖解機器學習|XGBoost模型詳解

1.XGBoost安裝

XGBoost作為常見的強大Python機器學習工具庫,安裝也比較簡單。

1.1 Python與IDE環境設置

python環境與IDE設置可以參考ShowMeAI文章 圖解python | 安裝與環境設置 進行設置。

1.2 工具庫安裝

(1) Linux/Mac等系統

這些系統下的XGBoost安裝,大家只要基於pip就可以輕松完成了,在命令行端輸入命令如下命令即可等待安裝完成。

pip install xgboost

大家也可以選擇國內的pip源,以獲得更好的安裝速度

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xgboost

(2) Windows系統

對於windows系統而言,比較高效便捷的安裝方式是:在網址http://www.lfd.uci.edu/~gohlke/pythonlibs/ 中去下載對應版本的的XGBoost安裝包,再通過如下命令安裝。

pip install xgboost‑1.5.1‑cp310‑cp310‑win32.whl

2.XGBoost數據讀取

應用XGBoost的第一步,需要加載所需的數據成為工具庫所能支持的格式形態。XGBoost可以加載多種數據格式的數據用於訓練建模:

  • libsvm格式的文本數據。
  • Numpy的二維數組。
  • XGBoost的二進制的緩存文件。加載的數據存儲在對象DMatrix中。

XGBoost的SKLearn接口也支持對於Dataframe格式的數據(參考ShowMeAI的文章 Python數據分析|Pandas核心操作函數大全 進行更多了解)進行處理。

下面是不同格式的數據,XGBoost的加載方式。

  • 加載libsvm格式的數據
dtrain1 = xgb.DMatrix('train.svm.txt')
  • 加載二進制的緩存文件
dtrain2 = xgb.DMatrix('train.svm.buffer')
  • 加載numpy的數組
data = np.random.rand(5,10) # 5 entities, each contains 10 features
label = np.random.randint(2, size=5) # binary target
dtrain = xgb.DMatrix( data, label=label)
  • 將scipy.sparse格式的數據轉化為 DMatrix 格式
csr = scipy.sparse.csr_matrix( (dat, (row,col)) )
dtrain = xgb.DMatrix( csr ) 
  • 將DMatrix格式的數據保存成XGBoost的二進制格式,在下次加載時可以提高加載速度,使用方式如下
dtrain = xgb.DMatrix('train.svm.txt')
dtrain.save_binary("train.buffer")
  • 可以用如下方式處理DMatrix中的缺失值
dtrain = xgb.DMatrix( data, label=label, missing = -999.0)
  • 當需要給樣本設置權重時,可以用如下方式
w = np.random.rand(5,1)
dtrain = xgb.DMatrix( data, label=label, missing = -999.0, weight=w)

3.XGBoost不同建模方式

3.1 內置建模方式:libsvm格式數據源

XGBoost內置了建模方式,有如下的數據格式與核心訓練方法:

  • 基於DMatrix格式的數據。
  • 基於xgb.train接口訓練。

下面是官方的一個簡單示例,演示了讀取libsvm格式數據(成DMatrix格式)並指定參數建模的過程。

# 導入工具庫
import numpy as np
import scipy.sparse
import pickle
import xgboost as xgb

# 從libsvm文件中讀取數據,做二分類
# 數據是libsvm的格式,如下樣本格式
#1 3:1 10:1 11:1 21:1 30:1 34:1 36:1 40:1 41:1 53:1 58:1 65:1 69:1 77:1 86:1 88:1 92:1 95:1 102:1 105:1 117:1 124:1
#0 3:1 10:1 20:1 21:1 23:1 34:1 36:1 39:1 41:1 53:1 56:1 65:1 69:1 77:1 86:1 88:1 92:1 95:1 102:1 106:1 116:1 120:1
#0 1:1 10:1 19:1 21:1 24:1 34:1 36:1 39:1 42:1 53:1 56:1 65:1 69:1 77:1 86:1 88:1 92:1 95:1 102:1 106:1 116:1 122:1
dtrain = xgb.DMatrix('./data/agaricus.txt.train')
dtest = xgb.DMatrix('./data/agaricus.txt.test')

# 超參數設定
# 主要是樹深、學習率、目標函數
param = {'max_depth':2, 'eta':1, 'silent':1, 'objective':'binary:logistic' }

# 設定watchlist用於建模過程中觀測模型狀態
watchlist  = [(dtest,'eval'), (dtrain,'train')]
num_round = 2
bst = xgb.train(param, dtrain, num_round, watchlist)

# 使用模型預測
preds = bst.predict(dtest)

# 判斷准確率
labels = dtest.get_label()
print('錯誤率為%f' % \
       (sum(1 for i in range(len(preds)) if int(preds[i]>0.5)!=labels[i]) /float(len(preds))))

# 模型存儲
bst.save_model('./model/0001.model')

[0]  eval-error:0.042831  train-error:0.046522
[1]  eval-error:0.021726  train-error:0.022263
錯誤率為0.021726

3.2 內置建模方式:csv格式數據源

下面的例子,輸入的數據源是csv文件,我們使用大家熟悉的pandas工具庫(參考ShowMeAI教程 數據分析系列教程數據科學工具速查 | Pandas使用指南)把數據讀取為Dataframe格式,再構建Dmatrix格式輸入,后續使用內置建模方式進行訓練。

# 皮馬印第安人糖尿病數據集 包含很多字段:懷孕次數 口服葡萄糖耐量試驗中血漿葡萄糖濃度 舒張壓(mm Hg) 三頭肌組織褶厚度(mm) 
# 2小時血清胰島素(μU/ ml) 體重指數(kg/(身高(m)^2) 糖尿病系統功能 年齡(歲)
import pandas as pd
data = pd.read_csv('./data/Pima-Indians-Diabetes.csv')
data.head()

# 導入工具庫
import numpy as np
import pandas as pd
import pickle
import xgboost as xgb
from sklearn.model_selection import train_test_split

# 用pandas讀入數據
data = pd.read_csv('./data/Pima-Indians-Diabetes.csv')

# 做數據切分
train, test = train_test_split(data)

# 轉換成Dmatrix格式
feature_columns = ['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI', 'DiabetesPedigreeFunction', 'Age']
target_column = 'Outcome'

# 取出Dataframe的numpy數組值去初始化DMatrix對象
xgtrain = xgb.DMatrix(train[feature_columns].values, train[target_column].values)
xgtest = xgb.DMatrix(test[feature_columns].values, test[target_column].values)

#參數設定
param = {'max_depth':5, 'eta':0.1, 'silent':1, 'subsample':0.7, 'colsample_bytree':0.7, 'objective':'binary:logistic' }

# 設定watchlist用於查看模型狀態
watchlist  = [(xgtest,'eval'), (xgtrain,'train')]
num_round = 10
bst = xgb.train(param, xgtrain, num_round, watchlist)

# 使用模型預測
preds = bst.predict(xgtest)

# 判斷准確率
labels = xgtest.get_label()
print('錯誤類為%f' % \
       (sum(1 for i in range(len(preds)) if int(preds[i]>0.5)!=labels[i]) /float(len(preds))))

# 模型存儲
bst.save_model('./model/0002.model')

[0]  eval-error:0.354167  train-error:0.194444
[1]  eval-error:0.34375   train-error:0.170139
[2]  eval-error:0.322917  train-error:0.170139
[3]  eval-error:0.28125   train-error:0.161458
[4]  eval-error:0.302083  train-error:0.147569
[5]  eval-error:0.286458  train-error:0.138889
[6]  eval-error:0.296875  train-error:0.142361
[7]  eval-error:0.291667  train-error:0.144097
[8]  eval-error:0.302083  train-error:0.130208
[9]  eval-error:0.291667  train-error:0.130208
錯誤類為0.291667

3.3 預估器建模方式:SKLearn接口+Dataframe

XGBoost也支持用SKLearn中統一的預估器形態接口進行建模,如下為典型的參考案例,對於讀取為Dataframe格式的訓練集和測試集,可以直接使用XGBoost初始化XGBClassifier進行fit擬合訓練。使用方法與接口,和SKLearn中其他預估器一致。

# 導入工具庫
import numpy as np
import pandas as pd
import pickle
import xgboost as xgb
from sklearn.model_selection import train_test_split

# 用pandas讀入數據
data = pd.read_csv('./data/Pima-Indians-Diabetes.csv')

# 做數據切分
train, test = train_test_split(data)

# 特征列
feature_columns = ['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI', 'DiabetesPedigreeFunction', 'Age']
# 標簽列
target_column = 'Outcome'

# 初始化模型
xgb_classifier = xgb.XGBClassifier(n_estimators=20,\
                                   max_depth=4, \
                                   learning_rate=0.1, \
                                   subsample=0.7, \
                                   colsample_bytree=0.7, \
                                   eval_metric='error')

# Dataframe格式數據擬合模型
xgb_classifier.fit(train[feature_columns], train[target_column])

# 使用模型預測
preds = xgb_classifier.predict(test[feature_columns])

# 判斷准確率
print('錯誤類為%f' %((preds!=test[target_column]).sum()/float(test_y.shape[0])))

# 模型存儲
joblib.dump(xgb_classifier, './model/0003.model')

錯誤類為0.265625

['./model/0003.model']

4.模型調參與高級功能

4.1 XGBoost參數詳解

在運行XGBoost之前,必須設置三種類型成熟:general parameters,booster parameters和task parameters:

  • 通用參數:General parameters

    • 該參數控制在提升(boosting)過程中使用哪種booster,常用的booster有樹模型(tree)和線性模型(linear model)。
  • 提升器參數:Booster parameters

    • 這取決於使用哪種booster,包含樹模型booster和線性booster參數。
  • 任務參數:Task parameters

    • 控制學習的場景,例如在回歸問題中會使用不同的參數控制排序。

(1) 通用參數

  • booster [default=gbtree]

有兩種模型可以選擇gbtree和gblinear。gbtree使用基於樹的模型進行提升計算,gblinear使用線性模型進行提升計算。缺省值為gbtree

  • silent [default=0]

取0時表示打印出運行時信息,取1時表示以緘默方式運行,不打印運行時信息。缺省值為0

  • nthread

XGBoost運行時的線程數。缺省值是當前系統可以獲得的最大線程數

  • num_pbuffer

預測緩沖區大小,通常設置為訓練實例的數目。緩沖用於保存最后一步提升的預測結果,無需人為設置。

  • num_feature

Boosting過程中用到的特征維數,設置為特征個數。XGBoost會自動設置,無需人為設置。

(2) 樹模型booster參數

  • eta [default=0.3]

為了防止過擬合,更新過程中用到的收縮步長。在每次提升計算之后,算法會直接獲得新特征的權重。 eta通過縮減特征的權重使提升計算過程更加保守。缺省值為0.3 取值范圍為:[0,1]

  • gamma [default=0]

樹要進一步分裂生長所需的最小loss減小值. the larger, the more conservative the algorithm will be. 取值范圍為:[0,∞]

  • max_depth [default=6]

數的最大深度。缺省值為6 取值范圍為:[1,∞]

  • min_child_weight [default=1]

孩子節點中最小的樣本權重和。如果一個葉子節點的樣本權重和小於min_child_weight則拆分過程結束。在現行回歸模型中,這個參數是指建立每個模型所需要的最小樣本數。該成熟越大算法越conservative 取值范圍為:[0,∞]

  • max_delta_step [default=0]

我們允許每個樹的權重被估計的值。如果它的值被設置為0,意味着沒有約束;如果它被設置為一個正值,它能夠使得更新的步驟更加保守。通常這個參數是沒有必要的,但是如果在邏輯回歸中類極其不平衡這時候他有可能會起到幫助作用。把它范圍設置為1-10之間也許能控制更新。 取值范圍為:[0,∞]

  • subsample [default=1]

用於訓練模型的子樣本占整個樣本集合的比例。如果設置為0.5則意味着XGBoost將隨機的從整個樣本集合中隨機的抽取出50%的子樣本建立樹模型,這能夠防止過擬合。 取值范圍為:(0,1]

  • colsample_bytree [default=1]

在建立樹時對特征采樣的比例。缺省值為1 取值范圍為:(0,1]

(3) 線性Booster參數

  • lambda [default=0]

L2正則的懲罰系數

  • alpha [default=0]

L1正則的懲罰系數

  • lambda_bias

在偏置上的L2正則。缺省值為0(在L1上沒有偏置項的正則,因為L1時偏置不重要)

(4) 任務參數

  • objective [ default=reg:linear ]

    • 定義學習任務及相應的學習目標
    • 可選的目標函數如下:
      • reg:linear : 線性回歸。
      • reg:logistic: 邏輯回歸。
      • binary:logistic: 二分類的邏輯回歸問題,輸出為概率。
      • binary:logitraw: 二分類的邏輯回歸問題,輸出的結果為wTx。
      • count:poisson: 計數問題的poisson回歸,輸出結果為poisson分布。在poisson回歸中,max_delta_step的缺省值為0.7。(used to safeguard optimization)。
      • multi:softmax :讓XGBoost采用softmax目標函數處理多分類問題,同時需要設置參數num_class(類別個數)。
      • multi:softprob:和softmax一樣,但是輸出的是ndata * nclass的向量,可以將該向量reshape成ndata行nclass列的矩陣。沒行數據表示樣本所屬於每個類別的概率。
      • rank:pairwise:set XGBoost to do ranking task by minimizing the pairwise loss。
  • base_score [ default=0.5 ]

    • 所有實例的初始化預測分數,全局偏置;
    • 為了足夠的迭代次數,改變這個值將不會有太大的影響。
  • eval_metric [ default according to objective ]

    • 校驗數據所需要的評價指標,不同的目標函數將會有缺省的評價指標(rmse for regression, and error for classification, mean average precision for ranking)
    • 用戶可以添加多種評價指標,對於Python用戶要以list傳遞參數對給程序,而不是map參數list參數不會覆蓋`eval_metric’
    • 可供的選擇如下:
      • rmse:root mean square error
      • logloss:negative log-likelihood
      • error:Binary classification error rate. It is calculated as #(wrong cases)/#(all cases). For the predictions, the evaluation will regard the instances with prediction value larger than 0.5 as positive instances, and the others as negative instances.
      • merror:Multiclass classification error rate. It is calculated as #(wrongcases)#(allcases).
      • mlogloss:Multiclass logloss
      • auc:Area under the curve for ranking evaluation.
      • ndcg:Normalized Discounted Cumulative Gain
      • map:Mean average precision
      • ndcg@n,map@n:n can be assigned as an integer to cut off the top positions in the lists for evaluation.
      • ndcg-,map-,ndcg@n-,map@n-:In XGBoost, NDCG and MAP will evaluate the score of a list without any positive samples as 1. By adding - in the evaluation metric XGBoost will evaluate these score as 0 to be consistent under some conditions. training repeatively
  • seed [ default=0 ]

    • 隨機數的種子。缺省值為0

4.2 內置調參優化

(1) 交叉驗證

XGBoost自帶實驗與調參的一些方法,如下為交叉驗證方法xgb.cv

xgb.cv(param, dtrain, num_round, nfold=5,metrics={'error'}, seed = 0)

(2) 添加預處理

我們可以把數據建模過程中的一些設置加到交叉驗證環節里,比如對於不同類別的樣本加權,可以參考下列代碼示例

# 計算正負樣本比,調整樣本權重
def fpreproc(dtrain, dtest, param):
    label = dtrain.get_label()
    ratio = float(np.sum(label == 0)) / np.sum(label==1)
    param['scale_pos_weight'] = ratio
    return (dtrain, dtest, param)

# 先做預處理,計算樣本權重,再做交叉驗證
xgb.cv(param, dtrain, num_round, nfold=5,
       metrics={'auc'}, seed = 0, fpreproc = fpreproc)

(3) 自定義損失函數與評估准則

XGBoost支持在訓練過程中,自定義損失函數和評估准則,其中損失函數的定義需要返回損失函數一階和二階導數的計算方法,評估准則部分需要對數據的label和預估值進行計算。其中損失函數用於訓練過程中的樹結構學習,而評估准則很多時候是用在驗證集上進行效果評估。

print('使用自定義損失函數進行交叉驗證')
# 自定義損失函數,需要提供損失函數的一階導和二階導
def logregobj(preds, dtrain):
    labels = dtrain.get_label()
    preds = 1.0 / (1.0 + np.exp(-preds))
    grad = preds - labels
    hess = preds * (1.0-preds)
    return grad, hess

# 自定義評估准則,評估預估值和標准答案之間的差距
def evalerror(preds, dtrain):
    labels = dtrain.get_label()
    return 'error', float(sum(labels != (preds > 0.0))) / len(labels)

watchlist  = [(dtest,'eval'), (dtrain,'train')]
param = {'max_depth':3, 'eta':0.1, 'silent':1}
num_round = 5
# 自定義損失函數訓練
bst = xgb.train(param, dtrain, num_round, watchlist, logregobj, evalerror)
# 交叉驗證
xgb.cv(param, dtrain, num_round, nfold = 5, seed = 0, obj = logregobj, feval=evalerror)

使用自定義損失函數進行交叉驗證
[0]  eval-rmse:0.306901   train-rmse:0.306164  eval-error:0.518312  train-error:0.517887
[1]  eval-rmse:0.179189   train-rmse:0.177278  eval-error:0.518312  train-error:0.517887
[2]  eval-rmse:0.172565   train-rmse:0.171728  eval-error:0.016139  train-error:0.014433
[3]  eval-rmse:0.269612   train-rmse:0.27111   eval-error:0.016139  train-error:0.014433
[4]  eval-rmse:0.396903   train-rmse:0.398256  eval-error:0.016139  train-error:0.014433

(4) 只用前n顆樹預測

對於boosting模型來說,最后會訓練得到很多基學習器(在XGBoost中很多時候是很多棵樹),我們可以一次完整訓練,只用前n棵樹的集成來完成預測。

#!/usr/bin/python
import numpy as np
import pandas as pd
import pickle
import xgboost as xgb
from sklearn.model_selection import train_test_split

# 基本例子,從csv文件中讀取數據,做二分類

# 用pandas讀入數據
data = pd.read_csv('./data/Pima-Indians-Diabetes.csv')

# 做數據切分
train, test = train_test_split(data)

# 轉換成Dmatrix格式
feature_columns = ['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI', 'DiabetesPedigreeFunction', 'Age']
target_column = 'Outcome'
xgtrain = xgb.DMatrix(train[feature_columns].values, train[target_column].values)
xgtest = xgb.DMatrix(test[feature_columns].values, test[target_column].values)

#參數設定
param = {'max_depth':5, 'eta':0.1, 'silent':1, 'subsample':0.7, 'colsample_bytree':0.7, 'objective':'binary:logistic' }

# 設定watchlist用於查看模型狀態
watchlist  = [(xgtest,'eval'), (xgtrain,'train')]
num_round = 10
bst = xgb.train(param, xgtrain, num_round, watchlist)

# 只用第1顆樹預測
ypred1 = bst.predict(xgtest, ntree_limit=1)
# 用前9顆樹預測
ypred2 = bst.predict(xgtest, ntree_limit=9)
label = xgtest.get_label()
print('用前1顆樹預測的錯誤率為 %f' % (np.sum((ypred1>0.5)!=label) /float(len(label))))
print('用前9顆樹預測的錯誤率為 %f' % (np.sum((ypred2>0.5)!=label) /float(len(label))))

[0]  eval-error:0.255208  train-error:0.196181
[1]  eval-error:0.234375  train-error:0.175347
[2]  eval-error:0.25   train-error:0.163194
[3]  eval-error:0.229167  train-error:0.149306
[4]  eval-error:0.213542  train-error:0.154514
[5]  eval-error:0.21875   train-error:0.152778
[6]  eval-error:0.21875   train-error:0.154514
[7]  eval-error:0.213542  train-error:0.138889
[8]  eval-error:0.1875 train-error:0.147569
[9]  eval-error:0.1875 train-error:0.144097
用前1顆樹預測的錯誤率為 0.255208
用前9顆樹預測的錯誤率為 0.187500

4.3 預估器調參優化

(1) SKLearn形態接口實驗評估

XGBoost有SKLearn預估器形態的接口,整體使用方法和SKLearn中其他預估器一致,如下是手動對數據做交叉驗證,注意到這里直接使用XGBClassifier對Dataframe數據進行fit擬合和評估。

import pickle
import xgboost as xgb

import numpy as np
from sklearn.model_selection import KFold, train_test_split, GridSearchCV
from sklearn.metrics import confusion_matrix, mean_squared_error
from sklearn.datasets import load_iris, load_digits, load_boston

rng = np.random.RandomState(31337)

# 二分類:混淆矩陣
print("數字0和1的二分類問題")
digits = load_digits(2)
y = digits['target']
X = digits['data']
# 數據切分對象
kf = KFold(n_splits=2, shuffle=True, random_state=rng)
print("在2折數據上的交叉驗證")
# 2折交叉驗證
for train_index, test_index in kf.split(X):
    xgb_model = xgb.XGBClassifier().fit(X[train_index],y[train_index])
    predictions = xgb_model.predict(X[test_index])
    actuals = y[test_index]
    print("混淆矩陣:")
    print(confusion_matrix(actuals, predictions))

#多分類:混淆矩陣
print("\nIris: 多分類")
iris = load_iris()
y = iris['target']
X = iris['data']
kf = KFold(n_splits=2, shuffle=True, random_state=rng)
print("在2折數據上的交叉驗證")
for train_index, test_index in kf.split(X):
    xgb_model = xgb.XGBClassifier().fit(X[train_index],y[train_index])
    predictions = xgb_model.predict(X[test_index])
    actuals = y[test_index]
    print("混淆矩陣:")
    print(confusion_matrix(actuals, predictions))

#回歸問題:MSE
print("\n波士頓房價回歸預測問題")
boston = load_boston()
y = boston['target']
X = boston['data']
kf = KFold(n_splits=2, shuffle=True, random_state=rng)
print("在2折數據上的交叉驗證")
for train_index, test_index in kf.split(X):
    xgb_model = xgb.XGBRegressor().fit(X[train_index],y[train_index])
    predictions = xgb_model.predict(X[test_index])
    actuals = y[test_index]
    print("MSE:",mean_squared_error(actuals, predictions))
數字0和1的二分類問題
在2折數據上的交叉驗證
混淆矩陣:
[[87  0]
 [ 1 92]]
混淆矩陣:
[[91  0]
 [ 3 86]]

Iris: 多分類
在2折數據上的交叉驗證
混淆矩陣:
[[19  0  0]
 [ 0 31  3]
 [ 0  1 21]]
混淆矩陣:
[[31  0  0]
 [ 0 16  0]
 [ 0  3 25]]

波士頓房價回歸預測問題
在2折數據上的交叉驗證
MSE: 9.860776812557337
MSE: 15.942418468446029

(2) 網格搜索調參

上面提到XGBoost的預估器接口,整體使用方法和SKLearn中其他預估器一致,所以我們也可以使用SKLearn中的超參數調優方法來進行模型調優。

如下是一個典型的網格搜索交法調優超參數的代碼示例,我們會給出候選參數列表字典,通過GridSearchCV進行交叉驗證實驗評估,選出XGBoost在候選參數中最優的超參數。
print("參數最優化:")

y = boston['target']
X = boston['data']
xgb_model = xgb.XGBRegressor()
clf = GridSearchCV(xgb_model,
                   {'max_depth': [2,4,6],
                    'n_estimators': [50,100,200]}, verbose=1)
clf.fit(X,y)
print(clf.best_score_)
print(clf.best_params_)

參數最優化:
Fitting 3 folds for each of 9 candidates, totalling 27 fits

[Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers.

0.6001029721598573
{'max_depth': 4, 'n_estimators': 100}

[Parallel(n_jobs=1)]: Done  27 out of  27 | elapsed:    1.3s finished

(3) early-stopping早停

XGBoost模型有時候會因為不停疊加新的樹(修正訓練集上擬合尚不正確的一些樣本),可能會因為對於訓練集過度學習而導致模型過擬合。early stopping早停止是一個有效的策略,具體的做法是,在訓練集不斷追加樹學習的過程中,對驗證集上的表現進行監控,如果出現一定輪次評估准則都沒有優化提升的情況,則回溯到歷史上驗證集最好的點,保存為最佳模型。

下面是對應的代碼示例,其中參數early_stopping_rounds設定了驗證集上能接受的效果不提升的最多輪次數,eval_set指定了驗證數據集。

# 在訓練集上學習模型,一顆一顆樹添加,在驗證集上看效果,當驗證集效果不再提升,停止樹的添加與生長
X = digits['data']
y = digits['target']
X_train, X_val, y_train, y_val = train_test_split(X, y, random_state=0)
clf = xgb.XGBClassifier()
clf.fit(X_train, y_train, early_stopping_rounds=10, eval_metric="auc",
        eval_set=[(X_val, y_val)])

[0]  validation_0-auc:0.999497
Will train until validation_0-auc hasn't improved in 10 rounds.
[1]  validation_0-auc:0.999497
[2]  validation_0-auc:0.999497
[3]  validation_0-auc:0.999749
[4]  validation_0-auc:0.999749
[5]  validation_0-auc:0.999749
[6]  validation_0-auc:0.999749
[7]  validation_0-auc:0.999749
[8]  validation_0-auc:0.999749
[9]  validation_0-auc:0.999749
[10] validation_0-auc:1
[11] validation_0-auc:1
[12] validation_0-auc:1
[13] validation_0-auc:1
[14] validation_0-auc:1
[15] validation_0-auc:1
[16] validation_0-auc:1
[17] validation_0-auc:1
[18] validation_0-auc:1
[19] validation_0-auc:1
[20] validation_0-auc:1
Stopping. Best iteration:
[10] validation_0-auc:1


XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,
       colsample_bytree=1, gamma=0, learning_rate=0.1, max_delta_step=0,
       max_depth=3, min_child_weight=1, missing=None, n_estimators=100,
       n_jobs=1, nthread=None, objective='binary:logistic', random_state=0,
       reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=None,
       silent=True, subsample=1)

(4) 特征重要度

XGBoost建模過程中,還可以學習到對應的特征重要度信息,並保存在模型的feature_importances_屬性中。如下為繪制特征重要度的可視化代碼:

iris = load_iris()
y = iris['target']
X = iris['data']
xgb_model = xgb.XGBClassifier().fit(X,y)

print('特征排序:')
feature_names=['sepal_length', 'sepal_width', 'petal_length', 'petal_width']
feature_importances = xgb_model.feature_importances_
indices = np.argsort(feature_importances)[::-1]

for index in indices:
    print("特征 %s 重要度為 %f" %(feature_names[index], feature_importances[index]))

%matplotlib inline
import matplotlib.pyplot as plt
plt.figure(figsize=(16,8))
plt.title("feature importances")
plt.bar(range(len(feature_importances)), feature_importances[indices], color='b')
plt.xticks(range(len(feature_importances)), np.array(feature_names)[indices], color='b')

特征排序:
特征 petal_length 重要度為 0.415567
特征 petal_width 重要度為 0.291557
特征 sepal_length 重要度為 0.179420
特征 sepal_width 重要度為 0.113456

(5) 並行訓練加速

在多資源的情況下,XGBoost可以實現並行訓練加速,示例代碼如下:

import os

if __name__ == "__main__":
    try:
        from multiprocessing import set_start_method
    except ImportError:
        raise ImportError("Unable to import multiprocessing.set_start_method."
                          " This example only runs on Python 3.4")
    #set_start_method("forkserver")

    import numpy as np
    from sklearn.model_selection import GridSearchCV
    from sklearn.datasets import load_boston
    import xgboost as xgb

    rng = np.random.RandomState(31337)

    print("Parallel Parameter optimization")
    boston = load_boston()

    os.environ["OMP_NUM_THREADS"] = "2"  # or to whatever you want
    y = boston['target']
    X = boston['data']
    xgb_model = xgb.XGBRegressor()
    clf = GridSearchCV(xgb_model, {'max_depth': [2, 4, 6],
                                   'n_estimators': [50, 100, 200]}, verbose=1,
                       n_jobs=2)
    clf.fit(X, y)
    print(clf.best_score_)
    print(clf.best_params_)
Parallel Parameter optimization
Fitting 3 folds for each of 9 candidates, totalling 27 fits

[Parallel(n_jobs=2)]: Using backend LokyBackend with 2 concurrent workers.
[Parallel(n_jobs=2)]: Done  24 out of  27 | elapsed:    2.2s remaining:    0.3s

0.6001029721598573
{'max_depth': 4, 'n_estimators': 100}

[Parallel(n_jobs=2)]: Done  27 out of  27 | elapsed:    2.4s finished

參考資料

機器學習【算法】系列教程

機器學習【實戰】系列教程

ShowMeAI系列教程推薦


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM