xgboost的sklearn接口和原生接口參數詳細說明及調參指點


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

1.模型參數

max_depth:int |每個基本學習器樹的最大深度,可以用來控制過擬合。典型值是3-10

learning_rate=0.1:

  即是eta,為了防止過擬合,更新過程中用到的收縮步長,使得模型更加健壯。每次提升計算之后,算法會直接獲得新特征的權重,eta通過縮減特征的權重使提升計算過程更加保守,缺省值為0.3 取值范圍為:[0,1]。典型值一般設置為:0.01-0.2。

n_estimators=100,估計器的數量

silent:boolean|是否打印信息

objective:定義學習任務及相應的學習目標,可選目標函數如下:

  “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

booster: default="gbtree",

  可選gbtree和gblinear,gbtree使用基於樹的模型進行提升計算,gblinear使用線性模型進行提升計算

n_jobs:線程數目

nthread:廢棄

gamma:0,損失閾值,在樹的一個葉節點上進行進一步分裂所需的最小損失減少量,越大,算法越保守。取值范圍為:[0,∞]。在節點分裂時,只有分裂后損失函數的值下降了,才會分裂這個節點。Gamma指定了節點分裂所需的最小損失函數下降值。這個參數的值越大,算法越保守。這個參數的值和損失函數息息相關,所以是需要調整的。

 

min_child_weight=1,

  拆分節點權重和閾值,如果節點的樣本權重和小於該閾值,就不再進行拆分。在現行回歸模型中,這個是指建立每個模型所需要的最小樣本數。越大,算法越保守,可以用來減少過擬合。 取值范圍為:[0,∞]

max_delta_step=0,

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

subsample=1,   

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

colsample_bytree=1,

  選取構造樹的特征比例。缺省值為1 取值范圍為:(0,1] 。典型值0.5-1

colsample_bylevel=1,

   Subsample ratio of columns for each split, in each level. 每個層分裂的節點數,這個一般很少用。用來控制樹的每一級的每一次分裂,對列數的采樣的占比。

 

reg_alpha=0,

  L1 regularization term on weights,這個主要是用在數據維度很高的情況下,可以提高運行速度。

reg_lambda=1,

  L2 regularization term on weights,這個其實用的很少

scale_pos_weight=1,

  用來控制正負樣本的比例,平衡正負樣本權重,處理樣本不平衡。在類別高度不平衡的情況下,將參數設置大於0,可以加快收斂。

 

base_score=0.5,

   The initial prediction score of all instances, global bias.

random_state=0,     

seed=None,廢棄

missing=None,

  在數據中,標注為缺失值的表示。如果為None,則默認為np.nan

**kwargs:

 

  tree_method: string,[default=’auto’],xgboost構建樹的算法,‘auto’,‘exact’,‘approx’,‘hist’  

  lambda_bias: 在偏置上的L2正則  

  sketch_eps: [default=0.03],只在approximate greedy algorithm上使用  

  updater: [default=’grow_colmaker,prune’],提供模塊化的方式來構建樹,一般不需要由用戶設置

  refresh_leaf: [default=1],刷新參數,如果為1,刷新葉子和樹節點,否則只刷新樹節點

  process_type: [default=’default’],提升的方式  

  grow_policy: string [default=’depthwise’],控制新增節點的方式,‘depthwise’,分裂離根節點最近的節點,‘lossguide’,分裂損失函數變化最大的節點

  max_leaves: [default=0],增加的最大節點數,只和lossguide’ grow policy相關

  max_bins: [default=256],只和tree_method的‘hist’相關

調參關鍵參數:

 xgboost的調參建議采用單參數調整的方法

 

過擬合控制參數:

直接控制模型的復雜度 

max_depth, min_child_weight, gamma

增大產生樹的隨機性 

subsample, colsample_bytree

eta, num_round

 

 

 

 

 

處理不平衡的數據集 :

預測的排序(AUC) 

scale_pos_weight

預測可靠性 

max_delta_step

 

 

 

 

判斷過擬合的一般方法:

clf = xgb.XGBClassifier()
clf.fit(X_train,y_train,eval_set
=[(X_train, y_train),(X_test, y_test)],eval_metric='logloss', verbose=True)

#
'validation_0':(X_train, y_train),'validation_1':(X_test, y_test)

evals_result = clf.evals_result()
#{'validation_0': {'logloss': ['0.604835', '0.531479']}, # 'validation_1': {'logloss': ['0.41965', '0.17686']}}

 xgboost如何判斷特征重要性:

weight  - 該特征在所有樹中被用作分割樣本的特征的次數;

gain      - 在所有樹中的平均增益;

cover    - the average coverage of the feature when it is used in trees。

 https://blog.csdn.net/lz_peter/article/details/85010931

2.方法列表

fit(X,y,sample_weight=None,eval_set=None,eval_metric=None,early_stopping_rounds=None,verbose=True, xgb_model=None, sample_weight_eval_set=None, callbacks=None)

sample_weight:每個樣本的權重,設置方法:sample_weight=np.where(y==1,len(y)-sum(y),sum(y))

eval_set=None,A list of (X, y) 

  用作提早停止的驗證集

eval_metric=None,  

  [默認和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,二值誤差率,計算方法為誤分樣本/總樣本
  “merror”: Multiclass classification error rate,多分類誤差率,計算方法同上
  “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.  

early_stopping_rounds=None,intoptional  

verbose=True,可視化

xgb_model=None,str

  file name of stored xgb model or ‘Booster’ instance Xgb model to be loaded before training (allows training continuation).

 

sample_weight_eval_set=None,

callbacks=None,

predict(data, output_margin=False, ntree_limit=None, validate_features=True)

 ntree_limit :int

  Limit number of trees in the prediction; defaults to best_ntree_limit if defined (i.e. it has been trained with early stopping), otherwise 0 (use all trees). 

 

predict_proba(data, ntree_limit=None, validate_features=True)

 

apply(X, ntree_limit=0)

Return the predicted leaf every tree for each sample.

array_like, shape=[n_samples, n_trees]

evals_result()

Return the evaluation results.

  If eval_set is passed to the fit function, you can call evals_result() to get evaluation results for all passed eval_sets. When eval_metric is also passed to the fit function, the evals_result will contain the eval_metrics passed to the fit function.

Return type:dictionary

 案例:

xgboost原理性

https://blog.csdn.net/a1b2c3d4123456/article/details/52849091

非常好的原理解釋https://blog.csdn.net/sb19931201/article/details/52557382

 

 

 

 

 

 

 


免責聲明!

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



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