Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sample_weight to eval_metric #8706

Merged
merged 3 commits into from
Feb 4, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions python-package/xgboost/sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,24 @@ def inner(y_score: np.ndarray, dmatrix: DMatrix) -> Tuple[str, float]:

return inner

def _sample_weight_metric_decorator(func: Callable) -> Metric:
"""Decorate a metric function from sklearn.

Converts an metric function that uses the typical sklearn metric signature so that it
is compatible with :py:func:`train`

"""

def inner(y_score: np.ndarray, dmatrix: DMatrix) -> Tuple[str, float]:
y_true = dmatrix.get_label()
sample_weight = dmatrix.get_weight()
try:
return func.__name__, func(y_true, y_score, sample_weight=sample_weight)
except TypeError:
#func has no sample_weight
return func.__name__, func(y_true, y_score)

return inner

__estimator_doc = """
n_estimators : int
Expand Down Expand Up @@ -819,6 +837,7 @@ def _configure_fit(
params: Dict[str, Any],
early_stopping_rounds: Optional[int],
callbacks: Optional[Sequence[TrainingCallback]],
sample_weight_metric: Optional[bool],
) -> Tuple[
Optional[Union[Booster, str, "XGBModel"]],
Optional[Metric],
Expand Down Expand Up @@ -865,7 +884,10 @@ def _duplicated(parameter: str) -> None:
metric = eval_metric
elif callable(eval_metric):
# Parameter from constructor or set_params
metric = _metric_decorator(eval_metric)
if sample_weight_metric:
metric = _sample_weight_metric_decorator(eval_metric)
else:
metric = _metric_decorator(eval_metric)
else:
params.update({"eval_metric": eval_metric})

Expand Down Expand Up @@ -1029,7 +1051,7 @@ def fit(
early_stopping_rounds,
callbacks,
) = self._configure_fit(
xgb_model, eval_metric, params, early_stopping_rounds, callbacks
xgb_model, eval_metric, params, early_stopping_rounds, callbacks, sample_weight != None
)
self._Booster = train(
params,
Expand Down Expand Up @@ -1475,7 +1497,7 @@ def fit(
early_stopping_rounds,
callbacks,
) = self._configure_fit(
xgb_model, eval_metric, params, early_stopping_rounds, callbacks
xgb_model, eval_metric, params, early_stopping_rounds, callbacks, sample_weight != None
)
train_dmatrix, evals = _wrap_evaluation_matrices(
missing=self.missing,
Expand Down Expand Up @@ -1974,7 +1996,7 @@ def fit(
early_stopping_rounds,
callbacks,
) = self._configure_fit(
xgb_model, eval_metric, params, early_stopping_rounds, callbacks
xgb_model, eval_metric, params, early_stopping_rounds, callbacks, sample_weight != None
)
if callable(metric):
raise ValueError(
Expand Down