code: def _sklearn_precision_recall_multiclass(
      preds, target, sk_fn, ignore_index, multidim_average, average
  ):
      if preds.ndim == target.ndim + 1:
          preds = torch.argmax(preds, 1)
  
      if multidim_average == "global":
          preds = preds.numpy().flatten()
          target = target.numpy().flatten()
          target, preds = remove_ignore_index(
              target, preds, ignore_index
          )
          return sk_fn(
              target,
              preds,
              average=average,
              labels=list(range(NUM_CLASSES))
              if average is None
              else None,
          )
  
      preds = preds.numpy()
      target = target.numpy()
      res = []
      for pred, true in zip(preds, target):
          pred = pred.flatten()
          true = true.flatten()
          true, pred = remove_ignore_index(true, pred, ignore_index)
          r = sk_fn(
              true,
              pred,
              average=average,
              labels=list(range(NUM_CLASSES))
              if average is None
              else None,
          )
          res.append(0.0 if np.isnan(r).any() else r)
  
      return np.stack(res, 0)
  
imports: from functools import partial
  import numpy
  import pytest
  import torch
  from scipy.special import expit
  from sklearn.metrics import confusion_matrix
  from sklearn.metrics import precision_score
  from sklearn.metrics import recall_score
  from torch import Tensor, tensor
  from torchmetrics.classification.precision_recall import BinaryPrecision, BinaryRecall, MulticlassPrecision, MulticlassRecall, MultilabelPrecision, MultilabelRecall, Precision, Recall
  from torchmetrics.functional.classification.precision_recall import binary_precision, binary_recall, multiclass_precision, multiclass_recall, multilabel_precision, multilabel_recall
  from torchmetrics.metric import Metric
  from unittests import NUM_CLASSES, THRESHOLD
  from unittests.classification.inputs import _binary_cases, _multiclass_cases, _multilabel_cases
  from unittests.helpers import seed_all
  from unittests.helpers.testers import MetricTester, inject_ignore_index, remove_ignore_index
label_desc: Use image node_id 2 for calling a global function with example usage: _sklearn_precision_recall_multiclass(preds, target, sk_fn, ignore_index, multidim_average, average) and returns: np, sk_fn