Python 中的 XGBoost XGBClassifier 默认值

XGBoost XGBClassifier Defaults in Python

我正在尝试使用 XGBoosts 分类器对一些二进制数据进行分类。当我做最简单的事情并且只使用默认值时(如下)

clf = xgb.XGBClassifier()
metLearn=CalibratedClassifierCV(clf, method='isotonic', cv=2)
metLearn.fit(train, trainTarget)
testPredictions = metLearn.predict(test)

我得到了相当不错的分类结果。

我的下一步是尝试调整我的参数。从参数指南猜测...... https://github.com/dmlc/xgboost/blob/master/doc/parameter.md 我想从默认开始并从那里开始工作...

# setup parameters for xgboost
param = {}
param['booster'] = 'gbtree'
param['objective'] = 'binary:logistic'
param["eval_metric"] = "error"
param['eta'] = 0.3
param['gamma'] = 0
param['max_depth'] = 6
param['min_child_weight']=1
param['max_delta_step'] = 0
param['subsample']= 1
param['colsample_bytree']=1
param['silent'] = 1
param['seed'] = 0
param['base_score'] = 0.5

clf = xgb.XGBClassifier(params)
metLearn=CalibratedClassifierCV(clf, method='isotonic', cv=2)
metLearn.fit(train, trainTarget)
testPredictions = metLearn.predict(test)

结果是一切都被预测为条件之一,而不是另一个。

好奇如果我设置

params={}

我希望给我与不提供任何参数相同的默认值,但我遇到了同样的事情

那么有人知道 XGBclassifier 的默认值是什么吗?这样我就可以开始调整了?

这不是您在 xgboost 中设置参数的方式。您可能想要将参数网格传递到训练函数中,例如 xgboost 的 train 或 sklearn 的 GridSearchCV,或者您可能想要使用 XGBClassifier 的 set_params 方法。另一件需要注意的事情是,如果您使用 xgboost 的包装器来进行 sklearn(即:XGBClassifier()XGBRegressor() classes),那么使用的参数名称与 sklearn 自己的参数名称相同GBM class(例如:eta --> learning_rate)。我没有看到 sklearn 包装器的确切文档隐藏在哪里,但是那些 classes 的代码在这里:https://github.com/dmlc/xgboost/blob/master/python-package/xgboost/sklearn.py

此处供您参考的是如何直接设置模型对象参数。

>>> grid = {'max_depth':10}
>>> 
>>> clf = XGBClassifier()
>>> clf.max_depth
3
>>> clf.set_params(**grid)
XGBClassifier(base_score=0.5, colsample_bylevel=1, colsample_bytree=1,
       gamma=0, learning_rate=0.1, max_delta_step=0, max_depth=10,
       min_child_weight=1, missing=None, n_estimators=100, nthread=-1,
       objective='binary:logistic', reg_alpha=0, reg_lambda=1,
       scale_pos_weight=1, seed=0, silent=True, subsample=1)
>>> clf.max_depth
10

编辑: 我想您可以在创建模型时设置参数,这样做并不是很典型,因为大多数人都以某种方式进行网格搜索。但是,如果您这样做,则需要将它们列为完整参数或使用 **kwargs。例如:

>>> XGBClassifier(max_depth=10)
XGBClassifier(base_score=0.5, colsample_bylevel=1, colsample_bytree=1,
       gamma=0, learning_rate=0.1, max_delta_step=0, max_depth=10,
       min_child_weight=1, missing=None, n_estimators=100, nthread=-1,
       objective='binary:logistic', reg_alpha=0, reg_lambda=1,
       scale_pos_weight=1, seed=0, silent=True, subsample=1)
>>> XGBClassifier(**grid)
XGBClassifier(base_score=0.5, colsample_bylevel=1, colsample_bytree=1,
       gamma=0, learning_rate=0.1, max_delta_step=0, max_depth=10,
       min_child_weight=1, missing=None, n_estimators=100, nthread=-1,
       objective='binary:logistic', reg_alpha=0, reg_lambda=1,
       scale_pos_weight=1, seed=0, silent=True, subsample=1)

在不使用 **kwargs 的情况下使用字典作为输入会将该参数设置为您的字典:

>>> XGBClassifier(grid)
XGBClassifier(base_score=0.5, colsample_bylevel=1, colsample_bytree=1,
       gamma=0, learning_rate=0.1, max_delta_step=0,
       max_depth={'max_depth': 10}, min_child_weight=1, missing=None,
       n_estimators=100, nthread=-1, objective='binary:logistic',
       reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=0, silent=True,
       subsample=1)

首先,您的变量 param.

似乎缺少 s

你在顶部写了 param:

param = {}
param['booster'] = 'gbtree'
param['objective'] = 'binary:logistic'
  .
  .
  .

...但是在训练模型时使用params

clf = xgb.XGBClassifier(params)  <-- different variable!

这只是您示例中的错字吗?

XGBClassifier 的默认值是:

  • max_depth=3
  • learning_rate=0.1
  • n_estimators=100
  • 沉默=真
  • objective='binary:logistic'
  • 助推器='gbtree'
  • n_jobs=1
  • nthread=None
  • 伽玛=0
  • min_child_weight=1
  • max_delta_step=0
  • 子样本=1
  • colsample_bytree=1
  • colsample_bylevel=1
  • reg_alpha=0
  • reg_lambda=1
  • scale_pos_weight=1
  • base_score=0.5
  • random_state=0
  • 种子=None
  • 缺失=None

Link 到具有 class 默认值的 XGBClassifier 文档:https://xgboost.readthedocs.io/en/latest/python/python_api.html#xgboost.XGBClassifier

你就快完成了!您只是忘记解压 params 字典(** 运算符)。而不是这个(它传递一个字典作为第一个位置参数):

clf = xgb.XGBClassifier(params)

你应该这样做(这使得字典中的每个键都作为关键字参数传递):

clf = xgb.XGBClassifier(**params)

(已更新)一旦您适合 out-of-box 分类器模型,默认值就可见:

XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,
              colsample_bynode=1, colsample_bytree=1, gamma=0, gpu_id=-1,
              importance_type='gain', interaction_constraints='',
              learning_rate=0.300000012, max_delta_step=0, max_depth=6,
              min_child_weight=1, missing=nan, monotone_constraints='()',
              n_estimators=100, n_jobs=12, num_parallel_tree=1,
              objective='multi:softprob', random_state=0, reg_alpha=0,
              reg_lambda=1, scale_pos_weight=None, subsample=1,
              tree_method='exact', use_label_encoder=False,
              validate_parameters=1, verbosity=None)

详情请见此处:https://xgboost.readthedocs.io/en/latest/parameter.html