Support sample weight in sklearn custom objective. (#10050)
This commit is contained in:
@@ -100,6 +100,7 @@ class LintersPaths:
|
||||
# demo
|
||||
"demo/json-model/json_parser.py",
|
||||
"demo/guide-python/external_memory.py",
|
||||
"demo/guide-python/sklearn_examples.py",
|
||||
"demo/guide-python/continuation.py",
|
||||
"demo/guide-python/callbacks.py",
|
||||
"demo/guide-python/cat_in_the_dat.py",
|
||||
|
||||
@@ -517,6 +517,12 @@ def test_regression_with_custom_objective():
|
||||
labels = y[test_index]
|
||||
assert mean_squared_error(preds, labels) < 25
|
||||
|
||||
w = rng.uniform(low=0.0, high=1.0, size=X.shape[0])
|
||||
reg = xgb.XGBRegressor(objective=tm.ls_obj, n_estimators=25)
|
||||
reg.fit(X, y, sample_weight=w)
|
||||
y_pred = reg.predict(X)
|
||||
assert mean_squared_error(y_true=y, y_pred=y_pred, sample_weight=w) < 25
|
||||
|
||||
# Test that the custom objective function is actually used
|
||||
class XGBCustomObjectiveException(Exception):
|
||||
pass
|
||||
|
||||
@@ -1750,9 +1750,20 @@ class TestWithDask:
|
||||
)
|
||||
tm.non_increasing(results_native["validation_0"]["rmse"])
|
||||
|
||||
reg = xgb.dask.DaskXGBRegressor(
|
||||
n_estimators=rounds, objective=tm.ls_obj, tree_method="hist"
|
||||
)
|
||||
rng = da.random.RandomState(1994)
|
||||
w = rng.uniform(low=0.0, high=1.0, size=y.shape[0])
|
||||
reg.fit(
|
||||
X, y, sample_weight=w, eval_set=[(X, y)], sample_weight_eval_set=[w]
|
||||
)
|
||||
results_custom = reg.evals_result()
|
||||
tm.non_increasing(results_custom["validation_0"]["rmse"])
|
||||
|
||||
def test_no_duplicated_partition(self) -> None:
|
||||
"""Assert each worker has the correct amount of data, and DMatrix initialization doesn't
|
||||
generate unnecessary copies of data.
|
||||
"""Assert each worker has the correct amount of data, and DMatrix initialization
|
||||
doesn't generate unnecessary copies of data.
|
||||
|
||||
"""
|
||||
with LocalCluster(n_workers=2, dashboard_address=":0") as cluster:
|
||||
|
||||
Reference in New Issue
Block a user