code: def ranking_eval(
      model,
      metrics,
      train_set,
      test_set,
      user_based=False,
      exclude_unknowns=True,
      mode="last",
      verbose=False,
  ):
      """Evaluate model on provided ranking metrics.
  
      Parameters
      ----------
      model: :obj:`cornac.models.NextItemRecommender`, required
          NextItemRecommender model to be evaluated.
  
      metrics: :obj:`iterable`, required
          List of rating metrics :obj:`cornac.metrics.RankingMetric`.
  
      train_set: :obj:`cornac.data.SequentialDataset`, required
          SequentialDataset to be used for model training. This will be used to exclude
          observations already appeared during training.
  
      test_set: :obj:`cornac.data.SequentialDataset`, required
          SequentialDataset to be used for evaluation.
  
      exclude_unknowns: bool, optional, default: True
          Ignore unknown users and items during evaluation.
  
      verbose: bool, optional, default: False
          Output evaluation progress.
  
      Returns
      -------
      res: (List, List)
          Tuple of two lists:
           - average result for each of the metrics
           - average result per user for each of the metrics
  
      """
  
      if len(metrics) == 0:
          return [], []
  
      avg_results = []
      session_results = [defaultdict(list) for _ in enumerate(metrics)]
      user_results = [defaultdict(list) for _ in enumerate(metrics)]
  
      user_sessions = defaultdict(list)
      session_ids = []
      for [sid], [mapped_ids], [session_items] in tqdm(
          test_set.si_iter(batch_size=1, shuffle=False),
          total=len(test_set.sessions),
          desc="Ranking",
          disable=not verbose,
          miniters=100,
      ):
          if (
              len(session_items) < 2
          ):  # exclude all session with size smaller than 2
              continue
          user_idx = test_set.uir_tuple[0][mapped_ids[0]]
          if user_based:
              user_sessions[user_idx].append(sid)
          session_ids.append(sid)
  
          start_pos = 1 if mode == "next" else len(session_items) - 1
          for test_pos in range(start_pos, len(session_items), 1):
              test_pos_items = session_items[test_pos]
  
              # binary mask for ground-truth positive items
              u_gt_pos_mask = np.zeros(test_set.num_items, dtype="int")
              u_gt_pos_mask[test_pos_items] = 1
  
              # binary mask for ground-truth negative items, removing all positive items
              u_gt_neg_mask = np.ones(test_set.num_items, dtype="int")
              u_gt_neg_mask[test_pos_items] = 0
  
              # filter items being considered for evaluation
              if exclude_unknowns:
                  u_gt_pos_mask = u_gt_pos_mask[: train_set.num_items]
                  u_gt_neg_mask = u_gt_neg_mask[: train_set.num_items]
  
              u_gt_pos_items = np.nonzero(u_gt_pos_mask)[0]
              u_gt_neg_items = np.nonzero(u_gt_neg_mask)[0]
              item_indices = np.nonzero(u_gt_pos_mask + u_gt_neg_mask)[
                  0
              ]
  
              item_rank, item_scores = model.rank(
                  user_idx,
                  item_indices,
                  history_items=session_items[:test_pos],
                  history_mapped_ids=mapped_ids[:test_pos],
                  sessions=test_set.sessions,
                  session_indices=test_set.session_indices,
                  extra_data=test_set.extra_data,
              )
  
              for i, mt in enumerate(metrics):
                  mt_score = mt.compute(
                      gt_pos=u_gt_pos_items,
                      gt_neg=u_gt_neg_items,
                      pd_rank=item_rank,
                      pd_scores=item_scores,
                      item_indices=item_indices,
                  )
                  if user_based:
                      user_results[i][user_idx].append(mt_score)
                  else:
                      session_results[i][sid].append(mt_score)
  
      # avg results of ranking metrics
      for i, mt in enumerate(metrics):
          if user_based:
              user_ids = list(user_sessions.keys())
              user_avg_results = [
                  np.mean(user_results[i][user_idx])
                  for user_idx in user_ids
              ]
              avg_results.append(np.mean(user_avg_results))
          else:
              session_result = [
                  score
                  for sid in session_ids
                  for score in session_results[i][sid]
              ]
              avg_results.append(np.mean(session_result))
      return avg_results, user_results
  
file_path: cornac/cornac/eval_methods/next_item_evaluation.py
imports: import time
  import warnings
  from collections import OrderedDict, defaultdict
  import numpy
  from tqdm.auto import tqdm
  from ..data import SequentialDataset
  from ..experiment.result import Result
  from ..models import NextItemRecommender
  from .None import BaseMethod
label_desc: Use image node_id 1 for calling a global function with example usage: ranking_eval(model, metrics, train_set, test_set, user_based, exclude_unknowns, mode, verbose) and returns: avg_results,  user_results, list,  list