code: def precision_recall_score(model, test, train=None, k=10):
      """
      Compute Precision@k and Recall@k scores. One score
      is given for every user with interactions in the test
      set, representing the Precision@k and Recall@k of all their
      test items.
  
      Parameters
      ----------
  
      model: fitted instance of a recommender model
          The model to evaluate.
      test: :class:`spotlight.interactions.Interactions`
          Test interactions.
      train: :class:`spotlight.interactions.Interactions`, optional
          Train interactions. If supplied, scores of known
          interactions will not affect the computed metrics.
      k: int or array of int,
          The maximum number of predicted items
      Returns
      -------
  
      (Precision@k, Recall@k): numpy array of shape (num_users, len(k))
          A tuple of Precisions@k and Recalls@k for each user in test.
          If k is a scalar, will return a tuple of vectors. If k is an
          array, will return a tuple of arrays, where each row corresponds
          to a user and each column corresponds to a value of k.
      """
  
      test = test.tocsr()
  
      if train is not None:
          train = train.tocsr()
  
      if np.isscalar(k):
          k = np.array([k])
  
      precision = []
      recall = []
  
      for user_id, row in enumerate(test):
          if not len(row.indices):
              continue
  
          predictions = -model.predict(user_id)
  
          if train is not None:
              rated = train[user_id].indices
              predictions[rated] = FLOAT_MAX
  
          predictions = predictions.argsort()
  
          targets = row.indices
  
          user_precision, user_recall = zip(
              *[
                  _get_precision_recall(predictions, targets, x)
                  for x in k
              ]
          )
  
          precision.append(user_precision)
          recall.append(user_recall)
  
      precision = np.array(precision).squeeze()
      recall = np.array(recall).squeeze()
  
      return precision, recall
  
imports: import numpy
  import scipy.stats
label_desc: Use image node_id 5 for calling a global function with example usage: precision_recall_score(model, test, train, k) and returns: precision,  recall