lenskit.metrics.ranking.NDCG#

class lenskit.metrics.ranking.NDCG(n=None, *, k=None, weight=LogRankWeight(), discount=None, gain=None)#

Bases: lenskit.metrics.ranking._base.ListMetric, lenskit.metrics.ranking._base.RankingMetricBase

Compute the normalized discounted cumulative gain [JarvelinKekalainen02].

Discounted cumultative gain is computed as:

\[\begin{align*} \mathrm{DCG}(L,u) & = \sum_{i=1}^{|L|} \frac{r_{ui}}{d(i)} \end{align*}\]

Unrated items are assumed to have a utility of 0; if no rating values are provided in the truth frame, item ratings are assumed to be 1.

This is then normalized as follows:

\[\begin{align*} \mathrm{nDCG}(L, u) & = \frac{\mathrm{DCG}(L,u)}{\mathrm{DCG}(L_{\mathrm{ideal}}, u)} \end{align*}\]

Note

Negative gains are clipped to zero before computing NDCG. This keeps the metric bounded between 0 and 1 and prevents cases where negative gains can lead to misleading positive scores due to cancellation effects.

Parameters:
  • n (int | None) – The maximum recommendation list length to consider (longer lists are truncated).

  • weight (lenskit.metrics.ranking._weighting.RankWeight) – The rank weighting to use.

  • discount (Discount | None) – The discount function to use. The default, base-2 logarithm, is the original function used by Järvelin and Kekäläinen [JarvelinKekalainen02]. It is deprecated in favor of the weight option.

  • gain (str | None) – The field on the test data to use for gain values. If None (the default), all items present in the test data have a gain of 1. If set to a string, it is the name of a field (e.g. 'rating'). In all cases, items not present in the truth data have a gain of 0.

  • k (int | None)

Stability:
Caller (see Stability Levels).
weight: lenskit.metrics.ranking._weighting.RankWeight#
discount: Discount | None#
gain: str | None#
property label#

The metric’s default label in output. The base implementation returns the class name by default.

measure_list(recs, test)#

Compute measurements for a single list.

Returns:

  • A float for simple metrics

  • Intermediate data for decomposed metrics

  • A dict mapping metric names to values for multi-metric classes

Parameters:
Return type:

float