XGBRegressor与xgboost.train的巨大速度差异?


13

如果我使用以下代码训练模型:

import xgboost as xg
params = {'max_depth':3,
'min_child_weight':10,
'learning_rate':0.3,
'subsample':0.5,
'colsample_bytree':0.6,
'obj':'reg:linear',
'n_estimators':1000,
'eta':0.3}

features = df[feature_columns]
target = df[target_columns]
dmatrix = xg.DMatrix(features.values,
                     target.values,
                     feature_names=features.columns.values)
clf = xg.train(params, dmatrix)

它会在大约1分钟内完成。

如果我使用Sci-Kit学习方法训练模型:

import xgboost as xg
max_depth = 3
min_child_weight = 10
subsample = 0.5
colsample_bytree = 0.6
objective = 'reg:linear'
num_estimators = 1000
learning_rate = 0.3

features = df[feature_columns]
target = df[target_columns]
clf = xg.XGBRegressor(max_depth=max_depth,
                min_child_weight=min_child_weight,
                subsample=subsample,
                colsample_bytree=colsample_bytree,
                objective=objective,
                n_estimators=num_estimators,
                learning_rate=learning_rate)
clf.fit(features, target)

需要30分钟以上。

我认为底层代码几乎完全相同(即XGBRegressor调用xg.train)-这是怎么回事?

Answers:


19

xgboost.trainn_estimatorsxgboost.XGBRegressor接受时将忽略parameter 。在中xgboost.train,提升迭代次数(即n_estimators)由num_boost_round(默认值:10)控制

在您的情况下,第一个代码将执行10次迭代(默认情况下),但是第二个代码将进行1000次迭代。如果您尝试将其更改clf = xg.train(params, dmatrix)clf = xg.train(params, dmatrix, 1000)

参考文献

http://xgboost.readthedocs.io/zh-CN/latest/python/python_api.html#xgboost.train

http://xgboost.readthedocs.io/en/latest/python/python_api.html#xgboost.XGBRegressor

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.