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

fix(deps): update dependency xgboost to v2 #70

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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