gpt4 book ai didi

python - Python 中的 XGBoost XGBClassifier 默认值

转载 作者:IT老高 更新时间:2023-10-28 20:33:16 25 4
gpt4 key购买 nike

我正在尝试使用 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() 类),那么使用的参数名称是相同的在 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)

关于python - Python 中的 XGBoost XGBClassifier 默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34674797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com