From b628cb06c3376498955b6645ef9c8778373b10c3 Mon Sep 17 00:00:00 2001 From: Victor Ko Date: Fri, 4 Mar 2022 01:33:56 -0500 Subject: [PATCH] deprecation warning for negative ndcg --- sklearn/metrics/_ranking.py | 10 ++++++++-- sklearn/metrics/tests/test_ranking.py | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/sklearn/metrics/_ranking.py b/sklearn/metrics/_ranking.py index 6653e4c433c08..d33dc0435c6b4 100644 --- a/sklearn/metrics/_ranking.py +++ b/sklearn/metrics/_ranking.py @@ -1622,11 +1622,17 @@ def ndcg_score(y_true, y_score, *, k=None, sample_weight=None, ignore_ties=False if (isinstance(y_true, np.ndarray)): if (y_true.min() < 0): - raise DeprecationWarning("ndcg_score should not use negative y_true values") + warnings.warn( + "ndcg_score should not use negative y_true values", + DeprecationWarning, + ) else: for value in y_true: if (value < 0): - raise DeprecationWarning("ndcg_score should not use negative y_true values") + warnings.warn( + "ndcg_score should not use negative y_true values", + DeprecationWarning, + ) return np.average(gain, weights=sample_weight) diff --git a/sklearn/metrics/tests/test_ranking.py b/sklearn/metrics/tests/test_ranking.py index 01de37b189733..dfd5dcac7aac6 100644 --- a/sklearn/metrics/tests/test_ranking.py +++ b/sklearn/metrics/tests/test_ranking.py @@ -1640,6 +1640,23 @@ def test_ndcg_ignore_ties_with_k(): ndcg_score(a, a, k=3, ignore_ties=True) ) +def test_ndcg_negative_ndarray_warn(): + y_true = np.array([-0.89, -0.53, -0.47, 0.39, 0.56]).reshape(1,-1) + y_score = np.array([0.07,0.31,0.75,0.33,0.27]).reshape(1,-1) + expected_message = "ndcg_score should not use negative y_true values" + with pytest.warns(DeprecationWarning, match=expected_message): + ndcg_score(y_true, y_score) + +def test_ndcg_negative_output(): + y_true = np.array([-0.89, -0.53, -0.47, 0.39, 0.56]).reshape(1,-1) + y_score = np.array([0.07,0.31,0.75,0.33,0.27]).reshape(1,-1) + assert ndcg_score(y_true, y_score) == pytest.approx(396.0329) + +def test_ndcg_positive_ndarray(): + y_true = np.array([0.11, 0.47, 0.53, 1.39, 1.56]).reshape(1,-1) + y_score = np.array([1.07, 1.31, 1.75, 1.33, 1.27]).reshape(1,-1) + with pytest.warns(None): + ndcg_score(y_true, y_score) def test_ndcg_invariant(): y_true = np.arange(70).reshape(7, 10)