XGBoost筆記

XGBoost筆記


本博客參考一下信息:
http://blog.csdn.net/heroacool/article/details/53668923
http://blog.csdn.net/chuanda112233/article/details/51913254
http://blog.csdn.net/zc02051126/article/details/46711047

1. xgboost原理

  • 決策樹
  • AdaBoost和加法模型
  • Boosting tree

2. XGBoost使用

# plot decision tree
from numpy import loadtxt
from xgboost import XGBClassifier
from xgboost import plot_tree
import matplotlib.pyplot as plt
from graphviz import Digraph
import pydot

# print pydot.find_graphviz()

dataset = loadtxt('pima-indians-diabetes.csv', delimiter=",")
# split data into X and y
X = dataset[:, 0:8]
y = dataset[:, 8]
# fit model no training data
model = XGBClassifier()
model.fit(X, y)
# plot single tree
plot_tree(model)
plt.show()

3. XGBoost參數調優

XGBoost的參數可以分爲三種類型:通用參數、booster參數以及學習目標參數

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

  • Booster parameters:這取決於使用哪種booster。

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

    除了以上參數還可能有其它參數,在命令行中使用

3.1 General Parameters

  • booster [default=gbtree]
    有兩中模型可以選擇gbtree和gblinear。gbtree使用基於樹的模型進行提升計算,gblinear使用線性模型進行提升計算。缺省值爲gbtree
  • silent [default=0]
    取0時表示打印出運行時信息,取1時表示以緘默方式運行,不打印運行時信息。缺省值爲0
    建議取0,過程中的輸出數據有助於理解模型以及調參。另外實際上我設置其爲1也通常無法緘默運行。。
  • nthread [default to maximum number of threads available if not set]
    XGBoost運行時的線程數。缺省值是當前系統可以獲得的最大線程數
    如果你希望以最大速度運行,建議不設置這個參數,模型將自動獲得最大線程
  • num_pbuffer [set automatically by xgboost, no need to be set by user]
    size of prediction buffer, normally set to number of training instances. The buffers are used to save the prediction results of last boosting step.
  • num_feature [set automatically by xgboost, no need to be set by user]
    boosting過程中用到的特徵維數,設置爲特徵個數。XGBoost會自動設置,不需要手工設置

Booster Parameters

From xgboost-unity, the bst: prefix is no longer needed for booster parameters. Parameter with or without bst: prefix will be equivalent(i.e. both bst:eta and eta will be valid parameter setting) .
Parameter for Tree Booster

eta [default=0.3] 
    爲了防止過擬合,更新過程中用到的收縮步長。在每次提升計算之後,算法會直接獲得新特徵的權重。 eta通過縮減特徵的權重使提升計算過程更加保守。缺省值爲0.3
    取值範圍爲:[0,1]
    通常最後設置eta爲0.01~0.2
gamma [default=0] 
    minimum loss reduction required to make a further partition on a leaf node of the tree. the larger, the more conservative the algorithm will be.
    range: [0,∞]
    模型在默認情況下,對於一個節點的劃分只有在其loss function 得到結果大於0的情況下才進行,而gamma 給定了所需的最低loss function的值
    gamma值是的算法更conservation,且其值依賴於loss function ,在模型中應該進行調參。
max_depth [default=6] 
    數的最大深度。缺省值爲6
    取值範圍爲:[1,∞]
    指樹的最大深度
    樹的深度越大,則對數據的擬合程度越高(過擬合程度也越高)。即該參數也是控制過擬合
    建議通過交叉驗證(xgb.cv ) 進行調參
    通常取值:3-10
min_child_weight [default=1] 
    孩子節點中最小的樣本權重和。如果一個葉子節點的樣本權重和小於min_child_weight則拆分過程結束。在現行迴歸模型中,這個參數是指建立每個模型所需要的最小樣本數。該成熟越大算法越conservative。即調大這個參數能夠控制過擬合。
    取值範圍爲: [0,∞]
max_delta_step [default=0] 
    Maximum delta step we allow each tree’s weight estimation to be. If the value is set to 0, it means there is no constraint. If it is set to a positive value, it can help making the update step more conservative. Usually this parameter is not needed, but it might help in logistic regression when class is extremely imbalanced. Set it to value of 1-10 might help control the update
    取值範圍爲:[0,∞]
    如果取值爲0,那麼意味着無限制。如果取爲正數,則其使得xgboost更新過程更加保守。
    通常不需要設置這個值,但在使用logistics 迴歸時,若類別極度不平衡,則調整該參數可能有效果
subsample [default=1] 
    用於訓練模型的子樣本佔整個樣本集合的比例。如果設置爲0.5則意味着XGBoost將隨機的衝整個樣本集合中隨機的抽取出50%的子樣本建立樹模型,這能夠防止過擬合。
    取值範圍爲:(0,1]
colsample_bytree [default=1] 
    在建立樹時對特徵隨機採樣的比例。缺省值爲1
    取值範圍:(0,1]
colsample_bylevel[default=1]
    決定每次節點劃分時子樣例的比例
    通常不使用,因爲subsample和colsample_bytree已經可以起到相同的作用了
scale_pos_weight[default=0]
    A value greater than 0 can be used in case of high class imbalance as it helps in faster convergence.
    大於0的取值可以處理類別不平衡的情況。幫助模型更快收斂

Parameter for Linear Booster

lambda [default=0] 
    L2 正則的懲罰係數
    用於處理XGBoost的正則化部分。通常不使用,但可以用來降低過擬合
alpha [default=0] 
    L1 正則的懲罰係數
    當數據維度極高時可以使用,使得算法運行更快。
lambda_bias 
    在偏置上的L2正則。缺省值爲0(在L1上沒有偏置項的正則,因爲L1時偏置不重要)

Task Parameters

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 ] 
    the initial prediction score of all instances, global bias
eval_metric [ default according to objective ] 
    校驗數據所需要的評價指標,不同的目標函數將會有缺省的評價指標(rmse for regression, and error for classification, mean average precision for ranking)
    用戶可以添加多種評價指標,對於Python用戶要以list傳遞參數對給程序,而不是map參數list參數不會覆蓋’eval_metric’
    The choices are listed below:
    “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 #(wrong cases)/#(all cases).
    “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
    可以用於產生可重複的結果(每次取一樣的seed即可得到相同的隨機劃分)

Console Parameters

The following parameters are only used in the console version of xgboost
* use_buffer [ default=1 ]
- 是否爲輸入創建二進制的緩存文件,緩存文件可以加速計算。缺省值爲1
* num_round
- boosting迭代計算次數。
* data
- 輸入數據的路徑
* test:data
- 測試數據的路徑
* save_period [default=0]
- 表示保存第i*save_period次迭代的模型。例如save_period=10表示每隔10迭代計算XGBoost將會保存中間結果,設置爲0表示每次計算的模型都要保持。
* task [default=train] options: train, pred, eval, dump
- train:訓練明顯
- pred:對測試數據進行預測
- eval:通過eval[name]=filenam定義評價指標
- dump:將學習模型保存成文本格式
* model_in [default=NULL]
- 指向模型的路徑在test, eval, dump都會用到,如果在training中定義XGBoost將會接着輸入模型繼續訓練
* model_out [default=NULL]
- 訓練完成後模型的保持路徑,如果沒有定義則會輸出類似0003.model這樣的結果,0003是第三次訓練的模型結果。
* model_dir [default=models]
- 輸出模型所保存的路徑。
* fmap
- feature map, used for dump model
* name_dump [default=dump.txt]
- name of model dump file
* name_pred [default=pred.txt]
- 預測結果文件
* pred_margin [default=0]
- 輸出預測的邊界,而不是轉換後的概率

如果你比較習慣scikit-learn的參數形式,那麼XGBoost的Python 版本也提供了sklearn形式的接口 XGBClassifier。它使用sklearn形式的參數命名方式,對應關係如下:

eta –> learning_rate
lambda –> reg_lambda
alpha –> reg_alpha
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章