python xgboost输出变量重要性_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)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值