8000 Merge pull request #2 from trinhcon/fix/ndcg-score-with-test · Micky774/scikit-learn@07a2447 · GitHub
[go: up one dir, main page]

Skip to content

Commit 07a2447

Browse files
authored
Merge pull request #2 from trinhcon/fix/ndcg-score-with-test
deprecation warning for negative ndcg
2 parents 689e72d + b628cb0 commit 07a2447

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

sklearn/metrics/_ranking.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,11 +1622,17 @@ def ndcg_score(y_true, y_score, *, k=None, sample_weight=None, ignore_ties=False
16221622

16231623
if (isinstance(y_true, np.ndarray)):
16241624
if (y_true.min() < 0):
1625-
raise DeprecationWarning("ndcg_score should not use negative y_true values")
1625+
warnings.warn(
1626+
"ndcg_score should not use negative y_true values",
1627+
DeprecationWarning,
1628+
)
16261629
else:
16271630
for value in y_true:
16281631
if (value < 0):
1629-
raise DeprecationWarning("ndcg_score should not use negative y_true values")
1632+
warnings.warn(
1633+
"ndcg_score should not use negative y_true values",
1634+
DeprecationWarning,
1635+
)
16301636
return np.average(gain, weights=sample_weight)
16311637

16321638

sklearn/metrics/tests/test_ranking.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,23 @@ def test_ndcg_ignore_ties_with_k():
16401640
ndcg_score(a, a, k=3, ignore_ties=True)
16411641
)
16421642

1643+
def test_ndcg_negative_ndarray_warn():
1644+
y_true = np.array([-0.89, -0.53, -0.47, 0.39, 0.56]).reshape(1,-1)
1645+
y_score = np.array([0.07,0.31,0.75,0.33,0.27]).reshape(1,-1)
1646+
expected_message = "ndcg_score should not use negative y_true values"
1647+
with pytest.warns(DeprecationWarning, match=expected_message):
1648+
ndcg_score(y_true, y_score)
1649+
1650+
def test_ndcg_negative_output():
1651+
y_true = np.array([-0.89, -0.53, -0.47, 0.39, 0.56]).reshape(1,-1)
1652+
y_score = np.array([0.07,0.31,0.75,0.33,0.27]).reshape(1,-1)
1653+
assert ndcg_score(y_true, y_score) == pytest.approx(396.0329)
1654+
1655+
def test_ndcg_positive_ndarray():
1656+
y_true = np.array([0.11, 0.47, 0.53, 1.39, 1.56]).reshape(1,-1)
1657+
y_score = np.array([1.07, 1.31, 1.75, 1.33, 1.27]).reshape(1,-1)
1658+
with pytest.warns(None):
1659+
ndcg_score(y_true, y_score)
16431660

16441661
def test_ndcg_invariant():
16451662
y_true = np.arange(70).reshape(7, 10)

0 commit comments

Comments
 (0)
0