class_docstr: Computes NDCG (normalized discounted cumulative gain).
class_docstr_tok: ["Computes","NDCG","(","normalized","discounted","cumulative","gain",")","."]
docstr: Calculate NDCG@k, based on given rank and gain values.
  
  Args:
    values: A list of tuples representing rank order and gain values.
    k: The maximum position to consider in calculating nDCG
  
  Returns:
    The value of NDCG@k, for the given list of values.
docstr_tok: ["Calculate","NDCG","@","k",",","based","on","given","rank","and","gain","values",".","Args",":","values",":","A","list","of","tuples","representing","rank","order","and","gain","values",".","k",":","The","maximum","position","to","consider","in","calculating","nDCG","Returns",":","The","value","of","NDCG","@","k",",","for","the","given","list","of","values","."]
code: def _calculate_ndcg(
      self, values: List[Tuple[int, float]], k: int
  ) -> float:
      """Calculate NDCG@k, based on given rank and gain values.
  
      Args:
        values: A list of tuples representing rank order and gain values.
        k: The maximum position to consider in calculating nDCG
  
      Returns:
        The value of NDCG@k, for the given list of values.
      """
      max_rank = min(k, len(values))
      ranked_values = [
          gain
          for _, gain in sorted(
              values, key=lambda x: x[0], reverse=False
          )
      ]
      optimal_values = [
          gain
          for _, gain in sorted(
              values, key=lambda x: x[1], reverse=True
          )
      ]
      dcg = self._calculate_dcg_at_k(max_rank, ranked_values)
      optimal_dcg = self._calculate_dcg_at_k(max_rank, optimal_values)
      if optimal_dcg > 0:
          return dcg / optimal_dcg
      else:
          return 0
  
imports: from typing import Dict, Iterable, List, Optional, Tuple, Union
  import apache_beam
  import numpy
  from tensorflow_model_analysis.metrics import metric_types
  from tensorflow_model_analysis.metrics import metric_util
  from tensorflow_model_analysis.proto import config_pb2
  from tensorflow_model_analysis.utils import util
label_desc: Use image node_id 5 for calling the _NDCGCombiner obj's underlying member method code with example usage: obj._calculate_ndcg(values, k) and returns: unknown, int