lenskit.metrics.NDCG#

class lenskit.metrics.NDCG(k=None, *, discount=<ufunc 'log2'>, gain=None)#

Bases: ListMetric, 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*}\]
Parameters:
  • k (int | None) – The maximum recommendation list length to consider (longer lists are truncated).

  • discount (Callable[[ndarray[Any, dtype[number]]], ndarray[Any, dtype[float64]]]) – The discount function to use. The default, base-2 logarithm, is the original function used by Järvelin and Kekäläinen [JarvelinKekalainen02].

  • 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.

__init__(k=None, *, discount=<ufunc 'log2'>, gain=None)#
Parameters:

Methods

__init__([k, discount, gain])

measure_list(recs, test)

Compute the metric value for a single result list.

truncate(items)

Truncate an item list if it is longer than k.

Attributes

default

The default value to infer when computing statistics over missing values.

k

The maximum length of rankings to consider.

label

The metric's default label in output.

discount

gain

property label#

The metric’s default label in output.

The base implementation returns the class name by default.

measure_list(recs, test)#

Compute the metric value for a single result list.

Individual metric classes need to implement this method.

Parameters:
Return type:

float