code: def precision_at_k(r, k):
      """Score is precision @ k
      Relevance is binary (nonzero is relevant).
      Returns:
          Precision @ k
      Raises:
          ValueError: len(r) must be >= k
      """
      assert k >= 1
      r = np.asarray(r)[:k]
      return np.mean(r)
  
file_path: dgl/examples/pytorch/NGCF/NGCF/utility/metrics.py
imports: import numpy
  from sklearn.metrics import roc_auc_score
label_desc: Use image node_id 2 for calling a global function with example usage: precision_at_k(r, k) and returns: np