code: def _sklearn_accuracy_multiclass(
      preds, target, 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
          )
          if average == "micro":
              return _sklearn_accuracy(target, preds)
          confmat = sk_confusion_matrix(
              target, preds, labels=list(range(NUM_CLASSES))
          )
          acc_per_class = confmat.diagonal() / confmat.sum(axis=1)
          acc_per_class[np.isnan(acc_per_class)] = 0.0
          if average == "macro":
              acc_per_class = acc_per_class[
                  (
                      np.bincount(preds, minlength=NUM_CLASSES)
                      + np.bincount(target, minlength=NUM_CLASSES)
                  )
                  != 0.0
              ]
              return acc_per_class.mean()
          if average == "weighted":
              weights = confmat.sum(1)
              return ((weights * acc_per_class) / weights.sum()).sum()
          return acc_per_class
  
      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)
          if average == "micro":
              res.append(_sklearn_accuracy(true, pred))
          else:
              confmat = sk_confusion_matrix(
                  true, pred, labels=list(range(NUM_CLASSES))
              )
              acc_per_class = confmat.diagonal() / confmat.sum(axis=1)
              acc_per_class[np.isnan(acc_per_class)] = 0.0
              if average == "macro":
                  acc_per_class = acc_per_class[
                      (
                          np.bincount(pred, minlength=NUM_CLASSES)
                          + np.bincount(true, minlength=NUM_CLASSES)
                      )
                      != 0.0
                  ]
                  res.append(
                      acc_per_class.mean()
                      if len(acc_per_class) > 0
                      else 0.0
                  )
              elif average == "weighted":
                  weights = confmat.sum(1)
                  score = (
                      (weights * acc_per_class) / weights.sum()
                  ).sum()
                  res.append(0.0 if np.isnan(score) else score)
              else:
                  res.append(acc_per_class)
      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 accuracy_score
  from sklearn.metrics import confusion_matrix
  from torchmetrics.classification.accuracy import Accuracy, BinaryAccuracy, MulticlassAccuracy, MultilabelAccuracy
  from torchmetrics.functional.classification.accuracy import accuracy, binary_accuracy, multiclass_accuracy, multilabel_accuracy
  from torchmetrics.metric import Metric
  from unittests import NUM_CLASSES, THRESHOLD
  from unittests.classification.inputs import _binary_cases, _input_binary, _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 4 for calling a global function with example usage: _sklearn_accuracy_multiclass(preds, target, ignore_index, multidim_average, average) and returns: np, acc_per_class, _sklearn_accuracy, acc_per_class, unknown