class_docstr: This class evaluates an Information Retrieval (IR) setting.
  
  Given a set of queries and a large corpus set. It will retrieve for each query the top-k most similar document. It measures
  Mean Reciprocal Rank (MRR), Recall@k, and Normalized Discounted Cumulative Gain (NDCG)
class_docstr_tok: ["This","class","evaluates","an","Information","Retrieval","(","IR",")","setting",".","Given","a","set","of","queries","and","a","large","corpus","set",".","It","will","retrieve","for","each","query","the","top-k","most","similar","document",".","It","measures","Mean","Reciprocal","Rank","(","MRR",")",",","Recall","@","k",",","and","Normalized","Discounted","Cumulative","Gain","(","NDCG",")"]
code: def compute_metrics(self, queries_result_list: List[object]):
      # Init score computation values
      num_hits_at_k = {k: 0 for k in self.accuracy_at_k}
      precisions_at_k = {k: [] for k in self.precision_recall_at_k}
      recall_at_k = {k: [] for k in self.precision_recall_at_k}
      MRR = {k: 0 for k in self.mrr_at_k}
      ndcg = {k: [] for k in self.ndcg_at_k}
      AveP_at_k = {k: [] for k in self.map_at_k}
  
      # Compute scores on results
      for query_itr in range(len(queries_result_list)):
          query_id = self.queries_ids[query_itr]
  
          # Sort scores
          top_hits = sorted(
              queries_result_list[query_itr],
              key=lambda x: x["score"],
              reverse=True,
          )
          query_relevant_docs = self.relevant_docs[query_id]
  
          # Accuracy@k - We count the result correct, if at least one relevant doc is across the top-k documents
          for k_val in self.accuracy_at_k:
              for hit in top_hits[0:k_val]:
                  if hit["corpus_id"] in query_relevant_docs:
                      num_hits_at_k[k_val] += 1
                      break
  
          # Precision and Recall@k
          for k_val in self.precision_recall_at_k:
              num_correct = 0
              for hit in top_hits[0:k_val]:
                  if hit["corpus_id"] in query_relevant_docs:
                      num_correct += 1
  
              precisions_at_k[k_val].append(num_correct / k_val)
              recall_at_k[k_val].append(
                  num_correct / len(query_relevant_docs)
              )
  
          # MRR@k
          for k_val in self.mrr_at_k:
              for rank, hit in enumerate(top_hits[0:k_val]):
                  if hit["corpus_id"] in query_relevant_docs:
                      MRR[k_val] += 1.0 / (rank + 1)
                      break
  
          # NDCG@k
          for k_val in self.ndcg_at_k:
              predicted_relevance = [
                  1
                  if top_hit["corpus_id"] in query_relevant_docs
                  else 0
                  for top_hit in top_hits[0:k_val]
              ]
              true_relevances = [1] * len(query_relevant_docs)
  
              ndcg_value = self.compute_dcg_at_k(
                  predicted_relevance, k_val
              ) / self.compute_dcg_at_k(true_relevances, k_val)
              ndcg[k_val].append(ndcg_value)
  
          # MAP@k
          for k_val in self.map_at_k:
              num_correct = 0
              sum_precisions = 0
  
              for rank, hit in enumerate(top_hits[0:k_val]):
                  if hit["corpus_id"] in query_relevant_docs:
                      num_correct += 1
                      sum_precisions += num_correct / (rank + 1)
  
              avg_precision = sum_precisions / min(
                  k_val, len(query_relevant_docs)
              )
              AveP_at_k[k_val].append(avg_precision)
  
      # Compute averages
      for k in num_hits_at_k:
          num_hits_at_k[k] /= len(self.queries)
  
      for k in precisions_at_k:
          precisions_at_k[k] = np.mean(precisions_at_k[k])
  
      for k in recall_at_k:
          recall_at_k[k] = np.mean(recall_at_k[k])
  
      for k in ndcg:
          ndcg[k] = np.mean(ndcg[k])
  
      for k in MRR:
          MRR[k] /= len(self.queries)
  
      for k in AveP_at_k:
          AveP_at_k[k] = np.mean(AveP_at_k[k])
  
      return {
          "accuracy@k": num_hits_at_k,
          "precision@k": precisions_at_k,
          "recall@k": recall_at_k,
          "ndcg@k": ndcg,
          "mrr@k": MRR,
          "map@k": AveP_at_k,
      }
  
file_path: sentence-transformers/sentence_transformers/evaluation/InformationRetrievalEvaluator.py
imports: from .None import SentenceEvaluator
  import torch
  from torch import Tensor
  import logging
  from tqdm import trange
  from ..util import cos_sim, dot_score
  import os
  import numpy
  from typing import List, Dict, Set, Callable
  import heapq
label_desc: Use image node_id 4 for calling the InformationRetrievalEvaluator obj's underlying member method code with example usage: obj.compute_metrics(queries_result_list) and returns: dict