-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9baee8
commit c1195a7
Showing
3 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
"""Copyright 2019-2024, XGBoost contributors""" | ||
|
||
import os | ||
from typing import Generator | ||
|
||
import numpy as np | ||
import pytest | ||
import scipy.sparse | ||
from dask import dataframe as dd | ||
from distributed import Client, LocalCluster, Scheduler, Worker | ||
from distributed.utils_test import gen_cluster | ||
|
||
from xgboost import dask as dxgb | ||
from xgboost import testing as tm | ||
from xgboost.testing import dask as dtm | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def cluster() -> Generator: | ||
n_threads = os.cpu_count() | ||
assert n_threads is not None | ||
with LocalCluster( | ||
n_workers=2, threads_per_worker=n_threads // 2, dashboard_address=":0" | ||
) as dask_cluster: | ||
yield dask_cluster | ||
|
||
|
||
@pytest.fixture | ||
def client(cluster: LocalCluster) -> Generator: | ||
with Client(cluster) as dask_client: | ||
yield dask_client | ||
|
||
|
||
def test_dask_ranking(client: Client) -> None: | ||
dpath = "demo/rank/" | ||
mq2008 = tm.data.get_mq2008(dpath) | ||
data = [] | ||
for d in mq2008: | ||
if isinstance(d, scipy.sparse.csr_matrix): | ||
d[d == 0] = np.inf | ||
d = d.toarray() | ||
d[d == 0] = np.nan | ||
d[np.isinf(d)] = 0 | ||
data.append(dd.from_array(d, chunksize=32)) | ||
else: | ||
data.append(dd.from_array(d, chunksize=32)) | ||
|
||
( | ||
x_train, | ||
y_train, | ||
qid_train, | ||
x_test, | ||
y_test, | ||
qid_test, | ||
x_valid, | ||
y_valid, | ||
qid_valid, | ||
) = data | ||
qid_train = qid_train.astype(np.uint32) | ||
qid_valid = qid_valid.astype(np.uint32) | ||
qid_test = qid_test.astype(np.uint32) | ||
|
||
rank = dxgb.DaskXGBRanker( | ||
n_estimators=2500, eval_metric=["ndcg"], early_stopping_rounds=10 | ||
) | ||
rank.fit( | ||
x_train, | ||
y_train, | ||
qid=qid_train, | ||
eval_set=[(x_test, y_test), (x_train, y_train)], | ||
eval_qid=[qid_test, qid_train], | ||
verbose=True, | ||
) | ||
assert rank.n_features_in_ == 46 | ||
assert rank.best_score > 0.98 | ||
|
||
|
||
def test_no_group_split(client: Client) -> None: | ||
X_tr, q_tr, y_tr = dtm.make_ltr(client, 4096, 128, 4, 5) | ||
X_va, q_va, y_va = dtm.make_ltr(client, 1024, 128, 4, 5) | ||
|
||
ltr = dxgb.DaskXGBRanker(allow_group_split=False, n_estimators=32) | ||
ltr.fit( | ||
X_tr, | ||
y_tr, | ||
qid=q_tr, | ||
eval_set=[(X_tr, y_tr), (X_va, y_va)], | ||
eval_qid=[q_tr, q_va], | ||
verbose=True, | ||
) | ||
print(X_tr.shape, X_tr.columns) |