code: def _reciprocal_rank_at_k(
      target: np.ndarray, preds: np.ndarray, top_k: Optional[int] = None
  ):
      """Adaptation of `sklearn.metrics.label_ranking_average_precision_score`.
  
      Since the original sklearn metric works as RR only when the number of positive targets is exactly 1, here we remove
      every positive target that is not the most important. Remember that in RR only the positive target with the highest
      score is considered.
  
      """
      assert target.shape == preds.shape
      assert (
          len(target.shape) == 1
      )  # works only with single dimension inputs
  
      # take k largest predictions here because sklearn does not allow it
      if top_k is not None:
          top_k = min(top_k, len(preds))
          ind = np.argpartition(preds, -top_k)[-top_k:]
          target = target[ind]
          preds = preds[ind]
  
      # going to remove T targets that are not ranked as highest
      indexes = preds[target.astype(bool)]
      if len(indexes) > 0:
          target[
              preds != indexes.max(-1, keepdims=True)[0]
          ] = 0  # ensure that only 1 positive label is present
  
      if target.sum() > 0:
          # sklearn `label_ranking_average_precision_score` requires at most 2 dims
          return label_ranking_average_precision_score(
              np.expand_dims(target, axis=0),
              np.expand_dims(preds, axis=0),
          )
      return 0.0
  
file_path: torchmetrics/tests/unittests/retrieval/test_mrr.py
imports: from typing import Callable, Optional, Union
  import numpy
  import pytest
  from sklearn.metrics import label_ranking_average_precision_score
  from torch import Tensor
  from torchmetrics.functional.retrieval.reciprocal_rank import retrieval_reciprocal_rank
  from torchmetrics.retrieval.reciprocal_rank import RetrievalMRR
  from typing_extensions import Literal
  from unittests.helpers import seed_all
  from unittests.retrieval.helpers import RetrievalMetricTester, _concat_tests, _custom_aggregate_fn, _default_metric_class_input_arguments, _default_metric_class_input_arguments_ignore_index, _default_metric_functional_input_arguments, _errors_test_class_metric_parameters_default, _errors_test_class_metric_parameters_no_pos_target, _errors_test_functional_metric_parameters_default
label_desc: Use image node_id 1 for calling a global function with example usage: _reciprocal_rank_at_k(target, preds, top_k) and returns: int, label_ranking_average_precision_score