docstr: Method to calculate all the metrics for item recommendation scenario using dictionaries of ranking
  and test set. Use read() in ReadFile to transform your file in a dict
  
  :param predictions: Dictionary with ranking information
  :type predictions: dict
  
  :param test_set: Dictionary with test set information.
  :type test_set: dict
  
  :return: Dictionary with all evaluation metrics and results
  :rtype: dict
docstr_tok: ["Method","to","calculate","all","the","metrics","for","item","recommendation","scenario","using","dictionaries","of","ranking","and","test","set",".","Use","read","(",")","in","ReadFile","to","transform","your","file","in","a","dict",":","param","predictions",":","Dictionary","with","ranking","information",":","type","predictions",":","dict",":","param","test_set",":","Dictionary","with","test","set","information",".",":","type","test_set",":","dict",":","return",":","Dictionary","with","all","evaluation","metrics","and","results",":","rtype",":","dict"]
code: def evaluate(self, predictions, test_set):
      """
      Method to calculate all the metrics for item recommendation scenario using dictionaries of ranking
      and test set. Use read() in ReadFile to transform your file in a dict
  
      :param predictions: Dictionary with ranking information
      :type predictions: dict
  
      :param test_set: Dictionary with test set information.
      :type test_set: dict
  
      :return: Dictionary with all evaluation metrics and results
      :rtype: dict
  
      """
  
      eval_results = {}
      num_user = len(test_set["users"])
      partial_map_all = None
  
      if self.all_but_one_eval:
          for user in test_set["users"]:
              # select a random item
              test_set["items_seen_by_user"][user] = [
                  random.choice(
                      test_set["items_seen_by_user"].get(user, [-1])
                  )
              ]
  
      for i, n in enumerate(self.n_ranks):
          if n < 1:
              raise ValueError("Error: N must >= 1.")
  
          partial_precision = list()
          partial_recall = list()
          partial_ndcg = list()
          partial_map = list()
  
          for user in test_set["users"]:
              hit_cont = 0
              # Generate user intersection list between the recommended items and test.
              list_feedback = set(list(predictions.get(user, []))[:n])
              intersection = list(
                  list_feedback.intersection(
                      test_set["items_seen_by_user"].get(user, [])
                  )
              )
  
              if len(intersection) > 0:
                  ig_ranking = np.zeros(n)
                  for item in intersection:
                      hit_cont += 1
                      ig_ranking[
                          list(predictions[user]).index(item)
                      ] = 1
  
                  partial_precision.append(
                      precision_at_k([ig_ranking], n)
                  )
                  partial_recall.append(
                      (
                          float(len(intersection))
                          / float(
                              len(test_set["items_seen_by_user"][user])
                          )
                      )
                  )
                  partial_map.append(
                      mean_average_precision([ig_ranking])
                  )
                  partial_ndcg.append(ndcg_at_k(list(ig_ranking)))
  
              partial_map_all = partial_map
  
          # create a dictionary with final results
          eval_results.update(
              {
                  "PREC@"
                  + str(n): round(
                      sum(partial_precision) / float(num_user), 6
                  ),
                  "RECALL@"
                  + str(n): round(
                      sum(partial_recall) / float(num_user), 6
                  ),
                  "NDCG@"
                  + str(n): round(
                      sum(partial_ndcg) / float(num_user), 6
                  ),
                  "MAP@"
                  + str(n): round(
                      sum(partial_map) / float(num_user), 6
                  ),
                  "MAP": round(
                      sum(partial_map_all) / float(num_user), 6
                  ),
              }
          )
  
      # if (self.save_eval_file is not None):
      #     # Saving evaluations to a file
      #     from caserec.utils.process_data import WriteFile
  
      #     WriteFile(output_file=save_eval_file, data=)
  
      if self.verbose:
          self.print_results(eval_results)
  
      return eval_results
  
file_path: CaseRecommender/caserec/evaluation/item_recommendation.py
imports: import numpy
  import random
  from caserec.evaluation.base_evaluation import BaseEvaluation
  from caserec.evaluation.item_recomendation_functions import precision_at_k, mean_average_precision, ndcg_at_k
label_desc: Use image node_id 2 for calling the ItemRecommendationEvaluation obj's underlying member method code with example usage: obj.evaluate(predictions, test_set) and returns: eval_results