XGBoost特征重要性以及CV

1 feature importance

gradient boosting 有一个优点是可以给出训练好的模型的特征重要性,这样就可以知道哪些变量需要被保留,哪些可以舍弃。
首先要进入两个类:

from xgboost import plot_importance
from matplotlib import pyplot

然后在model fit后面添加打印或绘制特征重要性:

model.fit(X, y)
plot_importance(model)
pyplot.show()

2 参数优化

XGBoost中提供GridSearchCV方法来对一些重要的超参进行调整,例如:
learning_rate
tree_depth
subsample
n_estimators
max_depth

当然,大多时候学习率的大小一定程度上很影响模型的稳定性,学习率较小容易过拟合,学习率较大又容易欠拟合,所以很多时候都会先考虑对学习率进行调整。
引入如下两个类:

from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import StratifiedKFold

将要尝试的学习率的值放在一个list中即可:
learning_rate = [0.005, 0.001, 0.01, 0.1, 0.2, 0.3]

model = XGBClassifier()
learning_rate = [0.005, 0.001, 0.01, 0.1, 0.2, 0.3]
param_grid = dict(learning_rate=learning_rate)
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=7)
grid_search = GridSearchCV(model, param_grid, scoring="neg_log_loss", n_jobs=-1, cv=kfold)
grid_result = grid_search.fit(X, Y)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容