Python中的XGBoost XGBClassifier默认值

2024-04-26 06:07:33 发布

您现在位置: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的默认值是什么吗?这样我就可以开始调谐了?


Tags: 参数param分类train事情methodcvfit
3条回答

这不是在xgboost中设置参数的方式。您可以将参数网格传递到训练函数中,例如xgboost的train或sklearn的GridSearchCV,也可以使用XGBClassifier的set_params方法。另一件需要注意的事情是,如果您使用xgboost的包装器来sklearn(即,XGBClassifier()XGBRegressor()类),那么所使用的参数名与sklearn自己的GBM类中使用的参数名相同(例如:eta-->;learning-rate)。我不知道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)

XGBClassifier的默认值为:

  • 最大深度=3
  • 学习率=0.1
  • n_估计=100
  • 静默=真
  • objective='二进制:逻辑'
  • 助推器'gbtree'
  • n_作业=1
  • nthread=无
  • 伽马=0
  • 最小儿童体重=1
  • 最大增量步长=0
  • 子样本=1
  • colsample_bytree=1
  • colsample_bylevel=1
  • 注册阿尔法=0
  • 雷格朗姆达=1
  • 秤位重量=1
  • 基本得分=0.5
  • 随机状态=0
  • 种子=无
  • 缺失=无

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

首先,看起来您缺少变量params

你在上面写了param

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

…但在训练模型时,请在更下面的位置使用params

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

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

相关问题 更多 >

    热门问题