Skip to content

Commit ad34df1

Browse files
committed
Tests.
1 parent 2ce97b5 commit ad34df1

File tree

5 files changed

+61
-25
lines changed

5 files changed

+61
-25
lines changed

python-package/xgboost/testing/data.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,7 @@ class ClickFold:
361361

362362

363363
class RelDataCV(NamedTuple):
364-
"""Simple data struct for holding a train-test split of a learning to rank dataset.
365-
366-
"""
364+
"""Simple data struct for holding a train-test split of a learning to rank dataset."""
367365

368366
train: RelData
369367
test: RelData

python-package/xgboost/testing/metrics.py

+53-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,61 @@
11
"""Tests for evaluation metrics."""
2-
from typing import Dict
2+
from typing import Dict, List
33

44
import numpy as np
5+
import pytest
56

67
import xgboost as xgb
8+
from xgboost.compat import concat
9+
from xgboost.core import _parse_eval_str
10+
11+
12+
def check_precision_score(tree_method: str) -> None:
13+
"""Test for precision with ranking and classification."""
14+
datasets = pytest.importorskip("sklearn.datasets")
15+
16+
X, y = datasets.make_classification(
17+
n_samples=1024, n_features=4, n_classes=2, random_state=2023
18+
)
19+
qid = np.zeros(shape=y.shape) # same group
20+
21+
ltr = xgb.XGBRanker(n_estimators=2, tree_method=tree_method)
22+
ltr.fit(X, y, qid=qid)
23+
24+
# re-generate so that XGBoost doesn't evaluate the result to 1.0
25+
X, y = datasets.make_classification(
26+
n_samples=512, n_features=4, n_classes=2, random_state=1994
27+
)
28+
29+
ltr.set_params(eval_metric="pre@32")
30+
result = _parse_eval_str(
31+
ltr.get_booster().eval_set(evals=[(xgb.DMatrix(X, y), "Xy")])
32+
)
33+
score_0 = result[1][1]
34+
35+
X_list = []
36+
y_list = []
37+
n_query_groups = 3
38+
q_list: List[np.ndarray] = []
39+
for i in range(n_query_groups):
40+
# same for all groups
41+
X, y = datasets.make_classification(
42+
n_samples=512, n_features=4, n_classes=2, random_state=1994
43+
)
44+
X_list.append(X)
45+
y_list.append(y)
46+
q = np.full(shape=y.shape, fill_value=i, dtype=np.uint64)
47+
q_list.append(q)
48+
49+
qid = concat(q_list)
50+
X = concat(X_list)
51+
y = concat(y_list)
52+
53+
result = _parse_eval_str(
54+
ltr.get_booster().eval_set(evals=[(xgb.DMatrix(X, y, qid=qid), "Xy")])
55+
)
56+
assert result[1][0].endswith("pre@32")
57+
score_1 = result[1][1]
58+
assert score_1 == score_0
759

860

961
def check_quantile_error(tree_method: str) -> None:

tests/ci_build/lint_python.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def check_cmd_print_failure_assistance(cmd: List[str]) -> bool:
9292

9393
subprocess.run([cmd[0], "--version"])
9494
msg = """
95-
Please run the following command on your machine to address the formatting error:
95+
Please run the following command on your machine to address the error:
9696
9797
"""
9898
msg += " ".join(cmd)

tests/python-gpu/test_gpu_eval_metrics.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import xgboost
77
from xgboost import testing as tm
8-
from xgboost.testing.metrics import check_quantile_error
8+
from xgboost.testing.metrics import check_precision_score, check_quantile_error
99

1010
sys.path.append("tests/python")
1111
import test_eval_metrics as test_em # noqa
@@ -59,6 +59,9 @@ def test_pr_auc_multi(self):
5959
def test_pr_auc_ltr(self):
6060
self.cpu_test.run_pr_auc_ltr("gpu_hist")
6161

62+
def test_precision_score(self):
63+
check_precision_score("gpu_hist")
64+
6265
@pytest.mark.skipif(**tm.no_sklearn())
6366
def test_quantile_error(self) -> None:
6467
check_quantile_error("gpu_hist")

tests/python/test_eval_metrics.py

+2-19
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import xgboost as xgb
55
from xgboost import testing as tm
6-
from xgboost.testing.metrics import check_quantile_error
6+
from xgboost.testing.metrics import check_precision_score, check_quantile_error
77

88
rng = np.random.RandomState(1337)
99

@@ -318,24 +318,7 @@ def test_pr_auc_ltr(self):
318318
self.run_pr_auc_ltr("hist")
319319

320320
def test_precision_score(self):
321-
from sklearn.metrics import precision_score
322-
from sklearn.datasets import make_classification
323-
324-
x, y = make_classification(n_samples=128, n_features=4, n_classes=2)
325-
qid = np.zeros(shape=y.shape) # same group
326-
327-
ltr = xgb.XGBRanker()
328-
ltr.fit(x, y, qid=qid)
329-
p = ltr.predict(x)
330-
sorted_idx = np.argsort(p)
331-
332-
score_0 = precision_score(y, y[sorted_idx])
333-
print(score_0)
334-
335-
Xy = xgb.DMatrix(x, y)
336-
ltr.set_params(eval_metric="pre")
337-
e = ltr.get_booster().eval_set(evals=[(Xy, "Xy")])
338-
print(e)
321+
check_precision_score("hist")
339322

340323
@pytest.mark.skipif(**tm.no_sklearn())
341324
def test_quantile_error(self) -> None:

0 commit comments

Comments
 (0)