我试图使用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中设置参数的方式.您可能希望将param网格传递到训练函数中,例如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的默认值为:
MAX_DEPTH = 3
learning_rate = 0.1
n_estimators = 100
沉默=真
客观="二进制:物流"
升压= '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