Xgboost参数解释及调参

首先xgboost有两种接口,xgboost自带API和Scikit-Learn的API,具体用法有细微的差别但不大。

在运行 XGBoost 之前, 我们必须设置三种类型的参数: (常规参数)general parameters,(提升器参数)booster parameters和(任务参数)task parameters。

  • 常规参数与我们用于提升的提升器有关,通常是树模型或线性模型
  • 提升器参数取决于你所选择的提升器
  • 学习任务的参数决定了学习场景, 例如回归任务可以使用不同的参数进行排序相关的任务
  • 命令行参数的行为与 xgboost 的 CLI 版本相关

本文只介绍xgboost自带的API,Scikit-Learn的API可以对照参考。

xgboost.train(params, dtrain, num_boost_round=10, evals=(), \
obj=None, feval=None, maximize=False, early_stopping_rounds=None, \
evals_result=None, verbose_eval=True, learning_rates=None, \
xgb_model=None, callbacks=None)

params:这是一个字典,里面包含着训练中的参数关键字和对应的值,形式如下:

params = {
    'booster':'gbtree',
    'min_child_weight': 100,
    'eta': 0.02,
    'colsample_bytree': 0.7,
    'max_depth': 12,
    'subsample': 0.7,
    'alpha': 1,
    'gamma': 1,
    'silent': 1,
    'objective': 'reg:linear',
    'verbose_eval': True,
    'seed': 12
}

其中具体的参数以下会介绍。

General Parameters

  • booster [default=gbtree]

    • 有两中模型可以选择gbtreegblineargbtree使用基于树的模型进行提升计算,gblinear使用线性模型进行提升计算。缺省值为gbtree
  • silent [default=0]

    • 取0时表示打印出运行时信息,取1时表示以缄默方式运行,不打印运行时信息。缺省值为0。
  • nthread [default to maximum number of threads available if not set]

    • XGBoost运行时的线程数。缺省值是当前系统可以获得的最大线程数
  • num_pbuffer [set automatically by xgboost, no need to be set by user]

    • size of prediction buffer, normally set to number of training instances. The buffers are used to save the prediction results of last boosting step.
  • num_feature [set automatically by xgboost, no need to be set by user]

    • boosting过程中用到的特征维数,设置为特征个数。XGBoost会自动设置,不需要手工设置。

Booster Parameters

  • eta [default=0.3]

    • 为了防止过拟合,更新过程中用到的收缩步长。在每次提升计算之后,算法会直接获得新特征的权重。 eta通过缩减特征的权重使提升计算过程更加保守。缺省值为0.3
    • 取值范围为:[0,1]
  • gamma [default=0]

    • minimum loss reduction required to make a further partition on a leaf node of the tree. the larger, the more conservative the algorithm will be.
    • range: [0,∞]
  • max_depth [default=6]

    • 数的最大深度。缺省值为6
    • 取值范围为:[1,∞]
  • min_child_weight [default=1]

    • 孩子节点中最小的样本权重和。如果一个叶子节点的样本权重和小于min_child_weight则拆分过程结束。在现行回归模型中,这个参数是指建立每个模型所需要的最小样本数。该成熟越大算法越conservative
    • 取值范围为: [0,∞]
  • max_delta_step [default=0]

    • Maximum delta step we allow each tree’s weight estimation to be. If the value is set to 0, it means there is no constraint. If it is set to a positive value, it can help making the update step more conservative. Usually this parameter is not needed, but it might help in logistic regression when class is extremely imbalanced. Set it to value of 1-10 might help control the update
    • 取值范围为:[0,∞]
  • subsample [default=1]

    • 用于训练模型的子样本占整个样本集合的比例。如果设置为0.5则意味着XGBoost将随机的冲整个样本集合中随机的抽取出50%的子样本建立树模型,这能够防止过拟合。
    • 取值范围为:(0,1]
  • colsample_bytree [default=1]
    • 在建立树时对特征采样的比例。缺省值为1
    • 取值范围:(0,1]

Task Parameters

  • objective [ default=reg:linear ]
    定义学习任务及相应的学习目标,可选的目标函数如下:

    • “reg:linear” –线性回归。
    • “reg:logistic” –逻辑回归。
    • “binary:logistic”–二分类的逻辑回归问题,输出为概率。
    • “binary:logitraw”–二分类的逻辑回归问题,输出的结果为wTx。
    • “count:poisson”–计数问题的poisson回归,输出结果为poisson分布。在poisson回归中,max_delta_step的缺省值为0.7。(used to safeguard optimization)
    • “multi:softmax” –让XGBoost采用softmax目标函数处理多分类问题,同时需要设置参数num_class(类别个数)
    • “multi:softprob” –和softmax一样,但是输出的是ndata * nclass的向量,可以将该向量reshape成ndata行nclass列的矩阵。没行数据表示样本所属于每个类别的概率。
    • “rank:pairwise”–set XGBoost to do ranking task by minimizing the pairwise loss
  • base_score [ default=0.5 ]

    • the initial prediction score of all instances, global bias
  • eval_metric [ default according to objective ]
    • 校验数据所需要的评价指标,不同的目标函数将会有缺省的评价指标(rmse for regression, and error for classification, mean average precision for ranking)
    • 用户可以添加多种评价指标,对于Python用户要以list传递参数对给程序,而不是map参数list参数不会覆盖’eval_metric’
    • The choices are listed below:
    • “rmse”: root mean square error
    • “logloss”: negative log-likelihood
    • “error”: Binary classification error rate. It is calculated as #(wrong cases)/#(all cases). For the predictions, the evaluation will regard the instances with prediction value larger than 0.5 as positive instances, and the others as negative instances.
    • “merror”: Multiclass classification error rate. It is calculated as #(wrong cases)/#(all cases).
    • “mlogloss”: Multiclass logloss
    • “auc”: Area under the curve for ranking evaluation.
    • “ndcg”:Normalized Discounted Cumulative Gain
    • “map”:Mean average precision
    • “ndcg@n”,”map@n”: n can be assigned as an integer to cut off the top positions in the lists for evaluation.
    • “ndcg-”,”map-”,”ndcg@n-”,”map@n-”: In XGBoost, NDCG and MAP will evaluate the score of a list without any positive samples as 1. By adding “-” in the evaluation metric XGBoost will evaluate these score as 0 to be consistent under some conditions. training repeatively
    • “gamma-deviance”: [residual deviance for gamma regression]
  • seed[ default=0 ]

    • random number seed.

    • 随机数的种子。缺省值为0

  • dtrain:训练的数据

  • num_boost_round:这是指提升迭代的次数,也就是生成多少基模型

  • evals:这是一个列表,用于对训练过程中进行评估列表中的元素。形式是evals = [(dtrain,'train'),(dval,'val')]或者是evals = [(dtrain,'train')],对于第一种情况,它使得我们可以在训练过程中观察验证集的效果

  • obj:自定义目的函数

  • feval:自定义评估函数

  • maximize:是否对评估函数进行最大化

  • early_stopping_rounds:早期停止次数 ,假设为100,验证集的误差迭代到一定程度在100次内不能再继续降低,就停止迭代。这要求evals 里至少有 一个元素,如果有多个,按最后一个去执行。返回的是最后的迭代次数(不是最好的)。如果early_stopping_rounds存在,则模型会生成三个属性,bst.best_scorebst.best_iterationbst.best_ntree_limit

  • evals_result:字典,存储在watchlist中的元素的评估结果。

  • verbose_eval :(可以输入布尔型或数值型),也要求evals里至少有 一个元素。如果为True,则对evals中元素的评估结果会输出在结果中;如果输入数字,假设为5,则每隔5个迭代输出一次。

  • learning_rates:每一次提升的学习率的列表,

  • xgb_model:在训练之前用于加载的xgb model。

  • 28
    点赞
  • 219
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
XGBoost是一种非常强大的机器学习算法,但是它的调参比较复杂。下面是XGBoost参数调参的步骤及代码: 1. 确定参数范围:首先需要确定每个超参数的范围,可以根据经验或者网上资料来确定。常见的超参数包括learning_rate、max_depth、min_child_weight、subsample、colsample_bytree等。 ``` # 设置超参数的范围 param_grid = { 'learning_rate': [0.01, 0.05, 0.1, 0.15, 0.2], 'max_depth': [3, 4, 5, 6, 7], 'min_child_weight': [1, 3, 5, 7], 'subsample': [0.6, 0.7, 0.8, 0.9], 'colsample_bytree': [0.6, 0.7, 0.8, 0.9], } ``` 2. 网格搜索:使用GridSearchCV函数进行网格搜索,该函数会遍历所有可能的超参数组合,并返回最优的超参数。 ``` from sklearn.model_selection import GridSearchCV # 定义XGBoost模型 xgb_model = xgb.XGBClassifier() # 进行网格搜索 grid_search = GridSearchCV(estimator=xgb_model, param_grid=param_grid, cv=5, n_jobs=-1) # 拟合训练数据 grid_search.fit(train_X, train_y) # 输出最优的超参数 print(grid_search.best_params_) ``` 3. 随机搜索:如果网格搜索的超参数范围比较大,那么计算量可能会很大,这时候可以使用随机搜索来减少计算量。随机搜索会在超参数范围内随机选择若干个超参数组合进行训练,并返回最优的超参数。 ``` from sklearn.model_selection import RandomizedSearchCV # 定义XGBoost模型 xgb_model = xgb.XGBClassifier() # 进行随机搜索 random_search = RandomizedSearchCV(estimator=xgb_model, param_distributions=param_grid, cv=5, n_jobs=-1, n_iter=20) # 拟合训练数据 random_search.fit(train_X, train_y) # 输出最优的超参数 print(random_search.best_params_) ``` 4. 调整学习率:学习率是XGBoost中非常重要的超参数,它控制每次迭代的步长。如果学习率过大,可能会导致算法无法收敛;如果学习率过小,可能会导致算法收敛速度过慢。因此,需要在确定好其他超参数后,再调整学习率。 ``` # 定义XGBoost模型 xgb_model = xgb.XGBClassifier(learning_rate=0.1, max_depth=5, min_child_weight=3, subsample=0.8, colsample_bytree=0.8) # 训练模型 xgb_model.fit(train_X, train_y, eval_metric='auc') # 调整学习率 learning_rates = [0.01, 0.05, 0.1, 0.15, 0.2] for learning_rate in learning_rates: xgb_model.set_params(learning_rate=learning_rate) xgb_model.fit(train_X, train_y, eval_metric='auc') print("Learning rate: ", learning_rate) print("Accuracy score (train): {0:.3f}".format(xgb_model.score(train_X, train_y))) print("Accuracy score (test): {0:.3f}".format(xgb_model.score(test_X, test_y))) ``` 以上就是XGBoost参数调参的步骤及代码。需要注意的是,调参是一个反复试错的过程,需要不断地尝试不同的超参数组合,并根据模型的表现来调整超参数
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值