Skip to content

Commit

Permalink
fix(deps): update dependency xgboost to v2 (#70)
Browse files Browse the repository at this point in the history
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: 34j <55338215+34j@users.noreply.github.com>
  • Loading branch information
renovate[bot] and 34j authored Oct 11, 2023
1 parent 6443e32 commit 856bdbb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
11 changes: 3 additions & 8 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ packages = [

[tool.poetry.dependencies]
python = "^3.8"
xgboost = "^1.7.5"
xgboost = "^2.0.0"
lightgbm = "^3.3.5"
catboost = "^1.2"
pyhumps = "^3.8.0"
Expand Down
14 changes: 12 additions & 2 deletions src/boost_loss/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ def _dataset_to_ndarray(
return y_, weight
if isinstance(y, xgb.DMatrix):
y_ = y.get_label()
return y_, np.ones_like(y_)
weight = y.get_weight()
if weight is None or weight.size == 0:
weight = np.ones_like(y_)
return y_, weight
return y, np.ones_like(y)


Expand Down Expand Up @@ -340,12 +343,16 @@ def eval_metric_lgb(
self,
y_true: NDArray | lgb.Dataset | xgb.DMatrix,
y_pred: NDArray | lgb.Dataset | xgb.DMatrix,
sample_weight: NDArray | lgb.Dataset | xgb.DMatrix | None = None
# not used, exists for eval_metric_xgb_sklearn
) -> tuple[str, float, bool]:
"""LightGBM-compatible interface"""
if isinstance(y_pred, lgb.Dataset) or isinstance(y_pred, xgb.DMatrix):
# NOTE: swap (it is so fucking that the order is inconsistent)
y_true, y_pred = y_pred, y_true
y_true, weight = _dataset_to_ndarray(y=y_true)
if sample_weight is not None:
weight = sample_weight
y_pred, _ = _dataset_to_ndarray(y=y_pred)
loss = self.loss(y_true=y_true, y_pred=y_pred)
if isinstance(loss, float) and not np.allclose(weight, 1.0):
Expand All @@ -372,9 +379,12 @@ def eval_metric_xgb_sklearn(
self,
y_true: NDArray | lgb.Dataset | xgb.DMatrix,
y_pred: NDArray | lgb.Dataset | xgb.DMatrix,
sample_weight: NDArray | lgb.Dataset | xgb.DMatrix | None = None,
) -> float:
"""XGBoost-sklearn-api-compatible interface"""
result = self.eval_metric_lgb(y_true=y_true, y_pred=y_pred)
result = self.eval_metric_lgb(
y_true=y_true, y_pred=y_pred, sample_weight=sample_weight
)
return result[1]

def __add__(self, other: LossBase) -> LossBase:
Expand Down
1 change: 1 addition & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def xgboost_baseline(self):
raise SkipTest(f"XGBoost does not support {self.loss_name} loss.")
model = xgb.XGBRegressor(
objective=self.loss_names["xgboost"][self.loss_name],
base_score=0.5,
)
model.fit(
self.X_train,
Expand Down

0 comments on commit 856bdbb

Please sign in to comment.