code: def ndcg_at_k(
      predicted_ranks, test_interactions, k=10, preserve_rows=False
  ):
      """
      Calculate Normalized Discounted Cumulative Gain @K.
      :param predicted_ranks: Numpy matrix
      The results of model.predict_rank()
      :param test_interactions: scipy.sparse matrix
      Test interactions matrix of shape [n_users, n_items]
      :param k: int
      The rank at which to stop evaluating NDCG.
      :param preserve_rows: bool
      If True, a value of 0 will be returned for every user without test interactions.
      If False, only users with test interactions will be returned.
      :return: np.array
      """
  
      relevance, k_mask, ranks_of_relevant, ror_at_k = _setup_ndcg(
          predicted_ranks, test_interactions, k
      )
  
      dcg = np.asarray(
          _dcg(relevance, k_mask, ror_at_k, ranks_of_relevant)
      )[0]
      idcg = np.apply_along_axis(_idcg, 1, relevance.A)
  
      ndcg = dcg / idcg
  
      if not preserve_rows:
          positive_test_interactions = test_interactions > 0
          ndcg = ndcg[positive_test_interactions.getnnz(axis=1) > 0]
  
      return ndcg
  
file_path: tensorrec/tensorrec/eval.py
imports: import numpy
  import scipy.sparse
  from .tensorrec import TensorRec
label_desc: Use image node_id 6 for calling a global function with example usage: ndcg_at_k(predicted_ranks, test_interactions, k, preserve_rows) and returns: ndcg