【集成學習】sklearn中xgboost模塊的XGBClassifier函數

原文

常規參數

booster
gbtree 樹模型做爲基分類器(默認)
gbliner 線性模型做爲基分類器
silent
silent=0時,不輸出中間過程(默認)
silent=1時,輸出中間過程
nthread
nthread=-1時,使用全部CPU進行並行運算(默認)
nthread=1時,使用1個CPU進行運算。
scale_pos_weight
正樣本的權重,在二分類任務中,當正負樣本比例失衡時,設置正樣本的權重,模型效果更好。例如,當正負樣本比例爲1:10時,scale_pos_weight=10。

模型參數

n_estimatores
含義:總共迭代的次數,即決策樹的個數
調參:
early_stopping_rounds
含義:在驗證集上,當連續n次迭代,分數沒有提高後,提前終止訓練。
調參:防止overfitting。
max_depth
含義:樹的深度,默認值爲6,典型值3-10。
調參:值越大,越容易過擬合;值越小,越容易欠擬合。
min_child_weight
含義:默認值爲1,。
調參:值越大,越容易欠擬合;值越小,越容易過擬合(值較大時,避免模型學習到局部的特殊樣本)。
subsample
含義:訓練每棵樹時,使用的數據佔全部訓練集的比例。默認值爲1,典型值爲0.5-1。
調參:防止overfitting。
colsample_bytree
含義:訓練每棵樹時,使用的特徵佔全部特徵的比例。默認值爲1,典型值爲0.5-1。
調參:防止overfitting。

學習任務參數

learning_rate
含義:學習率,控制每次迭代更新權重時的步長,默認0.3。
調參:值越小,訓練越慢。
典型值爲0.01-0.2。
objective 目標函數
迴歸任務
reg:linear (默認)
reg:logistic
二分類
binary:logistic 概率
binary:logitraw 類別
多分類
multi:softmax num_class=n 返回類別
multi:softprob num_class=n 返回概率
rank:pairwise
eval_metric
迴歸任務(默認rmse)
rmse–均方根誤差
mae–平均絕對誤差
分類任務(默認error)
auc–roc曲線下面積
error–錯誤率(二分類)
merror–錯誤率(多分類)
logloss–負對數似然函數(二分類)
mlogloss–負對數似然函數(多分類)

gamma
懲罰項係數,指定節點分裂所需的最小損失函數下降值。
調參:
alpha
L1正則化係數,默認爲1
lambda
L2正則化係數,默認爲1

代碼主要函數:

載入數據:load_digits()
數據拆分:train_test_split()
建立模型:XGBClassifier()
模型訓練:fit()
模型預測:predict()
性能度量:accuracy_score()
特徵重要性:plot_importance()

複製代碼
1 # -- coding: utf-8 --
2 “”"
3 ###############################################################################
4 # 作者:wanglei5205
5 # 郵箱:[email protected]
6 # 代碼:http://github.com/wanglei5205
7 # 博客:http://cnblogs.com/wanglei5205
8 # 目的:學習xgboost的XGBClassifier函數
9 # 官方API文檔:http://xgboost.readthedocs.io/en/latest/python/python_api.html#module-xgboost.training
10 ###############################################################################
11 “”"
12 ### load module
13 import matplotlib.pyplot as plt
14 from sklearn import datasets
15 from sklearn.model_selection import train_test_split
16 from sklearn.metrics import accuracy_score
17 from xgboost import XGBClassifier
18 from xgboost import plot_importance
19
20 ### load datasets
21 digits = datasets.load_digits()
22
23 ### data analysis
24 print(digits.data.shape)
25 print(digits.target.shape)
26
27 ### data split
28 x_train,x_test,y_train,y_test = train_test_split(digits.data,
29 digits.target,
30 test_size = 0.3,
31 random_state = 33)
32 ### fit model for train data
33 model = XGBClassifier(learning_rate=0.1,
34 n_estimators=1000, # 樹的個數–1000棵樹建立xgboost
35 max_depth=6, # 樹的深度
36 min_child_weight = 1, # 葉子節點最小權重
37 gamma=0., # 懲罰項中葉子結點個數前的參數
38 subsample=0.8, # 隨機選擇80%樣本建立決策樹
39 colsample_btree=0.8, # 隨機選擇80%特徵建立決策樹
40 objective=‘multi:softmax’, # 指定損失函數
41 scale_pos_weight=1, # 解決樣本個數不平衡的問題
42 random_state=27 # 隨機數
43 )
44 model.fit(x_train,
45 y_train,
46 eval_set = [(x_test,y_test)],
47 eval_metric = “mlogloss”,
48 early_stopping_rounds = 10,
49 verbose = True)
50
51 ### plot feature importance
52 fig,ax = plt.subplots(figsize=(15,15))
53 plot_importance(model,
54 height=0.5,
55 ax=ax,
56 max_num_features=64)
57 plt.show()
58
59 ### make prediction for test data
60 y_pred = model.predict(x_test)
61
62 ### model evaluate
63 accuracy = accuracy_score(y_test,y_pred)
64 print(“accuarcy: %.2f%%” % (accuracy*100.0))
65 “”"
66 95.74%
67 “”"
複製代碼

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