# 常規參數
- 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()
# -*- coding: utf-8 -*-
"""
###############################################################################
# 作者:wanglei5205
# 郵箱:wanglei5205@126.com
# 代碼:http://github.com/wanglei5205
# 博客:http://cnblogs.com/wanglei5205
# 目的:學習xgboost的XGBClassifier函數
# 官方API文檔:http://xgboost.readthedocs.io/en/latest/python/python_api.html#module-xgboost.training
###############################################################################
"""
### load module
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from xgboost import XGBClassifier
from xgboost import plot_importance
### load datasets
digits = datasets.load_digits()
### data analysis
print(digits.data.shape)
print(digits.target.shape)
### data split
x_train,x_test,y_train,y_test = train_test_split(digits.data,
digits.target,
test_size = 0.3,
random_state = 33)
### fit model for train data
model = XGBClassifier(learning_rate=0.1,
n_estimators=1000, # 樹的個數--1000棵樹建立xgboost
max_depth=6, # 樹的深度
min_child_weight = 1, # 葉子節點最小權重
gamma=0., # 懲罰項中葉子結點個數前的參數
subsample=0.8, # 隨機選擇80%樣本建立決策樹
colsample_btree=0.8, # 隨機選擇80%特征建立決策樹
objective='multi:softmax', # 指定損失函數
scale_pos_weight=1, # 解決樣本個數不平衡的問題
random_state=27 # 隨機數
)
model.fit(x_train,
y_train,
eval_set = [(x_test,y_test)],
eval_metric = "mlogloss",
early_stopping_rounds = 10,
verbose = True)
### plot feature importance
fig,ax = plt.subplots(figsize=(15,15))
plot_importance(model,
height=0.5,
ax=ax,
max_num_features=64)
plt.show()
### make prediction for test data
y_pred = model.predict(x_test)
### model evaluate
accuracy = accuracy_score(y_test,y_pred)
print("accuarcy: %.2f%%" % (accuracy*100.0))
"""
95.74%
"""