xgboost調參實踐

xgboost調參筆記

complete-guide-parameter-tuning-xgboost

github code: Parameter_Tuning_XGBoost_with_Example

中文翻譯

以二分類爲例,主要思路就是用網格搜索做參數調優。

數據源:

如果找不到合適的二分類數據,可以用from sklearn.datasets import make_hastie_10_2數據集。

特徵工程

  • 缺失值使用中位數填充,

    data.apply(lambda x: sum(x.isnull())),
    data[‘Amount_Applied’].fillna(data[‘Amount_Applied’].median(),inplace=True)

  • 刪除City這種取值過多的特徵。查看len(data[‘City’].unique())
  • 可以對數值型特徵離散化,pd.cut分箱分桶。
  • 對類別特徵Device_Type、Gender等用LabelEncoder處理後最後用get_dummies做one-hot編碼。
  • 其他特徵轉化、drop、組合等處理。

分步調節最優參數

step1: 加載數據,設置初始參數

  1. max_depth = 5 :默認6樹的最大深度,這個參數的取值最好在3-10之間。
  2. min_child_weight = 1 :默認是1決定最小葉子節點樣本權重和。如果是一個極不平衡的分類問題,某些葉子節點下的值會比較小,這個值取小點。
  3. gamma = 0 :默認0,在0.1到0.2之間就可以。樹的葉子節點上作進一步分裂所需的最小損失減少。這個參數後繼也是要調整的。
  4. subsample, colsample_bytree = 0.8 :樣本採樣、列採樣。典型值的範圍在0.5-0.9之間。
  5. scale_pos_weight = 1 :默認1,如果類別十分不平衡取較大正值。

step2: 網格搜索

引用庫

import xgboost as xgb
from xgboost.sklearn import XGBClassifier
from sklearn.model_selection import cross_validate,GridSearchCV
from sklearn import metrics

固定其他已調好的參數,驗證param_testN中的best_params.

param_test3 = {
    'subsample':[i/10.0 for i in range(6,10)],
    'colsample_bytree':[i/10.0 for i in range(6,10)]
}
gsearch3 = GridSearchCV(estimator = XGBClassifier( learning_rate =0.1, n_estimators=150, max_depth=5,
                                        min_child_weight=10, gamma=0, subsample=0.8, colsample_bytree=0.8,
                                        objective= 'binary:logistic', nthread=4, scale_pos_weight=1,seed=27), 
                       param_grid = param_test3, scoring='roc_auc',n_jobs=4,iid=False, cv=5)
gsearch3.fit(train[predictors], train[target])
gsearch3.cv_results_, gsearch3.best_params_, gsearch3.best_score_

從輸出結果看當前最佳值

  ......
  
  'mean_test_score': array([0.83749862, 0.83905418, 0.83884314, 0.83902026, 0.83814523,
         0.83788037, 0.83866718, 0.84048344, 0.83756248, 0.83808899,
         0.84111194, 0.83955118, 0.83635295, 0.83623314, 0.83893753,
         0.83788233]),
  'std_test_score': array([0.00867607, 0.00580994, 0.0078614 , 0.00751303, 0.00582818,
         0.00723515, 0.00681313, 0.00620886, 0.00815349, 0.00798908,
         0.00638452, 0.00579401, 0.00875388, 0.00561627, 0.00631244,
         0.00675911]),
  'rank_test_score': array([14,  4,  7,  5,  9, 12,  8,  2, 13, 10,  1,  3, 15, 16,  6, 11])},
 {'colsample_bytree': 0.8, 'subsample': 0.8},
 0.8411119400279029)

結果表明:

colsample_bytree=0.8 subsample=0.8 最好

step3: 繼續修改其他參數,依次得出最佳值


主要參數調整過程

  1. n_estimators (int) – Number of gradient boosted trees. Equivalent to number of boosting rounds. 總共迭代的次數,亦即決策樹的個數。
  2. max_depth 和 min_child_weight 對最終結果有較大的影響。
  • max_depth 樹的最大深度,值越大,樹越複雜。這個可以用來控制過擬合,典型值是3-10,默認是6。
  • min_child_weight 決定最小葉子節點樣本權重和。用於避免過擬合。當它的值較大時,可以避免模型學習到局部的特殊樣本,但是如果值過高,會導致欠擬合。
  1. gamma (default=0, alias: min_split_loss) Minimum loss reduction required to make a further partition on a leaf node of the tree.在節點分裂時,只有分裂後損失函數的值下降了,纔會分裂這個節點。Gamma指定了一個結點被分割時,所需要的最小損失函數減小的大小。
  2. subsample 和 colsample_bytree。Subsample是樣本的採樣率,如果設置成0.5,那麼Xgboost會隨機選擇一般的樣本作爲訓練集。colsample_bytree表示構造每棵樹時,列採樣率(一般是feature採樣率)
  3. 正則化參數
  • reg_alpha 權重的L1正則化項。可以用在很高維度的情況下,使得算法的速度更快。
  • reg_lambda 權重的L2正則化項。這個其實用的很少。
  1. 進一步 降低學習速率 並增加更多的樹。調節learning_rate和n_estimators。

參數調整後記錄結果,auc_Score.append(metrics.roc_auc_score(y_test, y_pro)),畫圖分析是否逐步提升。

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