code: def NDCGatK_r(test_data, r, k):
      """
      Normalized Discounted Cumulative Gain
      rel_i = 1 or 0, so 2^{rel_i} - 1 = 1 or 0
      """
      assert len(r) == len(test_data)
      pred_data = r[:, :k]
  
      test_matrix = np.zeros((len(pred_data), k))
      for i, items in enumerate(test_data):
          length = k if k <= len(items) else len(items)
          test_matrix[i, :length] = 1
      max_r = test_matrix
      idcg = np.sum(max_r * 1.0 / np.log2(np.arange(2, k + 2)), axis=1)
      dcg = pred_data * (1.0 / np.log2(np.arange(2, k + 2)))
      dcg = np.sum(dcg, axis=1)
      idcg[idcg == 0.0] = 1.0
      ndcg = dcg / idcg
      ndcg[np.isnan(ndcg)] = 0.0
      return np.sum(ndcg)
  
imports: import paddle
  from paddle import nn, optimizer
  import numpy
  from paddle import log
  from dataloader import BasicDataset
  from paddle.io import Dataset
  from time import time
  from sklearn.metrics import roc_auc_score
  import random
  import os
  from tqdm import tqdm
label_desc: Use image node_id 6 for calling a global function with example usage: NDCGatK_r(test_data, r, k) and returns: np