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

[MRG+1] Add Normalized Discounted Cumulative Gain #9951

Merged
merged 67 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
4f70065
add DCG and NDCG
jeromedockes Oct 18, 2017
0e06a80
add wikipedia links for ndcg
jeromedockes Oct 18, 2017
3542fcd
missing blank line
jeromedockes Oct 18, 2017
7e5190e
fix ndcg test
jeromedockes Oct 18, 2017
f78f8cb
undo unrelated change to metrics/__init__.py
jeromedockes Oct 19, 2017
776ed36
improve dcg and ndcg docstrings
jeromedockes Oct 19, 2017
17ca372
use check_array and check_consistent_length
jeromedockes Oct 19, 2017
dbf3826
add dcg and ndcg to test_common thresholded metrics
jeromedockes Oct 19, 2017
f9d741f
backport np.unique with return_counts in sklearn.utils.fixes
johny-c Oct 22, 2017
140a682
add a test function in utils.tests.test_fixes.py
johny-c Oct 22, 2017
ad71190
change link to correct numpy commit
johny-c Oct 24, 2017
c3552f0
unused import
jeromedockes Nov 6, 2017
ca48505
remove references in private functions' docstrings
jeromedockes Nov 19, 2017
560ccd3
add test dcg and ndcg on toy examples
jeromedockes Nov 21, 2017
d68ee1a
add examples to ndcg_score and dcg_score docstrings
jeromedockes Nov 21, 2017
aa4bd9c
fix ndcg_score doctests
jeromedockes Nov 21, 2017
e6c2c01
average ties in ndcg
jeromedockes Jan 3, 2018
1bc867e
faster dcg tie averaging
jeromedockes Jan 3, 2018
f395ba0
Merge branch 'master' into add_ndcg
jeromedockes Mar 5, 2018
15bbb4f
add test dcg ties
jeromedockes Mar 5, 2018
934969e
Merge remote-tracking branch 'johny_c/backport-np-unique' into add_ndcg
jeromedockes Mar 5, 2018
cfc2309
use backport for unique with return_counts
jeromedockes Mar 5, 2018
5d0b9a6
test ndcg invariant if order preserved
jeromedockes Mar 5, 2018
abd579f
check translation invariant
jeromedockes Mar 5, 2018
2bb30b2
add reference for tied ndcg
jeromedockes Mar 7, 2018
a9d8368
add narrative doc for ndcg
jeromedockes Mar 8, 2018
fb0b667
Merge branch 'master' into add_ndcg
jeromedockes Apr 23, 2018
2c996e9
improve ndcg documentation
jeromedockes May 7, 2018
09cf1b5
plain assert instead of assert_true in ndcg tests
jeromedockes May 7, 2018
df1df69
Merge branch 'master' into add_ndcg
jeromedockes May 7, 2018
4517f06
move checks to public dcg/ndcg functions
jeromedockes May 7, 2018
97677ee
fix key error in metrics/test_common.py
jeromedockes May 7, 2018
f908fef
check target type in ndcg (not only dcg)
jeromedockes May 7, 2018
981783c
add ignore_ties option to dcg and ndcg for performance
jeromedockes May 17, 2018
af81310
improve examples in dcg and ndcg docstrings
jeromedockes May 17, 2018
5bf100a
Merge remote-tracking branch 'upstream/master' into add_ndcg
jeromedockes May 17, 2018
6059a7c
docstring details
jeromedockes May 17, 2018
c545c63
unused variable
jeromedockes May 17, 2018
d865cea
add ndcg to whatsnew
jeromedockes May 17, 2018
75f4515
Merge branch 'master' into add_ndcg
jeromedockes May 19, 2018
9ba639b
Merge branch 'master' into add_ndcg
jeromedockes Jun 7, 2018
66f63a3
more details about DCG in documentation
jeromedockes Jun 7, 2018
371ab30
Merge remote-tracking branch 'upstream/master' into add_ndcg
jeromedockes Feb 14, 2019
fc36f39
flake8 ignore unused import in fixes
jeromedockes Feb 14, 2019
c894380
address https://github.com/scikit-learn/scikit-learn/pull/9951#discus…
jeromedockes Feb 15, 2019
988546b
remove examples with perfect prediction from dcg docstrings
jeromedockes Feb 15, 2019
306cb88
more details in ndcg docs + comparison with ranking loss
jeromedockes Feb 15, 2019
1feef33
remove np.unique from utils.fixes
jeromedockes Mar 19, 2019
5d3c5a6
Merge remote-tracking branch 'upstream/master' into add_ndcg
jeromedockes Mar 19, 2019
ea0f545
remove tests made obsolete by numpy version bump
jeromedockes Mar 19, 2019
b396471
Merge remote-tracking branch 'upstream/master' into add_ndcg
jeromedockes Apr 24, 2019
e77eb64
Merge remote-tracking branch 'upstream/master' into add_ndcg
jeromedockes Jun 5, 2019
b763abe
update whatsnew
jeromedockes Jun 5, 2019
c7e2c19
Merge remote-tracking branch 'upstream/master' into add_ndcg
jeromedockes Jul 16, 2019
81ac36c
Merge remote-tracking branch 'upstream/master' into add_ndcg
jeromedockes Jul 19, 2019
ba2c07c
address comments on doc
jeromedockes Jul 19, 2019
96233cb
revert change unrelated to NDCG
jeromedockes Jul 19, 2019
67f4dc3
add docstring for _tie_averaged_dcg
jeromedockes Jul 22, 2019
00c2d2a
log_basis -> log_base
jeromedockes Jul 25, 2019
450065c
performance -> efficiency
jeromedockes Jul 25, 2019
11574ce
comment about ignoring ties when computing normalization in ndcg
jeromedockes Jul 25, 2019
ccc0875
np.zeros -> np.empty
jeromedockes Jul 25, 2019
3d4f2cc
parametrize test_ndcg_toy_examples with ignore_ties
jeromedockes Jul 25, 2019
cb8a6b4
check same results with k and ignore_ties in the absence of ties
jeromedockes Jul 25, 2019
3d6d286
pep8
jeromedockes Jul 25, 2019
24413d8
Merge branch 'master' into add_ndcg
glemaitre Aug 12, 2019
0cafb21
Merge remote-tracking branch 'origin/master' into add_ndcg
glemaitre Aug 12, 2019
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
61 changes: 61 additions & 0 deletions doc/modules/model_evaluation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1664,6 +1664,67 @@ Here is a small example of usage of this function::
* Tsoumakas, G., Katakis, I., & Vlahavas, I. (2010). Mining multi-label data. In
Data mining and knowledge discovery handbook (pp. 667-685). Springer US.

.. _ndcg:

Normalized Discounted Cumulative Gain
-------------------------------------

Discounted Cumulative Gain (DCG) and Normalized Discounted Cumulative Gain
(NDCG) are ranking metrics; they compare a predicted order to ground-truth
scores, such as the relevance of answers to a query.

from the Wikipedia page for Discounted Cumulative Gain:

"Discounted cumulative gain (DCG) is a measure of ranking quality. In
information retrieval, it is often used to measure effectiveness of web search
engine algorithms or related applications. Using a graded relevance scale of
documents in a search-engine result set, DCG measures the usefulness, or gain,
of a document based on its position in the result list. The gain is accumulated
from the top of the result list to the bottom, with the gain of each result
discounted at lower ranks"

DCG orders the true targets (e.g. relevance of query answers) in the predicted
order, then multiplies them by a logarithmic decay and sums the result. The sum
can be truncated after the first :math:`K` results, in which case we call it
DCG@K.
NDCG, or NDCG@K is DCG divided by the DCG obtained by a perfect prediction, so
that it is always between 0 and 1. Usually, NDCG is preferred to DCG.

Compared with the ranking loss, NDCG can take into account relevance scores,
rather than a ground-truth ranking. So if the ground-truth consists only of an
ordering, the ranking loss should be preferred; if the ground-truth consists of
actual usefulness scores (e.g. 0 for irrelevant, 1 for relevant, 2 for very
relevant), NDCG can be used.

For one sample, given the vector of continuous ground-truth values for each
target :math:`y \in \mathbb{R}^{M}`, where :math:`M` is the number of outputs, and
the prediction :math:`\hat{y}`, which induces the ranking funtion :math:`f`, the
jeremiedbb marked this conversation as resolved.
Show resolved Hide resolved
DCG score is

.. math::
\sum_{r=1}^{\min(K, M)}\frac{y_{f(r)}}{\log(1 + r)}

and the NDCG score is the DCG score divided by the DCG score obtained for
:math:`y`.

.. topic:: References:

* Wikipedia entry for Discounted Cumulative Gain:
https://en.wikipedia.org/wiki/Discounted_cumulative_gain

* Jarvelin, K., & Kekalainen, J. (2002).
Cumulated gain-based evaluation of IR techniques. ACM Transactions on
Information Systems (TOIS), 20(4), 422-446.

* Wang, Y., Wang, L., Li, Y., He, D., Chen, W., & Liu, T. Y. (2013, May).
A theoretical analysis of NDCG ranking measures. In Proceedings of the 26th
Annual Conference on Learning Theory (COLT 2013)

* McSherry, F., & Najork, M. (2008, March). Computing information retrieval
performance measures efficiently in the presence of tied scores. In
European conference on information retrieval (pp. 414-421). Springer,
Berlin, Heidelberg.

.. _regression_metrics:

Regression metrics
Expand Down
5 changes: 5 additions & 0 deletions doc/whats_new/v0.22.rst
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ Changelog
:mod:`sklearn.metrics`
......................

- |Feature| New ranking metrics :func:`metrics.ndcg_score` and
:func:`metrics.dcg_score` have been added to compute Discounted Cumulative
Gain and Normalized Discounted Cumulative Gain. :pr:`9951` by :user:`Jérôme
Dockès <jeromedockes>`.

- |MajorFeature| :func:`metrics.plot_roc_curve` has been added to plot roc
curves. This function introduces the visualization API described in
the :ref:`User Guide <visualizations>`. :pr:`14357` by `Thomas Fan`_.
Expand Down
4 changes: 4 additions & 0 deletions sklearn/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
from .ranking import auc
from .ranking import average_precision_score
from .ranking import coverage_error
from .ranking import dcg_score
from .ranking import label_ranking_average_precision_score
from .ranking import label_ranking_loss
from .ranking import ndcg_score
from .ranking import precision_recall_curve
from .ranking import roc_auc_score
from .ranking import roc_curve
Expand Down Expand Up @@ -95,6 +97,7 @@
'confusion_matrix',
'consensus_score',
'coverage_error',
'dcg_score',
'davies_bouldin_score',
'euclidean_distances',
'explained_variance_score',
Expand Down Expand Up @@ -123,6 +126,7 @@
'median_absolute_error',
'multilabel_confusion_matrix',
'mutual_info_score',
'ndcg_score',
'normalized_mutual_info_score',
'pairwise_distances',
'pairwise_distances_argmin',
Expand Down
Loading