0%

Machine Learning | XGBoost

eXtreme Gradient Boosting

XGBoost

作者:陈天奇
论文:XGBoost: A Scalable Tree Boosting System

  • 是梯度提升树框架下的一种机器学习算法

实例

XGBoost vs GBDT

GBDT XGBoost
基分类器 CART 还支持线性分类器
优化 梯度下降法[1] 牛顿法[2]
正则项[3] 没有
Learning rate Shrinkage
列抽样 支持列抽样[4]
缺失值 可以自动学习出缺失值的分裂方向

Python

特征重要性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from xgboost import XGBClassifier, plot_importance
from IPython.display import display

# 特征重要性(筛选变量)
cols_x = [
## 特征
]

cols_y = [
## 因变量
]

X = df[cols_x]
y = df[cols_y]
## 如果特征的量纲较大,应先进行标准化
arr_mean = np.mean(X)
arr_std = np.std(X, ddof=1)
newX = (X - arr_mean) / arr_std
X = newX

print("XGBoost Start!")
# 训练模型
model = XGBoostClassifier()
model.fit(X, y)

df_ip = pd.DataFrame(columns=['Feature', 'Importance'])
df_ip['Feature'] = X.columns
df_ip['Importance'] = model.feature_importances_
df_ip = df_ip.sort_values(by=['Importance'], ascending=False)
df_ip = df_ip.reset_index(dro=p=True)
df_ip.to_csv('xxxxx_xgboost_feature_importance.csv', index=False, encoding='utf_8_sig')
display(df_ip)
# cols_ip = df_ip.Feature[:10].to_list() ## 选取TOP 10特征
print("XGBoost Done!\n")

参考资料


  1. 在优化时只用到一阶导数信息 ↩︎

  2. XGBoost对代价函数进行了二阶泰勒展开,同时使用了一阶导数和二阶导数的信息;XGBoost还支持自定义代价函数 ↩︎

  3. 正则项降低了模型的Variance,学习得到的模型复杂度更低(防止过拟合) ↩︎

  4. XGBoost借鉴了随机森林的做法,支持列抽样(column sampling),既能降低过拟合,还能减少计算 ↩︎

Thank you for your approval.

欢迎关注我的其它发布渠道