我正在处理一个分类问题,我有一个不平衡的数据集,我有兴趣具有高精度。
因此,我想将XGBoost的目标函数更改为可以使我更加精确的事物。F_BETA分数似乎就是这样做的,但是我对此有问题:
model_xgbm = XGBClassifier(objective=fbeta_score)
random_search = RandomizedSearchCV(model_xgbm, param_distributions=param_dist, n_iter=n_iter_search,
scoring='average_precision')
这有效,但是我没有提供beta(我什至不确定它是如何工作的,因为beta是n强制性参数...)
model_xgbm = XGBClassifier(objective=fbeta_score(beta=0.5))
random_search = RandomizedSearchCV(model_xgbm, param_distributions=param_dist,
n_iter=n_iter_search,
scoring='average_precision')
这根本不起作用(" typeError:fbeta_score()至少需要3个参数(1个给定)")。但是,我在这里无法真正提供其他两个参数。
是否有解决方案而不复制或包装功能并粘贴为自定义目标?
编辑:我发现了一个可能有用的函数:make_param,但不幸的是我似乎无法正常工作:
model_xgbm = XGBClassifier(objective=make_scorer(fbeta_score, beta=0.5))
random_search = RandomizedSearchCV(model_xgbm, param_distributions=param_dist,
n_iter=n_iter_search,
scoring='precision')
但这也不起作用:" typeError:__call __()至少需要4个参数(3给定)"请注意,我不想将其用于模型选择:我希望它成为我XGBoost估算器的目标函数!因此,上述链接底部的示例对我不起作用。
edit2:好,所以实际上,问题似乎是Xgboost分类器期望我作为返回渐变和黑森的目标提供的函数...有人知道包装器是否知道包装器会这样做吗?
查看注释的这一部分
eval_metric : str, callable, optional
If a str, should be a built-in evaluation metric to use. See
doc/parameter.md. If callable, a custom evaluation metric. The call
signature is func(y_predicted, y_true) where y_true will be a
DMatrix object such that you may need to call the get_label
method. It must return a str, value pair where the str is a name
for the evaluation and value is the value of the evaluation
function. This objective is always minimized.
这实际上是错误的,因为您需要
func(y_true, y_predicted)
用于传递目标函数。
看来,如果您包裹f_beta_score
如下
def f_beta_wrapper(y_true, y_pred):
beta = 0.5
# note need to call .get_label() on y_true if using DMAtrix
return fbeta_score(y_pred, y_true, beta)
并将其传递。
它流过正确的bu,它达到了您提到的问题,即fbeta_score
返回float
,而不是您期望可以从中计算梯度的两个输出。更具体地
/usr/local/lib/python2.7/site-packages/xgboost/core.pyc in update(self,dtrain,b,Iteration,fobj)
807其他:
808 pred = self.predict(dtrain)
在这里810 self.Boost(Dtrain,Grad,Hess)
typeError:'numpy.float64'对象不是Itable
这是有道理的,因为objective
功能正在最小化,因此我们需要类似于最小化参数的输出,即梯度。