我试图使用sklearn的随机搜索CV函数来为回归任务的XGBoost选择超参数。这里是我的代码:
#搜索空间params_xgboost = { "learning_rate" : [0.05, 0.10, 0.15, 0.20, 0.25, 0.30], "max_depth" : [ 3, 4, 5, 6, 8, 10, 12, 15], "min_child_weight" : [ 1, 3, 5, 7 ], "gamma" : [ 0.0, 0.1, 0.2 , 0.3, 0.4 ], "colsample_bytree" : [ 0.3, 0.4, 0.5 , 0.7], 'n_estimators' : [5, 10, 15, 20, 25, 30, 35], 'objective': 'reg:squarederror' }model = XGBRegressor()random_search = RandomizedSearchCV(estimator = model, param_distributions = params_xgboost, n_iter = 100, cv = 5, verbose=1, random_state=42, scoring = 'neg_mean_squared_error', n_jobs = -1)#params glare probarandom_search.fit(X_transform, Y['dgp'])
我实在是搞不懂为什么会得到以下错误
Unknown objective function: `u`
XGBoostError: [16:46:53] /Users/runner/miniforge3/conda-bld/xgboost_1607604592557/work/src/objective/objective.cc:26: Unknown objective function: `u`Objective candidate: survival:aftObjective candidate: binary:hingeObjective candidate: multi:softmaxObjective candidate: multi:softprobObjective candidate: rank:pairwiseObjective candidate: rank:ndcgObjective candidate: rank:mapObjective candidate: reg:squarederrorObjective candidate: reg:squaredlogerrorObjective candidate: reg:logisticObjective candidate: reg:pseudohubererrorObjective candidate: binary:logisticObjective candidate: binary:logitrawObjective candidate: reg:linearObjective candidate: count:poissonObjective candidate: survival:coxObjective candidate: reg:gammaObjective candidate: reg:tweedieStack trace: [bt] (0) 1 libxgboost.dylib 0x00000001210ad23e dmlc::LogMessageFatal::~LogMessageFatal() + 110 [bt] (1) 2 libxgboost.dylib 0x00000001211a4bd7 xgboost::ObjFunction::Create(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, xgboost::GenericParameter const*) + 759 [bt] (2) 3 libxgboost.dylib 0x0000000121168d06 xgboost::LearnerConfiguration::ConfigureObjective(xgboost::LearnerTrainParam const&, std::__1::vector<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >*) + 1926 [bt] (3) 4 libxgboost.dylib 0x000000012115de1f xgboost::LearnerConfiguration::Configure() + 1247 [bt] (4) 5 libxgboost.dylib 0x000000012115e2e7 xgboost::LearnerImpl::UpdateOneIter(int, std::__1::shared_ptr<xgboost::DMatrix>) + 119 [bt] (5) 6 libxgboost.dylib 0x00000001210b1e5c XGBoosterUpdateOneIter + 156 [bt] (6) 7 libffi.7.dylib 0x0000000107c40ead ffi_call_unix64 + 85 [bt] (7) 8 ??? 0x00007ffee8691a00 0x0 + 140732797622784
我在另一个分类任务中运行了相同的代码,使用multi:softmax
目标函数运行良好,所以我不确定为什么在上述情况下会得到这个错误。
回答:
objective='reg:squarederror'
是默认值,所以你可以安全地省略它:
XGBRegressor?
Init signature: XGBRegressor(objective='reg:squarederror', **kwargs)Docstring: Implementation of the scikit-learn API for XGBoost regression.
如果你希望明确指定它,你可以始终这样做:
XGBRegressor(objective='reg:squarederror'...)
同时请注意关于sklearn API的**kwargs
的说明(文档):
**kwargs不被scikit-learn支持。我们不能保证通过此参数传递的参数会与scikit-learn正确交互。