code: def test_dcg(self):
      wiki_rel = sps.lil_matrix(np.array([3, 3, 1, 0, 2]))
      wiki_rank = np.array([1, 2, 3, 4, 5])
      rel, k_mask, ror, ror_at_k = _setup_ndcg(wiki_rank, wiki_rel)
  
      # by hand...
      mat = np.array([3, 3, 1, 2])
      ranks = np.array([1, 2, 3, 5])
  
      numer_bh = 2 ** np.multiply(mat, [1, 1, 1, 1]) - 1
      denom_bh = np.log2(ranks + 1)
  
      # in function calc
      numer_func = (2 ** np.multiply(rel.data, k_mask)) - 1
      denom_func = np.log2(ror_at_k + 1)
  
      numer_all_eq = sum(
          [v[0] == v[1] for v in zip(numer_bh, numer_func)]
      )
      self.assertEqual(numer_all_eq, len(mat))
  
      denom_all_eq = sum(
          [v[0] == v[1] for v in zip(denom_bh, denom_func)]
      )
      self.assertEqual(denom_all_eq, len(mat))
  
      by_hand_dcg = np.sum(numer_bh / denom_bh)
      func_dcg = _dcg(rel, k_mask, ror_at_k, ror)
      self.assertEqual(by_hand_dcg, func_dcg)
  
      # functional NDCG should equal example another example
      # http://people.cs.georgetown.edu/~nazli/classes/ir-Slides/Evaluation-13.pdf
      # about .96 but since we use Burges 2005 formulation
      # it will be a little different
      v = func_dcg / _idcg(np.array([3, 3, 1, 0, 2]))
      self.assertAlmostEqual(v.item(0), 0.979762, 3)
  
file_path: tensorrec/test/test_eval.py
imports: from unittest import TestCase
  from numpy.random import shuffle
  import numpy
  import scipy.sparse
  from tensorrec import TensorRec
  from tensorrec.eval import recall_at_k, precision_at_k, f1_score_at_k, ndcg_at_k
  from tensorrec.eval import _setup_ndcg, _idcg, _dcg
  from tensorrec.util import generate_dummy_data_with_indicator
label_desc: Use image node_id 8 for calling the EvalTestCase obj's underlying member method code with example usage: obj.test_dcg() without return types