简体   繁体   English

未调用 XGBoost fit() 自定义 eval_metric。 为什么?

[英]XGBoost fit() custom eval_metric is not called. Why?

I am passing a custom evaluation metric function to XGB fit(), but the Python API discards callable (ie, custom) functions.我将自定义评估指标 function 传递给 XGB fit(),但 Python API 丢弃了可调用(即自定义)函数。 Why?为什么? How are we supposed to pass in this function?我们应该如何传递这个 function? Or is this functionality not supported by the Python/Scikit API?还是 Python/Scikit API 不支持此功能?

model = XGBClassifier(scale_pos_weight=scale_pos_weight)
model.fit(X_train, y_train, eval_metric=xgb_f1)

In sklearn.py (line 755):在 sklearn.py(第 755 行)中:

def fit(self, X, y, sample_weight=None, base_margin=None,
            eval_set=None, eval_metric=None,
            early_stopping_rounds=None, verbose=True, xgb_model=None,
            sample_weight_eval_set=None, callbacks=None):
...
        feval = eval_metric if callable(eval_metric) else None
        if eval_metric is not None:
            if callable(eval_metric):
                eval_metric = None           # <<<<<<<<<< If eval_metric is callable, it's discarded
            else:
                xgb_options.update({"eval_metric": eval_metric})

If you take a deeper look into the source code your custom evaluation metric is indeed being used, in the code snippet that you posted above if you look at this line:如果您更深入地查看源代码,您的自定义评估指标确实正在使用,如果您查看此行,请在上面发布的代码片段中:

feval = eval_metric if callable(eval_metric) else None

your custom evaluation metric (which is a callable) is being assigned to this variable called feval which is then being passed on to the actual method, train , that does your fitting as done here .您的自定义评估指标(这是一个可调用的)被分配给这个名为feval的变量,然后将其传递给实际方法train ,该方法会按照此处完成的方式进行拟合。 And now when you look at the docstring of the train method you can find that feval corresponds to Customized evaluation function .现在,当您查看train方法的文档字符串时,您会发现feval对应于Customized evaluation function Since xgboost uses the term feval for referring to custom evaluation metric and sklearn uses the term eval_metric , the authors of xgboost retained the variable name as it is for ease of understanding and do this conversion internally.由于xgboost使用术语feval来指代自定义评估指标,而sklearn使用术语eval_metric ,因此 xgboost 的作者保留了变量名称,以便于理解并在内部进行此转换。

Hops this helps!啤酒花这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM