code: def ndcg(recs, truth, discount=np.log2, k=None):
      """
      Compute the normalized discounted cumulative gain :cite:p:`ndcg`.
  
      Discounted cumultative gain is computed as:
  
      .. math::
          \\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:
  
      .. math::
          \\begin{align*}
          \\mathrm{nDCG}(L, u) & = \\frac{\\mathrm{DCG}(L,u)}{\\mathrm{DCG}(L_{\\mathrm{ideal}}, u)}
          \\end{align*}
  
      This metric has a bulk implementation.
  
      Args:
          recs: The recommendation list.
          truth: The user's test data.
          discount(numpy.ufunc):
              The rank discount function.  Each item's score will be divided the discount of its rank,
              if the discount is greater than 1.
          k(int):
              The maximum list length.
      """
  
      if k is not None:
          recs = recs.iloc[:k]
  
      tpos = truth.index.get_indexer(recs["item"])
  
      if "rating" in truth.columns:
          i_rates = np.sort(truth.rating.values)[::-1]
          if k is not None:
              i_rates = i_rates[:k]
          ideal = _dcg(i_rates, discount)
          # make an array of ratings for this rec list
          r_rates = truth["rating"].values[tpos]
          r_rates[tpos < 0] = 0
          achieved = _dcg(r_rates, discount)
      else:
          ntrue = len(truth)
          if k is not None and ntrue > k:
              ntrue = k
          ideal = _fixed_dcg(ntrue, discount)
          tgood = tpos >= 0
          achieved = _dcg(tgood, discount)
  
      return achieved / ideal
  
file_path: lkpy/lenskit/metrics/topn.py
imports: import logging
  import warnings
  import numpy
  import pandas
label_desc: Use image node_id 13 for calling a global function with example usage: ndcg(recs, truth, discount, k) and returns: unknown