[go: nahoru, domu]

Skip to content
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

ENH Added warning for ndcg_score when used w/ negative y_true values #23461

Merged
merged 31 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
5a6df33
fix: DeprecationWarning for negative y_true values in ndcg_score
trinhcon Feb 27, 2022
689e72d
fixed for checking np.ndarrays and regular arrays
trinhcon Mar 2, 2022
b628cb0
deprecation warning for negative ndcg
VKo232 Mar 4, 2022
07a2447
Merge pull request #2 from trinhcon/fix/ndcg-score-with-test
trinhcon Mar 4, 2022
ebce452
updated the docstrings and changed exception to warning
trinhcon Mar 4, 2022
a32da19
Merge branch 'fix/ndcg-score' of https://github.com/trinhcon/scikit-l…
trinhcon Mar 4, 2022
7e2d9bb
Created a changelog entry
trinhcon Mar 6, 2022
7bb74cd
Edited a changelog entry with pr number
trinhcon Mar 6, 2022
36eafd3
Fixed changelog entry formatting
trinhcon Mar 6, 2022
0e23aa0
Fixed changelog entry formatting
trinhcon Mar 6, 2022
9483840
Merge branch 'main' of https://github.com/scikit-learn/scikit-learn i…
trinhcon Mar 6, 2022
20fcffa
Merge branch 'scikit-learn-main' into fix/ndcg-score
trinhcon Mar 6, 2022
c49cf4f
Moved changelog entry to appropriate location
trinhcon Mar 6, 2022
e559817
Fixed changelog entry formatting
trinhcon Mar 6, 2022
850bdf6
Merge branch 'main' into ndcg-score
Micky774 May 25, 2022
0bfd0a1
Updated emitted warning, fixed changelogs, updated tests
Micky774 May 25, 2022
910fd43
Reimplemented deprecation warning and added TODO comments
Micky774 May 25, 2022
548371a
Added missing PR number
Micky774 May 25, 2022
5a38ce4
Streamline test
Micky774 May 25, 2022
986f91c
Merge branch 'main' into ndcg-score
Micky774 May 26, 2022
df1b3dc
Merge branch 'main' into ndcg-score
Micky774 May 30, 2022
da6295f
Merge branch 'main' into ndcg-score
Micky774 May 31, 2022
a19312c
Merge branch 'main' into ndcg-score
Micky774 May 31, 2022
a2ce07e
Merge branch 'main' into ndcg-score
Micky774 Jun 1, 2022
88118e9
Merge branch 'main' into ndcg-score
Micky774 Jun 6, 2022
273ac2b
Incorporated review feedback
Micky774 Jun 6, 2022
e9b5d62
Apply suggestions from code review
Micky774 Jun 7, 2022
180724c
Merge branch 'main' into ndcg-score
Micky774 Jun 7, 2022
b65e7b2
Updated test
Micky774 Jun 7, 2022
85f1639
Update sklearn/metrics/_ranking.py
Micky774 Jun 7, 2022
5261413
Update v1.2.rst
jeremiedbb Jun 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion doc/whats_new/v1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,17 @@ Changelog
:mod:`sklearn.metrics`
......................

- |Feature| :func:`class_likelihood_ratios` is added to compute the positive and
- |Feature| :func:`metrics.class_likelihood_ratios` is added to compute the positive and
negative likelihood ratios derived from the confusion matrix
of a binary classification problem. :pr:`22518` by
:user:`Arturo Amor <ArturoAmorQ>`.

- |Fix| :func:`metrics.ndcg_score` will now trigger a warning when the `y_true`
value contains a negative value. Users may still use negative values, but the
result may not be between 0 and 1. Starting in v1.4, passing in negative
values for `y_true` will raise an error.
:pr:`22710` by :user:`Conroy Trinh <trinhcon>` and
:pr:`23461` by :user:`Meekail Zain <micky774>`.

:mod:`sklearn.neighbors`
........................
Expand Down
14 changes: 13 additions & 1 deletion sklearn/metrics/_ranking.py
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,11 @@ 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
that is not between 0 and 1.

.. versionchanged:: 1.2
These negative values are deprecated, and will raise an error in v1.4.

y_score : ndarray of shape (n_samples, n_labels)
Target scores, can either be probability estimates, confidence values,
Expand Down Expand Up @@ -1636,6 +1640,14 @@ def ndcg_score(y_true, y_score, *, k=None, sample_weight=None, ignore_ties=False
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)

if y_true.min() < 0:
Micky774 marked this conversation as resolved.
Show resolved Hide resolved
# TODO(1.4): Replace warning w/ ValueError
warnings.warn(
"ndcg_score should not be used on negative y_true values. ndcg_score will"
" raise a ValueError on negative y_true values starting from version 1.4.",
FutureWarning,
)
_check_dcg_target_type(y_true)
gain = _ndcg_sample_scores(y_true, y_score, k=k, ignore_ties=ignore_ties)
return np.average(gain, weights=sample_weight)
Expand Down
12 changes: 12 additions & 0 deletions sklearn/metrics/tests/test_ranking.py
Original file line number Diff line number Diff line change
Expand Up @@ -1694,6 +1694,18 @@ def test_ndcg_ignore_ties_with_k():
)


# TODO(1.4): Replace warning w/ ValueError
def test_ndcg_negative_ndarray_warn():
y_true = np.array([[-0.89, -0.53, -0.47, 0.39, 0.56]])
y_score = np.array([[0.07, 0.31, 0.75, 0.33, 0.27]])
expected_message = (
"ndcg_score should not be used on negative y_true values. ndcg_score will raise"
" a ValueError on negative y_true values starting from version 1.4."
)
with pytest.warns(FutureWarning, match=expected_message):
assert ndcg_score(y_true, y_score) == pytest.approx(396.0329)


def test_ndcg_invariant():
y_true = np.arange(70).reshape(7, 10)
y_score = y_true + np.random.RandomState(0).uniform(-0.2, 0.2, size=y_true.shape)
Expand Down