【问题标题】:XGBoost XGBClassifier Defaults in PythonPython 中的 XGBoost XGBClassifier 默认值
【发布时间】:2016-04-13 00:04:33
【问题描述】:

我正在尝试使用 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 的默认值是什么吗?这样我就可以开始调优了吗?

【问题讨论】:

标签: python scikit-learn classification analytics xgboost


【解决方案1】:

这不是您在 xgboost 中设置参数的方式。您可能希望将参数网格传递给您的训练函数,例如 xgboost 的 train 或 sklearn 的 GridSearchCV,或者您希望使用 XGBClassifier 的 set_params 方法。另一件需要注意的事情是,如果您使用 xgboost 的 sklearn 包装器(即:XGBClassifier()XGBRegressor() 类),则使用的参数名称与 sklearn 自己的 GBM 类中使用的名称相同(例如:eta -->学习率)。我没有看到 sklearn 包装器的确切文档隐藏在哪里,但这些类的代码在这里: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)

【讨论】:

  • 尽管我希望这是真的,但您不能将参数网格传递给 xgboost 的 train 函数 - 参数字典值不能是列表
【解决方案2】:

XGBClassifier 的默认值是:

  • max_depth=3
  • learning_rate=0.1
  • n_estimators=100
  • 静默=真
  • objective='binary:logistic'
  • booster='gbtree'
  • n_jobs=1
  • nthread=无
  • 伽马=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
  • 种子=无
  • 缺失=无

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

【讨论】:

  • 官方文档中没有为 sklearn API 的 XGBClassifier 引用默认参数(它们用于官方默认 xgboost API,但不能保证它与 sklearn 使用的默认参数相同,尤其是当 xgboost 状态时使用它时某些行为会有所不同)。有人知道现在在哪里可以找到它吗?为了了解默认参数可能是什么,不得不深入研究源代码真的不值得。
  • 不幸的是,这些是我所拥有的最接近官方文档的文档,但它们在我需要时可以可靠地定义默认值
【解决方案3】:

对于初学者,您的变量 param 似乎缺少 s

你在顶部写了 param

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

...但在训练模型时使用 params 更远:

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

这只是你的例子中的一个错字吗?

【讨论】:

    【解决方案4】:

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

    clf = xgb.XGBClassifier(params)
    

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

    clf = xgb.XGBClassifier(**params)
    

    【讨论】:

      【解决方案5】:

      (更新)一旦您适应了开箱即用的分类器模型,默认值就会可见:

      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

      【讨论】:

        猜你喜欢
        • 2019-03-20
        • 2023-03-15
        • 1970-01-01
        • 1970-01-01
        • 2015-11-04
        • 2018-11-17
        • 2012-08-01
        • 1970-01-01
        • 2015-03-15
        相关资源
        最近更新 更多