1. 程式人生 > >Python機器學習之XGBoost從入門到實戰(基本理論說明)

Python機器學習之XGBoost從入門到實戰(基本理論說明)

Xgboost從基礎到實戰

XGBoost:eXtreme Gradient Boosting
    * 應用機器學習領域的一個強有力的工具
    * Gradient Booting Machines(GBM)的優化表現,快速有效
        —深盟分散式機器學習開源平臺(Distributed machine learning Community,DMLC)的分支
        —DMLC也開源流行的深度學習庫mxnet
    * GBM:
        Machine:機器學習模型-對資料的產生規律建模
        Boosting Machines:弱學習器組合成強學習器/模型
        Gradient Booting Machines:根據梯度下降方式組合弱學習器

Machines:
    Machines:機器學習模型,建模資料產生規律--最小化目標函式
目標函式通常包括兩個部分: * 損失函式:與任務相關(選擇和訓練資料匹配的最好模型) ** 迴歸:殘差平方 ** 分類:0-1損失、logistic損失、合葉損失(SVM) *正則項:與模型複雜度有關(選擇最簡單的模型) L0、L1、L2正則項 L1會趨向於產生少量的特徵,而其他的特徵都是0,而L2會選擇更多的特徵,這些特徵都會接近於0。L1在特徵選擇時候非常有用,而L2就只是一種規則化而已。 Boosting Machines * Boosting:價格弱學習器組合成強學習器 * 弱學習器;比隨機猜測效能好的學習器 * 常用弱學習器:決策樹/分類迴歸樹 決策樹:每個葉子節點對應一個決策 分類迴歸樹:每個葉子節點有個預測分數(score),比決策樹更加的靈活 * Adaptive Boosting(AdaBoost):第一個Boosting演算法,最初應用於人臉識別,現在這個工具基本上用於機器學習 弱分類器:只有一個分裂的決策樹 對於當前分類器不能處理的樣本,增加其權重 不斷的加入新的弱學習器,知道達到終止條件(強學習器:弱學習器的加權線性組合,權重與其正確率有關) Gradient Boosting Machine(GBM) * Friedman將AdaBoost推廣到一般Gradient Booting框架,得到Gradient Booting Machines(GBM):將Booting視作一個數值優化問題,採用類似梯度下降的方式優化求解 也被稱為stage-wise additive model:每次增加一個弱分類器到已有模型,已在模型中的弱學習器不在變化 這種推廣使得我們可以使用可微的損失函式,從而支援的任務從兩類分類擴充套件到迴歸和多類分類問題。 XGBoost的特別之處: * 正則化:以“正則化提升(regularized booting )”著稱 -標準GBM的實現沒有顯式的正則化步驟 -正則化對減少過擬合有幫助 * 並行處理,相比GBM有了速度的飛躍 -藉助OpenMP,自動將單機CPU的多核進行平行計算 -支援GPU加速 -支援分散式 * 高度的靈活性:允許使用者自定義優化目標和評級標準 -只需要損失函式的一階導數和二階導數 * 剪枝 -當新增分裂帶來負增益時,GBM會停止分裂 -XGBoost一直分裂到指定的最大深度(max_depth),然後回頭來剪枝 * 內建交叉驗證 -XGBoost允許子在每一輪Boosting迭代中使用交叉驗證->可以方便的獲得Boosting迭代次數 -GBM使用網格搜尋,只能檢測有限個值 * 線上學習;XGBoost和GBM都支援 XGBoost的優勢 執行速度:確實比其他Gradient boosting實現快 模型效能:在結構化資料集上,在分類/迴歸/排序預測建模上表現突出 如何使用? 處理流程: 訓練資料讀取-->資料匯入(設定訓練引數(超引數))-->模型訓練-->模型評估(如果效果不好要轉換到第二步驟)
注意:訓練資料要進行特徵變換一.xgboost的優點 1.正則化 xgboost在代價函式里加入了正則項,用於控制模型的複雜度。正則項裡包含了樹的葉子節點個數、每個葉子節點上輸出的score的L2模的平方和。從Bias-variance tradeoff角度來講,正則項降低了模型的variance,使學習出來的模型更加簡單,防止過擬合,這也是xgboost優於傳統GBDT的一個特性。 2.並行處理 xgboost工具支援並行。boosting不是一種序列的結構嗎?怎麼並行的?注意xgboost的並行不是tree粒度的並行,xgboost也是一次迭代完才能進行下一次迭代的(第t次迭代的代價函式裡包含了前面t-1
次迭代的預測值)。xgboost的並行是在特徵粒度上的。我們知道,決策樹的學習最耗時的一個步驟就是對特徵的值進行排序(因為要確定最佳分割點),xgboost在訓練之前,預先對資料進行了排序,然後儲存為block結構,後面的迭代中重複地使用這個結構,大大減小計算量。這個block結構也使得並行成為了可能,在進行節點的分裂時,需要計算每個特徵的增益,最終選增益最大的那個特徵去做分裂,那麼各個特徵的增益計算就可以開多執行緒進行。 3.靈活性 xgboost支援使用者自定義目標函式和評估函式,只要目標函式二階可導就行。 4.缺失值的處理 對於特徵的值有缺失的樣本,xgboost可以自動學習出它的分裂方向 5.剪枝 XGBoost 先從頂到底建立所有可以建立的子樹,再從底到頂反向進行剪枝。比起GBM,這樣不容易陷入區域性最優解 6.內建交叉驗證 xgb.cv() 是不是感覺很方便 二.xgboost的引數 *XGBoost的引數可以分為三種類型:通用引數、booster引數以及學習目標引數 ** General Parameters:引數控制在提升(boosting)過程中使用哪種booster,常用的booster有樹模型(tree)和線性模型(linear model)。 ** Booster parameters:這取決於使用哪種booster。 ** Learning Task parameters:控制學習的場景,例如在迴歸問題中會使用不同的引數控制排序。 ** 除了以上引數還可能有其它引數,在命令列中使用 * 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即可得到相同的隨機劃分)

““