-
-
Notifications
You must be signed in to change notification settings - Fork 26.5k
Fix/ndcg score #22710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix/ndcg score #22710
Changes from all commits
5a6df33
689e72d
b628cb0
07a2447
ebce452
a32da19
7e2d9bb
7bb74cd
36eafd3
0e23aa0
9483840
20fcffa
c49cf4f
e559817
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1538,7 +1538,9 @@ def ndcg_score(y_true, y_score, *, k=None, sample_weight=None, ignore_ties=False | |||||
| ---------- | ||||||
| y_true : ndarray of shape (n_samples, n_labels) | ||||||
| True targets of multilabel classification, or true scores of entities | ||||||
| to be ranked. | ||||||
| to be ranked. Negative values in y_true may result in an output | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| that is not between 0 and 1. These negative values are deprecated, and | ||||||
| may cause an error in the future. | ||||||
|
|
||||||
| y_score : ndarray of shape (n_samples, n_labels) | ||||||
| Target scores, can either be probability estimates, confidence values, | ||||||
|
|
@@ -1616,11 +1618,26 @@ def ndcg_score(y_true, y_score, *, k=None, sample_weight=None, ignore_ties=False | |||||
| ... scores, k=1, ignore_ties=True) | ||||||
| 0.5 | ||||||
| """ | ||||||
|
|
||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For a cleaner git blame, try to revert any extraneous line additions/edits. |
||||||
| y_true = check_array(y_true, ensure_2d=False) | ||||||
| y_score = check_array(y_score, ensure_2d=False) | ||||||
| check_consistent_length(y_true, y_score, sample_weight) | ||||||
| _check_dcg_target_type(y_true) | ||||||
| gain = _ndcg_sample_scores(y_true, y_score, k=k, ignore_ties=ignore_ties) | ||||||
|
|
||||||
| if (isinstance(y_true, np.ndarray)): | ||||||
| if (y_true.min() < 0): | ||||||
| warnings.warn( | ||||||
| "ndcg_score should not use negative y_true values", | ||||||
| DeprecationWarning, | ||||||
| ) | ||||||
| else: | ||||||
| for value in y_true: | ||||||
| if (value < 0): | ||||||
| warnings.warn( | ||||||
| "ndcg_score should not use negative y_true values", | ||||||
| DeprecationWarning, | ||||||
| ) | ||||||
|
Comment on lines
+1634
to
+1640
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The documentation states that
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This clause was for one of the tests that from it seemed did not use numpy arrays, but the regular one. I found that removing the else clause, causes these kinds of tests to fail. Unless I am misunderstanding the issue, it is necessary for some of tests to pass |
||||||
| return np.average(gain, weights=sample_weight) | ||||||
|
|
||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1650,6 +1650,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) | ||||||||||
|
Comment on lines
+1654
to
+1655
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you're trying to organize this into an array of shape
Suggested change
|
||||||||||
| 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) | ||||||||||
|
Comment on lines
+1661
to
+1662
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| 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) | ||||||||||
|
Comment on lines
+1666
to
+1667
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| with pytest.warns(None): | ||||||||||
| ndcg_score(y_true, y_score) | ||||||||||
|
|
||||||||||
| def test_ndcg_invariant(): | ||||||||||
| y_true = np.arange(70).reshape(7, 10) | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try to use `backticks` for parameters like
y_true. This should probably also mention the deprecation of negative values fory_true. I made a minor wording edit as well, but feel free to rephrase as you see fit :)