ndcg_at_k#

scikitplot.corpus.ndcg_at_k(y_true, y_pred, *, k=10, gain='linear', sample_weight=None, relevance_threshold=1, duplicates='raise', zero_division='warn', average='mean')[source]#

Normalised discounted cumulative gain (nDCG@k) over graded judgments.

Parameters:
y_truesequence or mapping

Relevance judgments per query; grades are the gains.

y_predsequence or mapping

Rankings per query, best first.

kint or None, default=10

Cutoff of both DCG and the ideal DCG; None uses everything.

gain{“linear”, “exponential”}, default=”linear”

grade or 2 ** grade - 1.

sample_weightarray-like of shape (n_queries,), default=None

Per-query weights of the mean.

relevance_thresholdfloat, default=1

Accepted for a uniform signature; nDCG uses the grades, not the threshold.

duplicates{“raise”, “first”}, default=”raise”

What a repeated identifier in a ranking does.

zero_division{“warn”, 0.0, 1.0, np.nan}, default=”warn”

Value for a query whose judged grades are all zero.

average{“mean”, None}, default=”mean”

None returns the per-query values.

Returns:
float or numpy.ndarray

Values in [0, 1] (up to floating-point rounding).

Parameters:
  • y_true (Any)

  • y_pred (Any)

  • k (int | None)

  • gain (str)

  • sample_weight (Any)

  • relevance_threshold (float)

  • duplicates (str)

  • zero_division (Any)

  • average (str | None)

Return type:

Any

Notes

This takes rankings of identifiers and graded judgments, the form a retrieval system produces. sklearn.metrics.ndcg_score takes score matrices over a fixed label set instead.

Examples

>>> round(ndcg_at_k([{"a": 3, "b": 1}], [["b", "a"]], k=2), 4)
0.7967